Announcement Announcement Module
Collapse
No announcement yet.
spring3.1 mvc validation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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, 03:29 AM.

  • #2
    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?

    Comment


    • #3
      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.

      Comment


      • #4
        i know it,thanks!

        Comment

        Working...
        X