How can I convert a string from a form input (easyui-datetimebox, in case) to a Calendar property in an object in Controller, autobinded by Spring?
I've read http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/validation.html but I couldn't find anything nearly right-to-the-point there.
JSP:
<input id="DeadLineDate"
class="easyui-datetimebox"
name="DeadLineDate"
value="${SessionDeadLineDate}"
data-options="formatter:myformatter,
parser:myparser
/>
When submited, Spring validation throws an error:
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.
PS: Spring 3
Edit: adding controller's method to perform operation:
@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 ... */
}
I'm assuming your Project
class has the field DeadLineDate
(fields should start with a lowercase character).
Annotate it with @DateTimeFormat
like so
@DateTimeFormat(pattern = "yyyy/MM/dd") // or whatever pattern you want
private Calendar DeadLineDate;
Your client will then need to send the appropriate pattern.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments