Results 1 to 4 of 4

Thread: spring3.1 mvc validation

  1. #1
    Join Date
    Feb 2012
    Location
    xi'an China
    Posts
    4

    Default spring3.1 mvc validation

    I've just made a project with spring3.1 mvc.I want to use MvcConfig.java to instead of xml config,everything is working,but i override the WebMvcConfigurerAdapter's getValidator function like this:
    @Override
    public Validator getValidator() {
    LocalValidatorFactoryBean factory = new LocalValidatorFactoryBean();
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasename("/WEB-INF/messages/validation");
    if (environment.acceptsProfiles("embedded")) {
    messageSource.setCacheSeconds(0);
    }
    System.out.println("-=-=-=-===-=");
    factory.setValidationMessageSource(messageSource);
    return factory;
    }.
    Then, i use the '@Valid' and it works,but i don't know how config the Errors and show it to IE.
    anyone gives me a hand,thanks!
    Last edited by lyonyao; Feb 4th, 2012 at 03:29 AM.

  2. #2
    Join Date
    Feb 2012
    Posts
    9

    Default

    Would it be asking too much if you could attach your project? I would like to look through and see how you have set everything up?

  3. #3

    Default

    I'm not sure what "config the Errors and show it to IE" means. Can you clarify?

    If you are asking how to view validation errors in a page, look at the documentation for the <spring:errors> JSP tag here: http://static.springsource.org/sprin...glib-errorstag. This tag can be used to show all validation errors for a bean, or to show errors by field.

    The tag relies on an Errors object being added to the model. This is usually done automatically. A typical controller handler method might look like this:

    Code:
    @RequestMapping(value="/test", method=RequestMethod.POST)
    public String handleSubmit(@Valid SomeBean bean, BindingResult result, Model model) {
      if (result.hasErrors()) {
        model.addAttribute(bean);
        return "testView";
      }
    
      // ... controller processing
      return "redirect:/nextPage";
    }
    In this case, the "result" parameter contains the validation errors (BindingResult extends Errors) and is automatically added to the model by the framework. If the "testView.jsp" file uses the <spring:errors> tag, it will find the the BindingResult bean in the model.

  4. #4
    Join Date
    Feb 2012
    Location
    xi'an China
    Posts
    4

    Default

    i know it,thanks!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •