How to input Date to a Struts2 action from a jsp?(如何从 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:
创建转换器类(使用您需要的日期格式):
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 动作?
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01