如何从 jsp 将日期输入到 Struts2 动作?

How to input Date to a Struts2 action from a jsp?(如何从 jsp 将日期输入到 Struts2 动作?)

本文介绍了如何从 jsp 将日期输入到 Struts2 动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 jsp 页面中遇到了很多问题.我应该使用什么标签从用户那里获取日期(yyyy-MM-dd)并将其存储在 Struts2 操作的 Date 属性中?

I am facing a lot of problems here in my jsp page. What tag I should use to get a date (yyyy-MM-dd) from user and store it in a Date property of a Struts2 action ?

我的操作中的属性被声明为 java.util.Date.我希望来自 jsp 页面的输入登陆这个属性.

The property in my action is declared to be of java.util.Date. I want the input from jsp page to land in this property.

请帮忙.

如果使用 s:textfield 标记以正确的格式输入日期,我会收到 Invalid field 错误(在 JSP 中).

I get Invalid field error (in JSP) if is use a s:textfield tag to manyally enter a date in the right format.

推荐答案

我知道这篇文章有点老了,但一个解决方案可能对其他人有用.

I know this post is a bit old but a solution may be useful to others.

Struts 的默认转换器似乎无法正常工作.字段错误甚至发生在从操作填充的只读字段中.

The default converter of Struts does not seem to work properly. The field error even occurs with readonly fields populated from the action.

我通过定义自己的转换器解决了这个问题:

I resolved it by defining my own converter:

  1. 创建转换器类(使用您需要的日期格式):

  1. Create the converter class (using the date format you need):

public class StringToDateTimeConverter extends StrutsTypeConverter {
    // WARNING not safe in multi-threaded environments
    private static final DateFormat DATETIME_FORMAT = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss");

    public Object convertFromString(Map context, String[] strings, Class toClass) {     
        if (strings == null || strings.length == 0 || strings[0].trim().length() == 0) {
            return null;
        }

        try {
            return DATETIME_FORMAT.parse(strings[0]);
        } catch (ParseException e) {
            throw new TypeConversionException("Unable to convert given object to date: " + strings[0]);
        }
    }

    public String convertToString(Map context, Object date) {
        if (date != null && date instanceof Date) {         
            return DATETIME_FORMAT.format(date);
        } else {
            return null;
        }
    }
}

  • 将转换注解放在属性设置器上(或使用conversion.properties 文件)

  • Put the conversion annotation on the property setter (or use a conversion.properties file)

    @Conversion()
    public class MyEditAction {
        // ...
    
        @TypeConversion(converter="my.app.common.converter.StringToDateTimeConverter")
        public void setUploadedDate(Date uploadedDate) {
            this.uploadedDate = uploadedDate;
        }
    

  • 在我的情况下,我不需要编辑它,但想在 jsp 中指定格式.我使用了一个额外的隐藏字段来保留原始值(另一种选择是使用 Preparable 接口):

    In my case I did not need to edit it but wanted to specify the format in the jsp. I used an additional hidden field to keep the original value (another alternative would have been the use of Preparable interface):

    <s:textfield name="uploadedDateDisplay" value="%{getText('format.datetimesecond',{uploadedDate})}" size="70" disabled="true"/>
    <s:hidden name="uploadedDate" />
    

    这篇关于如何从 jsp 将日期输入到 Struts2 动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

    本文标题为:如何从 jsp 将日期输入到 Struts2 动作?

    基础教程推荐