struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject Struts2 Validation w/ModelDriven
Date Thu, 19 May 2011 16:49:43 GMT
When I implement the ModelDriven interface in my Struts2 actions; 
I follow your typical prepare() method as follows:

public void prepare()
throws Exception {
  if(id != null) {
    model = service.create(/* some parameters */);
  } else {
    model = service.lookupById(id);

When a user first displays lets say the data-entry form; the create()
method will query the system for data based on the arguments passed to
the create() action call; pre-populate the model accordingly and then
display the data entry form for the user.  When the user hits the 'save'
button to call the save() method; again the prepare() method is invoked,
the model is re-initialized; the interceptor stack populates the model
with the parameters from the form.

If a field is defined as a Long and a user happened to enter "xyz" as
the value in the form; then the default stack will add a field error for
that field to the error maps because xyz cannot be converted to a Long;
however, because validation happens after prepare(); the value in this
field has be reset back to its initial value.  So when this error
condition is met and the user redirected back to the INPUT form; the
field where they had entered "xyz" is now the original default
initialized value.

I am assuming there is no clean way to prevent this because ultimately
you cannot store "xyz" in a field meant to hold a Long value for when
the form is redrawn unless all fields are treated as Strings in the
action and then copied/mapped to the domain object after validation

How are others coping with this?  Do you just allow the field to either
default back to null or restore its state back to the original
initialized value when a user enters data into a field that triggers a
conversion problem?

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

View raw message