You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Gonçalo Luiz <go...@gmail.com> on 2007/02/01 23:34:32 UTC

ActionMessage/ActionError and redirect-action result

Hello,

Consider the following scenario:

I present a "create new user" dialog to a user. After the user hits
"submit" the user is created, and the login dialog is presented. I
want to display a "user sucessfully created, please proceed to login"
in the login page.
This message must be set from the Register action. I'm doing it as follows:

            List<String> args = new ArrayList<String>();
            args.add(this.username);
            this.addActionMessage(this.getText("userSucessfullyCreated", args));

However in after the redirect hte message seems to desappear... is
this by design? If so, how can I send a message that survives
redirects? If not, the same question stands.


Thank you very much.

Best Regards,
-- 
Gonçalo Luiz

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: ActionMessage/ActionError and redirect-action result

Posted by Gonçalo Luiz <go...@gmail.com>.
Here goes:

_________________________BEGIN______________________

/**
 *
 */
package eu.goncalo.solid.presentation;

import java.util.Collection;
import java.util.Map;

import org.apache.struts2.dispatcher.DefaultActionSupport;
import org.apache.struts2.interceptor.SessionAware;

import eu.goncalo.solid.service.user.IUserView;

/**
 * @author <a href="mailto:goncalo@goncalo.eu">Goncalo Luiz</a>
 *
 * <br>
 * <br>
 * Created at 2007/02/01, 22:46:36
 */
public class SolidActionSupport extends DefaultActionSupport
implements SessionAware
{

    private static final long serialVersionUID = 2812322910038417076L;

    private enum WEBKEYS
    {
        USER_VIEW, ACTION_MESSAGE, ACTION_ERROR;
    }

    private Map<WEBKEYS, Object> session;

    @SuppressWarnings("unchecked")
    public void setSession(Map session)
    {
        this.session = session;
    }

    protected void setUserView(IUserView userView)
    {
        this.session.put(WEBKEYS.USER_VIEW, userView);
    }

    protected IUserView getUserView()
    {
        return (IUserView) this.session.get(WEBKEYS.USER_VIEW);
    }

    protected void clearSession()
    {
        this.session.clear();
    }

    @Override
    public void addActionError(String anErrorMessage)
    {
        super.addActionError(anErrorMessage);
        this.session.put(WEBKEYS.ACTION_ERROR, super.getActionErrors());
    }

    @Override
    public void addActionMessage(String aMessage)
    {
        super.addActionMessage(aMessage);
        this.session.put(WEBKEYS.ACTION_MESSAGE, super.getActionMessages());
    }

    private void clearErrors()
    {
        this.session.remove(WEBKEYS.ACTION_ERROR);
    }

    private void clearMessages()
    {
        this.session.remove(WEBKEYS.ACTION_MESSAGE);
    }

    @Override
    public void clearErrorsAndMessages()
    {
        this.session.remove(WEBKEYS.ACTION_ERROR);
        this.session.remove(WEBKEYS.ACTION_MESSAGE);
        super.clearErrorsAndMessages();
    }

    @Override
    public Collection getActionErrors()
    {
        Collection errors =  (Collection)
this.session.get(WEBKEYS.ACTION_ERROR);
        this.clearErrors();
        return errors;
    }

    @Override
    public Collection getActionMessages()
    {
        Collection messages =  (Collection)
this.session.get(WEBKEYS.ACTION_MESSAGE);
        this.clearMessages();
        return messages;
    }
}

_________________________END______________________

On 01/02/07, Dave Newton <ne...@yahoo.com> wrote:
> --- Gonçalo Luiz <go...@gmail.com> wrote:
> > I think it does not make sense because many messages
> > that are presented are originated in the previous
> > interaction.
>
> Well, that's what session is for, I guess.
>
> > Just inherit your actions from this class and do
> > things normally... the messages will be
> automatically
> > persisted into session _until they are displayed_
> > (that is, until the _get_ method is invoked).
>
> The list doesn't accept attachments, but I'm not sure
> how this would work, unless it's just a getter that
> accesses something via a known key in a SessionAware
> action (like :flash in RoR).
>
> d.
>
>
>
>
> ____________________________________________________________________________________
> Do you Yahoo!?
> Everyone is raving about the all-new Yahoo! Mail beta.
> http://new.mail.yahoo.com
>


-- 
Gonçalo Luiz

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: ActionMessage/ActionError and redirect-action result

Posted by Dave Newton <ne...@yahoo.com>.
--- Gonçalo Luiz <go...@gmail.com> wrote:
> I think it does not make sense because many messages
> that are presented are originated in the previous
> interaction.

Well, that's what session is for, I guess.

> Just inherit your actions from this class and do
> things normally... the messages will be
automatically
> persisted into session _until they are displayed_ 
> (that is, until the _get_ method is invoked).

The list doesn't accept attachments, but I'm not sure
how this would work, unless it's just a getter that
accesses something via a known key in a SessionAware
action (like :flash in RoR).

d.



 
____________________________________________________________________________________
Do you Yahoo!?
Everyone is raving about the all-new Yahoo! Mail beta.
http://new.mail.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: ActionMessage/ActionError and redirect-action result

Posted by Gonçalo Luiz <go...@gmail.com>.
I think it does not make sense because many messages that are
presented are originated in the previous interaction.

I consider the following class I just developed an elegant workaround.
Just inherit your actions from this class and do things normally...
the messages will be automatically persisted into session _until they
are displayed_ (that is, until the _get_ method is invoked).

If you have any comments or suggestion please feel free.


On 01/02/07, Dave Newton <ne...@yahoo.com> wrote:
> --- Gonçalo Luiz <go...@gmail.com> wrote:
> > I want to display a "user sucessfully created,
> please
> > proceed to login" [...] However in after the
> redirect
> > hte message seems to desappear...
>
> Um... if you're redirecting to a different action
> wouldn't it make sense that a message set in the
> current action would go away?
>
> If you want to display a message in an action you're
> redirecting to then I'd imagine you'd have to put it
> in session or a parameter or something.
>
> There are probably more elegant solutions; I'm just
> learning S2 myself.
>
> d.
>
>
>
>
> ____________________________________________________________________________________
> Don't pick lemons.
> See all the new 2007 cars at Yahoo! Autos.
> http://autos.yahoo.com/new_cars.html
>


-- 
Gonçalo Luiz


Re: ActionMessage/ActionError and redirect-action result

Posted by Dave Newton <ne...@yahoo.com>.
--- Gonçalo Luiz <go...@gmail.com> wrote:
> I want to display a "user sucessfully created,
please
> proceed to login" [...] However in after the
redirect
> hte message seems to desappear... 

Um... if you're redirecting to a different action
wouldn't it make sense that a message set in the
current action would go away?

If you want to display a message in an action you're
redirecting to then I'd imagine you'd have to put it
in session or a parameter or something.

There are probably more elegant solutions; I'm just
learning S2 myself.

d.



 
____________________________________________________________________________________
Don't pick lemons.
See all the new 2007 cars at Yahoo! Autos.
http://autos.yahoo.com/new_cars.html 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org