I am using Spring 3 MVC Annotation Validation. I know how to do it in JSP(using modelAttribute property in JSP), but I don’t have any clue how to apply this kind of data binding in Freemarker.
Example of JSP equivalence
<c:url var="addUrl" value="/admin/workerAdd" />
<form:form modelAttribute="worker" action="${addUrl}" method="post">
<p>
<form:label for="code" path="code">Code</form:label>
<form:input path="code" readonly="false" />
</p>
...
Example of Controller:
@Controller
@RequestMapping("/admin/*")
public class AdminController {
@Autowired
private CommonService commonService;
/**
* For every request for this controller, this will
* create a person instance for the form.
*/
@ModelAttribute
public Worker newRequest(@RequestParam(required=false) String id) {
return (id != null ? commonService.getWorkerById(id) : new Worker());
}
@RequestMapping(value="/workerAdd", method=RequestMethod.POST)
public final String performAddUser(@Valid Worker worker, BindingResult result) throws Exception {
if (result.hasErrors()) {
return null;
}
// Create the worker here.
return "redirect:/administration/manageUser";
}
Now I want to use the same controller, but the views are written by Freemarker(ftl). The data-binding in my below freemarker doesn’t work (which is understandable, since it should have different syntax). I did some research and learn about command object in FTL, but I don’t get the point. I think it should be a similar attribute that tell Spring to do the binding but I still not find it yet.
<form id="worker" modelAttribute="worker" action="${rc.getContextUrl('/admin/workerAdd')}" method="post" >
<div class="Entry">
<label for="code">Code</label>
<input type="text" id="code" name="code" value="${worker.code}" />
</div>
Is there any simple way to make this annotation validation way (and the databinding as well) works with FTL? Any help will be appreciated.
Thanks,
Hoang Long
You JSP code can be rewritten in Freemarker as follows:
Note that Spring library for Freemarker doesn’t have specifal element for
form, so you need to use ordinary htmlformand prepend model attribute name to paths of individual fields.See also: