在 Struts2 中输入日期 [英] Input Date in Struts2
问题描述
如何在struts 2中改变java.util.Date对象的格式.我测试过,Struts 2接受m/d/yy.是否可以更改格式?
以及如何更改默认消息字段日期的字段值无效"?
struts2 中的日期标签只能在输出中使用.
您可以将格式传递给 s:date
标签
要更改默认错误消息,在您的validation.xml
中,您可以添加消息参数
<field-validator type="date"><message key="errors.myDateRequired"/></field-validator></field>
然后在您的 ApplicationResources.properties
文件中,您将为 errors.myDateRequired
更新:根据评论
s:date
只是一个只读标签,这意味着不能用于接受用户的输入.
为了让 textField 接受不同格式的日期,有几种方法:
- 使用 struts2-jQuery 插件,它有一个非常好的日期选择器,支持不同的日期时间格式,展示、DatePickerTag.立>
- 用于格式化用户输入的自定义 JavaScript 代码.
- 使用 jQuery(如果允许)并使用 jQuery 日期选择器
您可以使用自定义转换器为日期使用文本字段,示例>
How to change the format of java.util.Date object in struts 2. What I have tested, Struts 2 accepts m/d/yy. Is it possible to change the format?
And also how to change the default message "Invalid field value for field date"?
date tag in struts2 can only be used in output.
You can pass format to s:date
tag
<s:date name="myDate" format="yyyy-MM-dd" />
To change the default error message, in your validation.xml
you can add message parameter
<field name="myDate">
<field-validator type="date">
<message key="errors.myDateRequired" />
</field-validator>
</field>
Then in your ApplicationResources.properties
file you will provide value for errors.myDateRequired
Update: As per comment
s:date
is only a read only tag which means cannot be used to accept input from user.
For enabling textField to accept date in different format, couple of approaches:
- Use struts2-jQuery plugin, it has a very nice date picker which support different date timeformat, showcase, DatePickerTag.
- Custom javascript code to format user input.
- Use jQuery (if allowed) and use jQuery datepicker
You can use custom converter to use text field for date, example
这篇关于在 Struts2 中输入日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!