我正在使用Spring 3 MVC註釋驗證。我知道如何在JSP中使用它(在JSP中使用modelAttribute
屬性),但我不知道如何在Freemarker中應用這種數據綁定。 JSP等價的使用Spring MVC註釋驗證與Freemarker
實施例控制器的
<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一起工作?任何幫助將不勝感激。
感謝,
晃龍
它的工作原理,非常感謝你!你知道任何其他資源提供完整的示例項目。閱讀鏈接時有點困惑 – 2011-05-27 02:08:21
@Hoàng:不,我個人必須閱讀源代碼和測試才能得到它。 – axtavt 2011-05-27 08:02:08