Take the 2-minute tour ×

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">
 <form:label for="code" path="code">Code</form:label>
     <form:input path="code" readonly="false" />

Example of Controller:

public class AdminController {    
    private CommonService commonService;

     * For every request for this controller, this will 
     * create a person instance for the form.
    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}" />

Is there any simple way to make this annotation validation way (and the databinding as well) works with FTL? Any help will be appreciated.


Hoang Long

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

You JSP code can be rewritten in Freemarker as follows:

<#import "spring.ftl" as spring />
        <@spring.formInput "worker.code" />

Note that Spring library for Freemarker doesn't have specifal element for form, so you need to use ordinary html form and prepend model attribute name to paths of individual fields.

See also:

share|improve this answer
it works, thank you a lot! Do you know any other resources that offer full sample project. I'm a bit confused when reading the link –  Hoàng Long May 27 '11 at 2:08
@Hoàng: No. Personally I had to read source code and tests to get it. –  axtavt May 27 '11 at 8:02
add comment

I'd like to add the following clarification to save people some time. When reading the docs, it says that unless you override the @ModelAttribute(value=...) the bean would be accessed as "command" in your view.

For Freemarker (tested with 3.1M1), the default is className (eg if the Command class is named "Change PasswordCommand", then the bean will be bound to changePasswordCommand by default.

share|improve this answer
thanks for the explanation. I think it definitely help others:) –  Hoàng Long Jun 28 '11 at 2:15
add comment

Your Answer


