在 Struts2 中的一个动作类本身中创建多个方法? [英] Create multiple methods in one action class itself in Struts2?

查看:29
本文介绍了在 Struts2 中的一个动作类本身中创建多个方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在同一个动作类中创建两个方法吗?如果是这样,我们如何在 struts.xml 文件中指定它?

Can I create two methods in the same Action Class? If so how can we specify it in the struts.xml file ?

例如:我创建了一个简单的验证操作类来验证 email address 以及 password 使用两个单独的正则表达式.我在 Action 类中创建了两个方法:emailVerification()passVerification().我在里面编写了所有必要的验证代码,但是现在当它们返回 SUCCESS 时,它们应该会产生相同的成功页面结果,对于 ERROR 也是如此..

For example : I created a simple validation action class to validate the email address as well as password using two separate regular expression. I created two Methods in the Action class say: emailVerification() and passVerification(). I wrote all the necessary validation code inside, but now when they return SUCCESS they should result into the same success page result and for ERROR likewise..

推荐答案

是的,您可以在 Action Class 中创建任意数量的方法.你可以这样做

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> 

这篇关于在 Struts2 中的一个动作类本身中创建多个方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆