views:

87

answers:

1

I'm creating an struts2 application which has a login page and another user details page. There, i need to create only one action class. Because of that I'm unable to run the application by adding the field validations in both two pages to the action-validation.xml. Because with the login page validation it validate remaining field validations (field validations which belongs to user details page) in the validation.xml.

Therefore, please share your knowledge in struts2 to use one action class with action-validation.xml for two page(form) validations.

A: 

You can do it using wildcard mappings and associating your validation rules with your single action class:

Wildcard Mappings

In your struts.xml, define an action like so:

<action name="user-*" class="DoSomething" method="{1}" >
    <result name="input">/WEB-INF/{1}.jsp</result>
    <result name="success">/WEB-INF/{1}-success.jsp</result>
</action>

Then you can invoke the DoSomething action class with either of these two URLs:

http://yourapp.com/user-login.action
http://yourapp.com/user-details.action

The result will be that the part of the request after the hyphen ("login" or "details") will replace the {1} in your action definition. Therefore the first URL will invoke the DoSomething.login() method, go to the login.jsp on INPUT or the login-success.jsp on SUCCESS.

All you have to do is define login() and details() methods in DoSomething class.

Validation Rule Association

To associate validation rules with your DoSomething action class, simply name the .xml file containing them DoSomething-validation.xml. Since both user-login and user-details requests use that class, the rules will be run for both of them.

Pat
thanks Pat I'll check on this.
shalinda adikari