You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Eric Rank <er...@lo-fi.net> on 2007/02/16 18:52:22 UTC

[s2] Adding ActionErrors / Messages with a named key

Is there a way to associate an ActionError or ActionMessage with a  
named key in Struts 2?

In struts 1 we can add ActionErrors / Messages with a named key. The  
lines of code below add a new error with the key "login" to the action:

ActionMessages errors = new ActionMessages();
errors.add("login", new ActionMessage("errors.invalidLogin"));
saveErrors(request, errors);

The nice thing about doing things this way is that I can pull out  
that single action error if I want, without worrying about the other  
errors I might have:

<html:messages id="error" property="login">
      <p class="error">${error}</p>
</html:messages>

In Struts2 it seems that I'm stuck with all or nothing. ValidateAware  
only takes a single String as an argument for the addActionError  
method. The actionerror tag just spits out a list of errors, forcing  
me to show all of them.

What is the best way, in struts 2, to associate a non-field error  
with a key?

I can see 2 different ways:

1. Just use the addFieldError, even though there is no field for the  
error I want to name. (I don't like this approach because it uses the  
field errors for something which they are not intended)
2. create my own map of error messages


Thanks for your advice,

Eric Rank

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