Results 1 to 5 of 5

Thread: Error messages on top of page

  1. #1
    Join Date
    May 2011
    Posts
    3

    Default Error messages on top of page

    Hello,

    I'd like to display all my error messages on top of my pages rather than under each field.

    If I read the documentation correctly, I can do the following:

    Code:
    @RequestMapping(value="/customer", method= RequestMethod.POST)
    public String show(@ModelAttribute Customer customer, BindingResult result)
    and in my JSP:
    Code:
    <form:form modelAttribute="customer" ...>
        <form:errors path="*"/>
    </form:form>
    But then, I can't reuse the above code snippet because the modelAttribute varies for each page (customer, order, ...).

    Is there a (SpringMVC) way to stay DRY in this case?

    Bonus noob question: how do I display informative (=non error) messages?

    Thanks a bunch!

  2. #2
    Join Date
    Mar 2011
    Location
    Washington, DC
    Posts
    60

    Default

    As your requirement, you have to implement Validator class which validate each of your ModelAttribute class (one Validator for one ModelAttribute unless you are group them together into one bean).. Does that make sense?

  3. #3
    Join Date
    May 2011
    Posts
    3

    Default

    Hi,

    I'm not sure I see the point here... I'm not using any validator but instead I do something like:

    Code:
    result.rejectValue("uid", ERR_MISSING_UID);
    How would a Validator eventually help me in my JSP?

  4. #4
    Join Date
    Mar 2011
    Location
    Washington, DC
    Posts
    60

    Default

    I agree that you can validate something like above, but it will limit only what modelAttribute you need to pass.
    If you implement Validator, you can do something like this:
    Code:
     
    	customVAlidator.validate(yrmodelAttribute object, result);
    		if (result.hasErrors()){
    			logger.debug("has some error");
    			 modelMap.addAttribute("yrview", yrmodelAttribute object);
    			return "yrview";
    		} else{
    			status.setComplete();
    			logger.debug("Redirect to success form");
    			return "redirect:something";
    		}
    This will make yr code cleaner and easy to change validation logic. Also, it will group all of the errors into BindingResult Object.

    I don't know if this will help you. Anyone have other suggestion??? Thanks

  5. #5
    Join Date
    May 2011
    Posts
    3

    Default

    It's not really a matter of modular or clean code. The point is to find a DRY way to display all error and info messages on top of the page.

    In your example, you tight your validator to a view and to a flow, making it non re-usable (you won't be able to perform the same validation if you're connecting to another view). And anyway it doesn't solve my problem...

Posting Permissions

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