[英]How to convert a calendar object to date in struts2 textfield tag
I am editing a users profile, I need a way to edit the user's date of birth. 我正在编辑用户个人资料,我需要一种方法来编辑用户的出生日期。 In my action class the dateOfBirth is a Calendar object.
在我的动作类中,dateOfBirth是一个Calendar对象。 Now how can populate the date in the date of birth text field.
现在如何在出生日期文本字段中填充日期。
<s:textfield id="txtDob" name="dateOfBirth" />
public class Person {
private Calendar dateOfBirth;
public Calendar getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Calendar dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
}
when i run the jsp, this is what i get inside the textfield 当我运行jsp时,这就是我进入文本域的内容
java.util.GregorianCalendar[time=366229800000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Calcutta",offset=19800000,dstSavings=0,useDaylight=false,transitions=6,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=1981,MONTH=7,WEEK_OF_YEAR=33,WEEK_OF_MONTH=3,DAY_OF_MONTH=10,DAY_OF_YEAR=222,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=2,AM_PM=0,HOUR=0,HOUR_OF_DAY=0,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=19800000,DST_OFFSET=0]
Instead of using s:textfield tag try using dojo ajax sx:datetimepicker tag. 而不是使用s:textfield标记尝试使用dojo ajax sx:datetimepicker标记。 Before using dojo tags add respective jars.
在使用dojo标签之前添加相应的jar。
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<html>
<head>
<title>Example/title>
<s:head />
<sx:head />
</head>
<body>
<sx:datetimepicker name="dateOfBirth" label="Date Of Birth"
displayFormat="MM/dd/yyyy" />
</body>
</html>
This is because struts2 has a set of type convertors out of the box and struts2 only supports date conversion and it uses the SHORT format for the Locale associated with the current request. 这是因为struts2有一组开箱即用的类型转换器,struts2只支持日期转换,它使用与当前请求关联的Locale的SHORT格式。
You can create your custom type convertor and can tell struts2 to use that type convertor for Calendar object. 您可以创建自定义类型转换器,并可以告诉struts2将该类型转换器用于Calendar对象。 creating a custom type convertor is quite easy and straight forward.make use of
StrutsTypeConverter
class being provided by S2 for this purpose 创建自定义类型转换器非常简单直接。可以使用S2为此提供的
StrutsTypeConverter
类
public class MyConverter extends StrutsTypeConverter {
public Object convertFromString(Map context, String[] values, Class toClass) {
.....
}
public String convertToString(Map context, Object o) {
.....
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.