Results 1 to 8 of 8

Thread: How do I want display a sucess message on previous question

  1. #1

    Default 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. #2
    Join Date
    Oct 2004
    Location
    Herndon, VA, US
    Posts
    648

    Default

    Try return:
    Code:
    new ModelAndView("forward:view.do")
    --Jing Xue

  3. #3

    Default

    Quote 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 ?

  4. #4
    Join Date
    Oct 2004
    Location
    Herndon, VA, US
    Posts
    648

    Default

    Quote 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:".

    Quote Originally Posted by Paul Taylor
    Any comments ?
    Looks fine to me...
    --Jing Xue

  5. #5
    Join Date
    Sep 2004
    Location
    Tokyo, Japan
    Posts
    22

    Default

    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
    MaMuN

  6. #6

    Default

    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

  7. #7
    Join Date
    Jul 2005
    Posts
    28

    Default

    Quote 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.

  8. #8
    Join Date
    Sep 2004
    Location
    Tokyo, Japan
    Posts
    22

    Default

    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" />
    MaMuN

Similar Threads

  1. UpgradeAcegi Security System from 0.6.1 to 0.8.3
    By mannobug in forum Security
    Replies: 3
    Last Post: Sep 23rd, 2005, 07:00 PM
  2. Message Question
    By phasews in forum Web
    Replies: 3
    Last Post: Sep 14th, 2005, 07:54 AM
  3. How to display message about last action?
    By pir8ped in forum Architecture
    Replies: 8
    Last Post: Jun 6th, 2005, 11:01 AM
  4. Replies: 3
    Last Post: May 16th, 2005, 11:14 PM
  5. Channel and message transformation question
    By Alarmnummer in forum Architecture
    Replies: 12
    Last Post: May 11th, 2005, 05:06 PM

Posting Permissions

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