如何从jsp向Struts2动作输入Date? [英] How to input Date to a Struts2 action from a jsp?

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

问题描述

我在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.

请帮忙。

我得到无效字段错误(在JSP中)如果用作:textfield tag to manyally以正确的格式输入日期。

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动作输入Date?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆