开发者

Use Spring 3 MVC Annotation Validation with Freemarker

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 th开发者_运维问答e 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


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.


You JSP code can be rewritten in Freemarker as follows:

<#import "spring.ftl" as spring />
...
<form id="worker" action="${rc.getContextUrl('/admin/workerAdd')}" method="post" > 
    <p>
        <label for = "code">Code</label>
        <@spring.formInput "worker.code" />
    </p> 
...

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:

  • 16.4 Velocity & FreeMarker
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜