簡體   English   中英

在MultiActionController ModelAndView方法中調用valang驗證器......可能嗎? 怎么樣?

[英]call valang validator in MultiActionController ModelAndView method… possible? how?

這可能嗎? 我基本上使用MultiActionController類制作一個Spring CRUD Web應用程序,並希望我的表單得到驗證。 我之前使用過SimpleUrlController,valang完美運行。

根據MultiActionControlller API

考慮在控制器方法中直接使用ServletRequestDataBinder,INSTEAD OF依賴於聲明的命令參數。 這允許完全控制整個活頁夾的設置和使用,包括驗證者的調用以及綁定/驗證錯誤的后續評估。

所以你可以使用

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();

        ...
    }
}

問候,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM