Announcement Announcement Module
Collapse
No announcement yet.
How do I want display a sucess message on previous question Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How do I want display a sucess message on previous question

    I have two virtual views mapped with urlmapper each has a controller.
    and a jsp.
    view.do
    edit.do
    ViewController
    EditController
    View.jsp
    Edit.jsp

    EditController subclasses SimpleFormController. Once they have successfully edited a dataitem I want them to be redirected to the View without the EditController having to know anything about viewing the data. So in my onSubmit() method I return return
    Code:
    new ModelAndView(new RedirectView("view.do"))
    this causes the ViewController to be called which will retrieve the data and render the view (because it gots back to the db the changes I made are shown).

    But on successful editing I want to display a message at the top of the view Page to show the user their edit was sucessful.

    So rather than redirecting to the view.do I think I want to forward on the viewController and pass it the message to be displayed in the httprequest ?, but I cant see how to specify the controller rather than the do object.

    Alternatively I could put the message into the HttpSession object for the user, and then the ViewController would always check this for messages before displaying.

    Please, which is the correct way of doing this.

  • #2
    Try return:
    Code:
    new ModelAndView("forward:view.do")

    Comment


    • #3
      Originally posted by manifoldronin
      Try return:
      Code:
      new ModelAndView("forward:view.do")
      Thanks, this works although doesnt seem to be mentioned anywhere in the documentation.

      In EditController I did
      Code:
       //Write Message to Users Message log for display
              ArrayList messages = new ArrayList();
              messages.add("new Item added sucessfully");
              
               //Forward Internally
              request.setAttribute(ModelParameter.MESSAGES,messages);
              return new ModelAndView("forward:view.do");
      then in the ViewController I did
      Code:
      if(request.getAttribute(ModelParameter.MESSAGES)!=null)
      {
            myModel.put(ModelParameter.MESSAGES,
                              request.getAttribute(ModelParameter.MESSAGES));
       }
      then in view.jsp i did
      Code:
       <c&#58;forEach items="$&#123;model.messages&#125;" var="message">
      		   <h3><font color="green"><c&#58;out value="$&#123;message&#125;"/></font></h3>
      		  </c&#58;forEach>
      Any comments ?

      Comment


      • #4
        Originally posted by Paul Taylor
        Thanks, this works although doesnt seem to be mentioned anywhere in the documentation.
        It's briefly mentioned in UrlBasedViewResolver javadoc, along with "redirect:".

        Originally posted by Paul Taylor
        Any comments ?
        Looks fine to me...

        Comment


        • #5
          is there any way to pass message with RedirectView() ?

          i can do someting like the following >>>
          Code:
          map.put&#40;"message", "My Message"&#41;;
          return new ModelAndView&#40;new RedirectView&#40;"view.html"&#41;, map&#41;;
          this results .../view.html?message=My+Message in the browser url.
          but i dont want extra parameters in my url.
          is there any other way i can send a success message

          Comment


          • #6
            I ended up replacing all my forwards with redirect because it caused problems with browser back button. In order to display messages I ended up using a HttpSession.

            The controller for page1 added data into the HttpSession.
            The controller for page2 checks the HttpSession, copies the messages into the model
            Removes the messages from the HttpSession so only shown once
            The jsp for page2 reads and displays the messages from the model

            Comment


            • #7
              Originally posted by mamun
              ... this results .../view.html?message=My+Message in the browser url.
              but i dont want extra parameters in my url.
              is there any other way i can send a success message
              Since we are doing a redirect to another URL the model gets put in the request via URL parameters in the RedirectView. Theres two ways to set parameters through a GET or a POST (and maybe whatever other methods i forgot ) and we can't POST with form fields... So we're stuck with the url parameters.

              The suspect code is at http://cvs.sourceforge.net/viewcvs.p...java?view=auto
              in the appendQueryProperties method.

              I hate putting objects in the session that have no business there, so I'd rather clutter my URL with parameters. Nonetheless, Paul's idea of removing the messages fromthe session and then putting them in the model of the next controller is nice so you don't have to remove the messages in the view.

              Comment


              • #8
                I ended up with a nice solution through using interceptor.
                what it actually does is, i save messages in session then redirect and print the message in jsp. the interceptor removes messages from session. that's it...

                My Interceptor
                Code:
                public class MessagesInterceptor extends HandlerInterceptorAdapter &#123;
                
                  public boolean preHandle&#40;HttpServletRequest request,
                			     HttpServletResponse response,
                			     Object handler&#41;
                	throws Exception &#123;
                	
                    if &#40;request.getSession&#40;&#41;.getAttribute&#40;ModelParameter.MESSAGES&#41; != null&#41; &#123;
                	    request.setAttribute&#40;ModelParameter.MESSAGES, request.getSession&#40;&#41;.getAttribute&#40;ModelParameter.MESSAGES&#41;&#41;;
                	    request.getSession&#40;&#41;.removeAttribute&#40;ModelParameter.MESSAGES&#41;;
                    &#125;
                	
                    return true;
                  &#125;    
                
                  public void postHandle&#40;HttpServletRequest request,
                			   HttpServletResponse response,
                			   Object handler,
                			   ModelAndView modelAndView&#41;
                	throws Exception &#123;
                	
                    if &#40;request.getSession&#40;&#41;.getAttribute&#40;ModelParameter.MESSAGES&#41; != null&#41; &#123;
                	    request.setAttribute&#40;ModelParameter.MESSAGES, request.getSession&#40;&#41;.getAttribute&#40;ModelParameter.MESSAGES&#41;&#41;;
                    &#125;
                
                  &#125;
                
                &#125;
                in sample-servlet.xml
                Code:
                  <bean id="defaultHandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
                    <property name="interceptors">
                      <list><ref bean="messagesInterceptor"/></list>
                    </property>
                  </bean>
                  <bean id="messagesInterceptor" class="...web.MessagesInterceptor" />

                Comment

                Working...
                X