On 1/4/07, Edward Song <[EMAIL PROTECTED]> wrote:
Hi all,

STRUTS 1.2

Got a quick question on the Action "name" and "attribute" properties

name - The name of the form bean associated with this action.  This value
must be the name attribute from one of the form-bean elements.  This
attribute is optional and has no value.

attribute - The name of the request-or session-scope attribute under which
the form bean for this action can be accessed.  A value is allowed here only
if there is a form bean specified in the name attribute.  This attribute is
optional and has no default value.  If both this attribute and the name
attribute contain a value, this attribute will take precedence.

I have a generic form bean (SearchObjectForm), that I'd like to reuse
amongst many actions, along with its validation.

<form-bean name="SearchObjectForm"
type="org.apache.struts.validator.DynaValidatorForm">
    <!-- properties left out for email -->
</form-bean>

For the form bean above...I have these two actions defined.  (Defs are
simplified for illustration)

<action path="/admin/SearchUsers" type="com.SearchUsers" scope="session"
 attribute="SearchUsersForm" name="SearchObjectForm" validate="true"
input="/WEB-INF/jsps/searchUsers.jsp">
 <forward name="success" path="/WEB-INF/jsps/pages/admin/searchUsers.jsp"/>
</action>

<action path="/admin/SearchSubs" type="com.SearchSubs" scope="session"
 attribute="SearchSubsForm" name="SearchObjectForm" validate="true"
input="/WEB-INF/jsps/searchSubs.jsp">
 <forward name="success" path="/WEB-INF/jsps/pages/admin/searchSubs.jsp"/>
</action>

Now when I execute these actions, the validation that is called uses the
validation key "SearchUsersForm" or "SearchSubsForm", and upon not finding
those form beans, actually does not validate anything.

My expectations were to have it use the "name" property of the action to
access the form bean and necessary validation, and then use the attribute to
store the Form Bean within the request or session.  Thereby, reusing the
form bean defintion and validation and having the ability to access the form
with the attribute name, within the action.

However, as this is not the case, I set the validation key to the form name
and call validate in the action (Not what I want to do).  Is there another
way to reuse the form bean defintion and its validation, and have Struts
automatically store the form into the request or session using the attribute
field?

Any guidance, on how to more effectively approach this problem would be
appreciated  Thanks in advance.

One solution would be to create your own DynaValidatorForm
implementation and override the getValidationKey() method to use the
"name" rather than attribute. Personally I haven't actually ever used
the "attribute" configuration option and so name == attribute is
always true for me so whether there are other side effects I'm not
sure - but give it a go and see if it works for you:

public class MyDynaValidatorForm extends DynaValidatorForm {
      public String getValidationKey(ActionMapping mapping,
                                  HttpServletRequest request) {

       return mapping.getName();
   }
}

Another solution would be to upgrade to the latest Validator version
(1.3.1) - from Validator 1.2.0 onwards there is an "extends" attribute
where rules can inherit. That way you could re-use rules for one form
under a different name - which means you wouldn't have to duplicate
them in you validation.xml, so you could have something like the
following:

      <form name="SearchObjectForm">
           ...
       </form>

       <form name="SearchSubsForm" extends="SearchObjectForm">
           ...
       </form>

 http://jakarta.apache.org/commons/validator/
 http://wiki.apache.org/jakarta-commons/ValidatorVersion120

The only thing about upgrading to Validator 1.3.1 is that you would
need to use the new DTD and the arg0-arg3 elements have been removed
and would need replacing with <arg> elements. Otherwise Validator
1.3.1 is compatible with Struts 1.2.9

Niall

Ed

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

Reply via email to