Create multiple methods in one action class itself in Struts2?

北城以北 提交于 2019-12-01 14:17:15
Uchenna Nwanyanwu

Yes you can create any number of methods in an Action Class. You can do something like this

package com.myvalidation;

public class MyValidationClass extends ActionSupport
{
     public String emailVerification() throws Exception
     {
         //Your validation logic for email validation
         return SUCCESS;
     }

     public String passVerification() throws Exception
     {
         //Your validation logic for password validation
         return SUCCESS;
     }
}

struts.xml

<action name="emailVerification" method="emailVerification" class="com.myvalidation.MyValidationClass">
        <result name="success">/your_success_jsp.jsp</result>
        <result name="input">/your_error_jsp.jsp</result>
</action> 

<action name="passVerification" method="passVerification" class="com.myvalidation.MyValidationClass">
    <result name="success">/your_success_jsp.jsp</result>
    <result name="input">/your_error_jsp.jsp</result>
</action> 

Using the folowing URL format you can call any public method from Struts action class:

/ActionName!publicMethodName.action?p1=v1&p2=v2

For more information refer to: Action Configuration

Rather than code a separate mapping for each action class that uses this pattern, you can write it(method="{1}") once as a wildcard mapping.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!