Announcement Announcement Module
Collapse
No announcement yet.
Spring 3.x: displaying errors and messages Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring 3.x: displaying errors and messages

    As I moved from 2.x to 3.x I am having trouble with something as basic as displaying errors and messages. I am not talking about normal validation, I am talking about errors and success messages that happen during actual processing.

    I encounter two problems:
    1. Even though I set the error in the result, it doesn't display. I suspect that my submit method needs to return something other than a redirect, probably something that includes the stuffCmd and result objects. It would be particularly nice if subsequent displays of the edit form retained the values previously selected without having to manually set them in stuffEdit() every time. How do I assemble that?
    2. I would like to post a "submit successful" message when everything goes OK. Not sure how to go about it since it is not an error. Also note that the message should ONLY be displayed on successful return from the btn1Submit() method, not every time that the form is displayed.


    I have a JSP with the usual:
    Code:
     <form:errors path="*" cssClass="errors"></form:errors>
    My controller looks like:
    Code:
      @RequestMapping("/stuff/edit.htm")
      public ModelAndView edit(
          @ModelAttribute("stuffCmd") StuffCmd stuffCmd, BindingResult result,
          @RequestParam(value="parm1",     required=false) String parm1,
          @RequestParam(value="parm2",     required=false) String parm2{
        ...
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("stuffCmd",  stuffCmd);
        ...
        return new ModelAndView("stuffEdit", dataMap);
      }
    
      @RequestMapping("/stuff/submit.htm", params="button1")
      public String btn1Submit(@ModelAttribute("stuffCmd") StuffCmd stuffCmd, BindingResult result) {
        try{
          myService.dbSubmit(stuffCmd.getValue());
        }catch(Throwable t){
          result.addError(new ObjectError("stuff", "Error submitting to the database"));
        }
        return "redirect:/stuff/edit.htm?parm1=val1&parm2=val2";
      }

  • #2
    A redirect destroys the model as it basically is a new request after that. I suggest you take a look at the RedirectAttributes object and the explanation in the reference guide. (also this isn't something that changed that was already the case with Spring 1.x).

    If it is wizardstyle/flow style behavior you want to take a look ath web flow.

    Comment


    • #3
      As I stated on my original post I suspected that the redirect was wrong.

      I looked at the documentation again and I see nothing that tells me what to return from a process method (submit()) so that the display method (edit() ) receives the model.

      Unfortunately the Spring documentation is the usual worthless collection of snippets that can only be productively used by someone who already knows everything about the current version of Spring. Worse yet, the only decent tutorial is in the Spring Recipes book, and that does not cover the 3.0/3.1 techniques.

      Does somebody out there have a usable answer?
      Last edited by bmelloni; Mar 27th, 2012, 12:41 PM.

      Comment


      • #4
        Does somebody out there have a usable answer?
        It was in my answer..

        Originally posted by Marten Deinum
        I suggest you take a look at the RedirectAttributes object and the explanation in the reference guide.

        Comment


        • #5
          Originally posted by Marten Deinum View Post
          I suggest you take a look at the RedirectAttributes object and the explanation in the reference guide.
          Ctrl-F for "RedirectAttributes" fails to find a single match in the reference guide: http://static.springsource.org/sprin.../html/mvc.html
          Last edited by bmelloni; Mar 27th, 2012, 01:23 PM.

          Comment


          • #6
            I assumed you used 3.1 not 3.0...

            For 3.0 it indeed doesn't exists... You need to store it in the session yourself and retrieve it after the redirect.

            Comment

            Working...
            X