Announcement Announcement Module
No announcement yet.
How to display message about last action? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to display message about last action?


    I am building a dating agency (another!) where users can send messages, add contacts to a list, delete contacts, and so on.

    I have a few pages running just fine, and a dozen or so controllers so far.

    What I would like to add is a message about the last action to every page - for example, when a user sends a message to another user and is returned to the page he was looking at before he wrote the message, I would like a message displayed on the page saying "Your message to whoever has been sent". Or when he adds a contact, there is a display of "A contact has been added to your contact list".

    Two questions:

    How can I best implement this? I was thinking of using a super class for all my command objects, which has just an actionNotification property. But then how could I access this from a JSP page? I can't refer to the command objects superclass on the page:

    <c&#58;out value="$&#123;actionNotifier.lastAction&#125;"/>
    // who to send message to
    <c&#58;out value="$&#123;messageWriteInfo.toUserName&#125;"/>
    Any other ideas? I guess this is quite a common requirement.

    Also, I would like to display this info up near the masthead, which in all my pages is just an included jsp. Can I access command objects from this included page?

    Thanks for any suggestions,


  • #2
    Can`t you add a description of the last action to the session?


    • #3
      I tried that and it doesn't work, since, following Rod's advice, all my JSP's contain the line:

      <%@ page session="false"%>
      and sessions are dealt with exclusively in the controllers.

      java.lang.IllegalStateException: Cannot access session scope in page that does not participate in any session at....


      • #4
        I think I have to go with this:

        Create a singleton class called ActionNotifier, and give all Controllers access to it with dependency injection, and post messages about the last actions to that. ActionNotifier just has a HashMap in it, with sychronized accessor methods. ActionNotifier.getLastAction(userName) should remove the last action from the HashMap

        I should be able to access ActionNotifier something like this:

        <c&#58;out value="$&#123;someController.actionNotifier.lastActionMessage&#125;"/>
        I'll try that out - but I may be overlooking something obvious...


        • #5
          I am still struggling with this one!

          The solution I mentioned about using a ActionNotifier doesn't work. Getting hold of the last action info needs a userName as a parameter to get hold of the right action info from the Map inside it. I can't see how to get hold of the right action info without using a script in the page, which I want to avoid.

          I have also considered making a superclass of my model objects and adding info about the last action there. However, I can't know which page I might take the user to next after they have sent a message (there are several pages that a user can hit a link to send a message, and after they have written the message, I return them to the same page) so I don't know how I can include info about the last action except as request parameters which would look rather clunky.

          Perhaps I am in the wrong part of the forum with this question - I think passing info about the last action the user took from one page to the next (without necessarily knowing in advance which page might be the next one) must be a common requirement, and someone must have implemented it within the Spring community. No?



          • #6
            Can you just put it in the model? Most likely when you finish processing the submission from the first page, you'd just forward to the next page controller.


            • #7
              Thanks for the suggestion. That really is all I have to do, add it to the model. I have just created a superclass for all my command objects which handles the actionMessages. No problem there really!

              Still get parameters showing up in the URL though (only sometimes!), even though all my forms use "POST".



              • #8
                Appfuse ( ) does this quite elegantly. Basically you can add error / success messages from your controller in the session scope under a known error/success key.

                The web app pages have Sitemesh decorator which checks for values in the error / success session key and if it finds it then it displays the message and removes the value from the session scope.

                Adding error/success message to the session scope allows the message to be displayed even across redirects (ie if your controller uses a RedirectView).

                Take a look at the live demo of Appfuse to get a feel for how this looks.

                Hope this helps,


                • #9
                  In our application we a cannot use SessionScope excet for exceptional circumstances so I have added the code as follows. The Controllers that typically ( but not neccesarily)generate the messages are FormControllers
                  public class UIManager
                  public static final getSuccessMessages&#40;request&#41;
                      List messages = &#40;List&#41;req.getAttribute&#40;ModelParameter.MESSAGES&#41;; 
                      if &#40;messages == null&#41; 
                          messages = new ArrayList&#40;&#41;;
                          req.setAttribute&#40;ModelParameter.MESSAGES, messages&#41;;
                      return &#40;messages&#41;;
                  ....Method from a Typical AbstractFormController
                  ModelAndView onSubmit&#40;HttpServletRequest request, HttpServletResponse response, Object command,
                              BindException errors&#41; throws Exception
                  		catch &#40;BusinessException be&#41;
                         return this.showForm&#40;request, response, errors&#41;;
                  ...Calling Controller, always check for data in the ModelParameter.MESSAGES session attribute
                  myModel.put&#40;ModelParameter.MESSAGES, request.getAttribute&#40;ModelParameter.MESSAGES&#41;&#41;;
                  ... JSPs always check and display data accordingly
                  <c&#58;forEach items="$&#123;model.messages&#125;" var="message">
                    <h3 class="generalsuccess"><c&#58;out value="$&#123;message&#125;"/></font></h3>
                  It would be nice if there was a clearly defined way of doing this