I don't know how to do this but a generic solution sounds desirable (and technically feasible if I was more familiar with the S2 architecture).

Are you asking for a struts2 solution to catch a business exception, put it in the errors collection and choose the 'input' result?

Presumably you would also want the exception message localized, so the exception would have to be a l10n key with a string in your ApplicationResources.properties.

And whatever handles these exceptions must be able to distinguish your business exceptions from the system exceptions.

It sounds like a job for an interceptor, so there may be something there already in S2. Have you looked through the list of interceptors [1] for anything likely? Post a msg if you find one - it sounds useful.

regards
Adam

[1] http://struts.apache.org/2.0.11/docs/struts-defaultxml.html

Jeremy JGR. Grumbach on 18/12/07 10:13, wrote:
Thanks also for the answer,

I'm using Velocity so no problem with the null values. And yes that's a way to manage exceptions, but I as said in my previous
post, I was looking for something more generic, without specific code in
all my actions.

I'm still searching for a more generic solution during next days, and if
I don't find it, I will of course use your solution or the one from Dave
:)

Jeremy

-----Original Message-----
From: Gary Affonso [mailto:[EMAIL PROTECTED] Sent: Tuesday, December 18, 2007 6:42 AM
To: Struts Users Mailing List
Subject: Re: Exception Handling keeping user input

To follow up on my previous post, here's some code from a "showForm" action that does exactly what I described...


        public void prepare() {
        if(session.get(SignUpFormBean.SESSION_KEY) == null) {
                // they're here for the first time.  do nothing (leave
the signUpFormBean model object null)
        } else {
                // they're returning because they've already failed a
submission
                log.debug("retrieving SignUpFormBean from session");
signUpFormBean = (SignUpFormBean) session.get(SignUpFormBean.SESSION_KEY);
                session.remove(SignUpFormBean.SESSION_KEY);
                
                // errors from previous failure have been retrieved via
the MessageStore interceptor
        }       
        }
        
        public SignUpFormBean getModel() {
                return signUpFormBean;
        }

     // Struts execute method
        // NOTE: the error messages from the validation failure are
available to this action (and the view) via the StoreMessages interceptor
     public String execute() throws Exception {
        // we always provide region and country lists to the view
allPostalAddressRegions = postalAddressRegionDao.getAllPostalAddressRegions(); allPostalAddressCountries = postalAddressCountryDao.getAllPostalAddressCountries();
        
         return Action.SUCCESS;
     }                  


----

One of the fields for the form view (freemarker in my case) looks something like this...

                <!-- first and last name -->
                 <tr>
<td><label for="postalAddress.firstName" class="karta-required-form-field">First Name</label></td>
                     <td><input type="text" id="postalAddress.firstName"

name="postalAddress.firstName" value="${(postalAddress.firstName) !}" size="30" maxlength="64"/></td>
                     <td>${(fieldErrors["postalAddress.firstName"])
!}</td>
                 </tr>

----

The idea being that the view will show data in the SignUpFormBean if that object contains it. If that object is null Freemarker shows nothing. Works great, you can use the exact same form code regardless of whether SignUpFormBean (in your case NewCar) has data or not.

You may need to play with how your own view layer (JSP? Velocity?) handles the display of data for objects (or fields) that are null (and they will be if this is the 1st time the user visits the form, they'll only have data if the form is getting shown after a validation problem).

Freemarker is a bit touchy about null objects and properties so you have

to some special notation which you see above. I think velocity is more forgiving. Not sure about JSP (I'm still actively trying to avoid it, mostly succeeding).

Hopefully that gets you going in the right direction.

- Gary


Jeremy JGR. Grumbach wrote:
Hi,

Let's take the following scenario: we have a database which manage car
models (for example Dogde Viper). The column "name" of the "car model"
table must be unique.
The user wants to add a "car model" in the database, thus he has an
"add
screen" containing all the fields of the "car model" and a "Save"
button. However he has entered a model which already exists in the
database. I check that in my business layer and return a runtime
exception "name already exists".
I can display it using global-exceptions, however it is forwarded to
another page and consequently, the user must reenter all the field
values one more time.
To avoid that, I want to go back to the "add screen" without losing
user
inputs but with a red message "name already exists in the database" -
exactly like the struts validation error messages.

Do you think it is possible with Struts 2? I have used that lots of
time
in Struts 1.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to