struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gary Affonso <>
Subject Re: Exception Handling keeping user input
Date Tue, 18 Dec 2007 05:41:45 GMT
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) 
     		// 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 = 
     	allPostalAddressCountries = 
         return Action.SUCCESS;


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

             	<!-- first and last name -->
                     <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>


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.
> Thanks in advance,
> Jeremy 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message