Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a way to use more than 1 Validator with a Spring 3 annotated Controller?
    primarykey
    data
    text
    <p>I have a Spring 2.x controller that extends <code>SimpleFormController</code>, which is deprecated as of Spring 3 in favor of annotated controllers. So I'm trying to convert it to use <code>@Controller</code>, with <code>@InitBinder</code> and <code>@Valid</code> for form validation. However, I can't find a way to use multiple validators with a Spring 3.x controller. How do I do this?</p> <p>Here is what my controller's bean def currently looks like:</p> <pre class="lang-xml prettyprint-override"><code>&lt;bean name="/s/account" class="mywork.AccountSettingsController" p:formView="forms/account" p:successView="redirect:/app/s/account" p:commandName="accountSettingsForm"&gt; &lt;property name="validators"&gt; &lt;list&gt; &lt;ref bean="emailFormatValidator" /&gt; &lt;ref bean="uniqueEmailValidator" /&gt; &lt;ref bean="changeEmailValidator" /&gt; &lt;ref bean="passwordWithConfirmationValidator" /&gt; &lt;ref bean="changePasswordValidator" /&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre> <p>It's the controller for a page which lets the user change their email address and password. The validator beans are legacy code, but I'm guessing they were split into separate classes for better reusability.</p> <p>I'm trying to move all of that into the controller class itself, using annotations:</p> <pre class="lang-java prettyprint-override"><code>@Controller @Secured({BaseController.ROLE_LOGGED_IN}) @RequestMapping("/s/account") public class AccountSettingsController extends BaseController { private static final String FORM_URL = "/forms/account"; private static final String FORM_NAME = "accountSettingsForm"; @InitBinder(FORM_NAME) public void initBinder(WebDataBinder binder) { // TODO: how to inject &gt; 1 validator for the form? binder.setValidator(...); } @RequestMapping(method = RequestMethod.GET) public ModelAndView get() { ChangePasswordOrEmailForm form = new ChangePasswordOrEmailForm(); ... return new ModelAndView(FORM_URL, FORM_NAME, form); } ... } </code></pre> <p>As far as I can tell, Spring 3 assumes a 1-to-1 relationship between: Controller-Form-WebDataBinder-Validator. I could create a composite validator that aggregates the 5 individual validator beans, and delegates the <code>Validator#supports()</code> and <code>Validator#validate()</code> calls to them, but is that really the best solution?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload