Java Spring:将字符串从视图转换为日历对象
如何将表单输入(例如easyui datetimebox)中的字符串转换为控制器中由Spring自动索引的对象中的日历属性 我已经读过了,但是我找不到任何接近要点的东西 JSP: 附言:春天3 编辑:添加控制器的方法以执行操作:Java Spring:将字符串从视图转换为日历对象,java,spring,jsp,spring-mvc,Java,Spring,Jsp,Spring Mvc,如何将表单输入(例如easyui datetimebox)中的字符串转换为控制器中由Spring自动索引的对象中的日历属性 我已经读过了,但是我找不到任何接近要点的东西 JSP: 附言:春天3 编辑:添加控制器的方法以执行操作: @Controller @RequestMapping("/project/MaintainProjectFrm") @SessionAttributes({"project","SessionDeadLineDate"}) public class MaintainP
@Controller
@RequestMapping("/project/MaintainProjectFrm")
@SessionAttributes({"project","SessionDeadLineDate"})
public class MaintainProjectController {
/* ... many methods... */
@RequestMapping(params = "update", method = RequestMethod.POST, produces={"text/plain; charset=UTF-8"})
public String update(@ModelAttribute("project") Project project,
BindingResult result,
SessionStatus status,
ModelMap model,
HttpServletRequest req,
HttpServletResponse resp) throws IOException {
projectValidator.validate(project, result);
if (result.hasErrors()) {
//has errors, in this case, that one shown in text above, which is rendered again in view (JSP)
return "/project/MaintainProjectFrm";
} else {
try{
mpService.updateProject(project);
}
catch(Exception e){
resp.setStatus(500);
resp.getWriter().write("Error updating project: " + e.getMessage());
return "/project/MaintainProjectFrm";
}
status.setComplete();
}
}
/* ... yet other methods ... */
}
实现这一点有两种可能:可以使用
PropertyEditor
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Calendar.class, new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(parseDate());
}
private Calendar parseDate() {
try {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
cal.setTime(sdf.parse("Mon Mar 14 16:02:37 GMT 2011"));
return cal;
} catch (ParseException e) {
return null;
}
}
});
}
文件
或者您可以使用spring转换服务。请参见:.我假设您的
项目
类具有字段DeadLineDate
(字段应以小写字符开头)
用@DateTimeFormat
这样注释它
@DateTimeFormat(pattern = "yyyy/MM/dd") // or whatever pattern you want
private Calendar DeadLineDate;
然后,您的客户将需要发送适当的模式。尝试一下Sotirios Delimanolis说的方法
@DateTimeFormat(pattern = "yyyy/MM/dd") // or whatever pattern you want
private Calendar DeadLineDate;
最后,将其添加到pom.xml中:
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.3</version>
</dependency>
乔达时间
乔达时间
2.3
我们可以看看你的处理方法吗?@SotiriosDelimanolis:当然可以,完成了!我将使用@RequestParam从视图中接收DeadLineDate,并在其上手动创建日历对象,更新项目对象。但这当然不是最好的方式!我想知道,在某种情况下,Spring是否能够从该属性自动绑定日历。太棒了!!!我期待更多关于@gregor写的东西,但肯定这是最简单的方式。春天有这样的灵活性真是太好了。是的,我肯定在使用java模式。我的财产在从葡萄牙语翻译物品名称时被“误算”了。谢谢!太棒了!谢谢你的回答(我投了赞成票)。但我更倾向于使用Sotirios Delimanolis方法,这是最简单的解决方案。问候@亚历克斯:公认的解决方案对Spring3.1有效吗?就我而言,这个答案救了我!谢谢你,格雷戈+1@sarwar026,我实际上正在升级到Spring3.6,但我还不能测试它。@Alex:我明白了。我也会尝试升级我的。谢谢
@DateTimeFormat(pattern = "yyyy/MM/dd") // or whatever pattern you want
private Calendar DeadLineDate;
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.3</version>
</dependency>