Results 1 to 9 of 9

Thread: How to display message about last action?

  1. #1
    Join Date
    Aug 2004
    Location
    Devon, UK
    Posts
    132

    Default How to display message about last action?

    Hi,

    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:

    Code:
    <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,

    John

  2. #2
    Join Date
    Nov 2004
    Location
    Hilversum - The Netherlands
    Posts
    1,054

    Default

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

  3. #3
    Join Date
    Aug 2004
    Location
    Devon, UK
    Posts
    132

    Default

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

    Code:
    <%@ 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. #4
    Join Date
    Aug 2004
    Location
    Devon, UK
    Posts
    132

    Default

    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:

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

  5. #5
    Join Date
    Aug 2004
    Location
    Devon, UK
    Posts
    132

    Default

    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?

    John

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

    Default

    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.
    --Jing Xue

  7. #7
    Join Date
    Aug 2004
    Location
    Devon, UK
    Posts
    132

    Default

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

    John

  8. #8
    Join Date
    Sep 2004
    Location
    Boston, US
    Posts
    130

    Default

    Appfuse ( http://raibledesigns.com/wiki/Wiki.jsp?page=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,
    Sanjiv

  9. #9

    Default

    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
    Code:
    public class UIManager
    
    public static final getSuccessMessages&#40;request&#41;
    &#123;
        List messages = &#40;List&#41;req.getAttribute&#40;ModelParameter.MESSAGES&#41;; 
        if &#40;messages == null&#41; 
        &#123;
            messages = new ArrayList&#40;&#41;;
            req.setAttribute&#40;ModelParameter.MESSAGES, messages&#41;;
        &#125;
        return &#40;messages&#41;;
    &#125;
    ....Method from a Typical AbstractFormController
    Code:
    ModelAndView onSubmit&#40;HttpServletRequest request, HttpServletResponse response, Object command,
                BindException errors&#41; throws Exception
    &#123;      
        try
        &#123;        
        		widgetbean.createWidget&#40;widgetVO&#41;;
            UIManager.getSuccessMessages&#40;request&#41;.add&#40;getMessageSourceAccessor&#40;&#41;.getMessage&#40;"success.1"&#41;&#41;;			
        &#125;
    		catch &#40;BusinessException be&#41;
        &#123;
           return this.showForm&#40;request, response, errors&#41;;
        &#125;        
    &#125;
    ...Calling Controller, always check for data in the ModelParameter.MESSAGES session attribute
    Code:
    myModel.put&#40;ModelParameter.MESSAGES, request.getAttribute&#40;ModelParameter.MESSAGES&#41;&#41;;
    ... JSPs always check and display data accordingly
    Code:
    <c&#58;forEach items="$&#123;model.messages&#125;" var="message">
      <h3 class="generalsuccess"><c&#58;out value="$&#123;message&#125;"/></font></h3>
    </c&#58;forEach>
    It would be nice if there was a clearly defined way of doing this

Similar Threads

  1. Replies: 7
    Last Post: Sep 29th, 2005, 05:51 AM
  2. 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
  3. Replies: 1
    Last Post: Jun 16th, 2005, 04:25 AM
  4. Channel and message transformation question
    By Alarmnummer in forum Architecture
    Replies: 12
    Last Post: May 11th, 2005, 05:06 PM
  5. Displaying Error from message resource
    By qtipz4ever in forum Web
    Replies: 3
    Last Post: Mar 16th, 2005, 04:48 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
  •