Srping MultiactionController 使用綁定的設定

跟simpleform controller一樣,先在xml中設定好validator










只要validator的support有支援到在multiaction controller中的command的時候
就會採用該valiodator

接下來新增一個MyMultiacitonController 擴展原本的multiaction controller

public class MyMultiactionController extends MultiActionController {
//用來儲存binding error的model
private Map errorModel = null;
//以下是修改原始程式碼的結果
@Override
protected void bind(HttpServletRequest request, Object command)
throws Exception {
logger.debug("Binding request parameters onto MultiActionController command");
ServletRequestDataBinder binder = createBinder(request, command);
binder.bind(request);
if (getValidators() != null) {
for (int i = 0; i < getValidators().length; i++) {
if (getValidators()[i].supports(command.getClass())) {
ValidationUtils.invokeValidator(getValidators()[i],
command, binder.getBindingResult());
}
}
}
if (binder.getBindingResult().hasErrors()) {
setErrorModel(binder.getBindingResult().getModel());
}
}

public Map getErrorModel() {
Map map = this.errorModel;
this.errorModel = null;
return map;
}

public void setErrorModel(Map errorModel) {
this.errorModel = errorModel;
}
}

所以只要你的controller 去 extend 完MyMultiactionController後
在你的某個action中(這邊以save為例)

public ModelAndView save(HttpServletRequest request,
HttpServletResponse response, MemberForm command) {
ModelAndView mav = new ModelAndView("member.list");
mav.addAllObjects(getErrorModel());
mav.addObject("command", command);
return mav;
}

只要有上面粗體的代碼,標籤就可以像simple form controller一 樣的運作了

留言

這個網誌中的熱門文章

序曲Overture

Tomcat並存