Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Struts 2 »

Action name vs. attribute

Edward Song

2007-01-04

Replies:

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.


Ed

©2008 junlu.com - Jax Systems, LLC, U.S.A.