将Spring 3 MVC注释验证与Freemarker一起使用
||
我正在使用Spring 3 MVC注释验证。我知道如何在JSP中做到这一点(使用JSP中的
modelAttribute
属性),但是我不知道如何在Freemarker中应用这种数据绑定。
JSP等价示例
<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>
...
控制器示例:
@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\";
}
现在,我想使用相同的控制器,但是视图由Freemarker(ftl)编写。我下面的freemarker中的数据绑定不起作用(这是可以理解的,因为它应该具有不同的语法)。我进行了一些研究,并了解了FTL中的命令对象,但我不明白这一点。我认为应该是类似的属性,告诉Spring进行绑定,但是我仍然找不到它。
<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>
有什么简单的方法可以使此批注验证方法(以及数据绑定)与FTL一起使用?任何帮助将不胜感激。
谢谢,
黄朗
没有找到相关结果
已邀请:
2 个回复
喷乡顾沥沪
请注意,Freemarker的Spring库没有用于
的特殊元素,因此您需要使用普通的html
并将模型属性名称添加到各个字段的路径之前。 也可以看看: 16.4速度和FreeMarker
枫湃揩乾纲