struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jeremy JGR. Grumbach" <...@pulsar.be>
Subject RE: Exception Handling keeping user input
Date Tue, 18 Dec 2007 11:17:06 GMT
Yes that's what I'm looking for.
If this is possible, the way to manage a new Exception is very simple:
1) Create the new exception class (it must extends RuntimeException)
2) Add the associated message in the ApplicationResources.properties
3) Somewhere in the struts.xml, add a link between the new exception and
the message

And that's all!

My problem is that with the struts-2.0.dtd, we can just set the
exception, the name and the result as exception attributes. We cannot
set the key...

I will continue my investigation, and of course I will post a message if
I find something...

Jeremy


-----Original Message-----
From: Adam Hardy [mailto:ahardy.struts@cyberspaceroad.com] 
Sent: Tuesday, December 18, 2007 11:30 AM
To: Struts Users Mailing List
Subject: Re: Exception Handling keeping user input

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:glists2@greywether.com] 
> 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: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


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


Mime
View raw message