3
這可能嗎?我基本上使用MultiActionController類製作Spring CRUD Web應用程序,並希望驗證我的表單。我之前使用過SimpleUrlController,而且valang完美地工作。在MultiActionController ModelAndView方法中調用valang驗證器...可能嗎?怎麼樣?
這可能嗎?我基本上使用MultiActionController類製作Spring CRUD Web應用程序,並希望驗證我的表單。我之前使用過SimpleUrlController,而且valang完美地工作。在MultiActionController ModelAndView方法中調用valang驗證器...可能嗎?怎麼樣?
根據MultiActionControlller API
考慮一個ServletRequestDataBinder的直接使用在你的控制器的方法,而不是依靠一個宣佈命令的說法。這允許完全控制整個活頁夾的設置和使用,包括驗證者的啓動和結合/驗證錯誤的後續評估。
所以,你可以使用
public class AddMultiActionController extends MultiActionController {
public AddMultiActionController() {
// Set up Valang according to your business requirement
// You can use xml settings instead
setValidators(new Validator [] {new CommandValidator(), new AnotherCommandValidator()});
}
public ModelAndView addCommand(HttpServletRequest request, HttpServletResponse response) throws Exception {
Command command = new Command();
// createBinder is a MultiActionController method
ServletRequestDataBinder binder = createBinder(request, command);
// Sets up any custom editor if necessary
binder.registerCustomEditor(...);
binder.bind(command);
return (!(binder.getErrors().hasErrors())) ? new ModelAndView("success") : new ModelAndView("failure");
}
// similar approach as shown above
public ModelAndView addAnotherCommand(HttpServletRequest request, HttpServletResponse response) throws Exception {
AnotherCommand anotherCommand = new AnotherCommand();
...
}
}
問候,