Ale*_*lex 3 java spring jsp spring-mvc
如何将表单输入(easyui-datetimebox,以防万一)中的字符串转换为Controller中对象的Calendar属性,由Spring自动进行?
我已经阅读了http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/validation.html,但我找不到任何接近正确的地方.
JSP:
<input id="DeadLineDate"
class="easyui-datetimebox"
name="DeadLineDate"
value="${SessionDeadLineDate}"
data-options="formatter:myformatter,
parser:myparser
/>
Run Code Online (Sandbox Code Playgroud)
当提交时,Spring验证会抛出一个错误:
Failed to convert property value of type java.lang.String to required type java.util.Calendar for property DeadLineDate; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [java.util.Calendar] for property DeadLineDate: no matching editors or conversion strategy found.
Run Code Online (Sandbox Code Playgroud)
PS:春天3
编辑:添加控制器的方法来执行操作:
@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 ... */
}
Run Code Online (Sandbox Code Playgroud)
我假设你的Project类有字段DeadLineDate(字段应该以小写字符开头).
用@DateTimeFormat这样的方式注释它
@DateTimeFormat(pattern = "yyyy/MM/dd") // or whatever pattern you want
private Calendar DeadLineDate;
Run Code Online (Sandbox Code Playgroud)
然后,您的客户需要发送适当的模式.