Struts2 访问Action中的指定方法 三种方式的总结

来源:互联网 发布:知呼和分答 编辑:程序博客网 时间:2024/06/01 12:19

Struts2 中访问Action时,默认是执行Action的 execute 方法. 

但是我们在实际运用中往往会遇到一个Action中有多个方法,不同场景下发出的请求要去调用不同的执行方法的情况

Struts2中提供了三种方式,供使用者来自定义Action所要执行的方法


1.方式一:通过配置Action的method属性,每一个方法配置为一个单独的Action,要调用不同的方法,去请求不同的Action即可

 TestAction code:

public class TestAction extends ActionSupport {    public String test1() throws Exception {        System.out.println("执行了test1方法");        return SUCCESS;    }    public String test2() throws Exception {        System.out.println("进入了test2方法");        return SUCCESS;    }}

struts.xml 中配置:

<struts>    <constant name="struts.devMode" value="true" />    <!-- 定义包 -->    <package name="default" namespace="/" extends="struts-default">        <action name="testAction1" class="com.sy.actions.TestAction" method="test1">            <result>/success.jsp</result>        </action>        <action name="testAction2" class="com.sy.actions.TestAction" method="test2">            <result>/success.jsp</result>        </action>    </package></struts> 

TestAction中的两个方法,分别配置了两个Action,要访问哪个方法请求对应的Action即可.

但是这种方式非常不友好,如果方法过多的话,造成配置文件过于臃肿,得不偿失.


方式二:启用Struts2 动态方法调用

方式二最大的好处在于只需要设置一个常量值,Action的配置跟以往一样即可,并不需要每种方法对应一个Action

 TestAction code:

public class TestAction extends ActionSupport {    public String test1() throws Exception {        System.out.println("执行了test1方法");        return SUCCESS;    }    public String test2() throws Exception {        System.out.println("进入了test2方法");        return SUCCESS;    }}
 struts.xml 中配置:

<struts>    <constant name="struts.devMode" value="true" />    <!--启用动态方法调用-->    <constant name="struts.enable.DynamicMethodInvocation" value="true" />    <package name="default" namespace="/" extends="struts-default">        <action name="testAction" class="com.sy.actions.TestAction">            <result>/success.jsp</result>        </action>    </package></struts>

那么在请求Action时  使用  Action名!方法名.action 这样的方式便可实现对不同方法的访问.

eg:  testAction!test1.action

       testAction!test2.action

<

如果是2.5或更高版本,请在配置文件的包元素中开启通配符的使用 

<global-allowed-methods>regex:.*</global-allowed-methods>

>

方式三: 通过使用通配符来实现对不同方法的访问

TestAction code:

public class TestAction extends ActionSupport {    public String test1() throws Exception {        System.out.println("执行了test1方法");        return SUCCESS;    }    public String test2() throws Exception {        System.out.println("进入了test2方法");        return SUCCESS;    }}

struts.xml 中配置:

<struts>    <constant name="struts.devMode" value="true" />           <!-- 定义包 -->           <package name="default" namespace="/" extends="struts-default">               <action name="testAction_*" class="com.sy.actions.TestAction" method="{1}">                   <result>/success.jsp</result>               </action>           </package></struts>

在请求Action时,样式请与配置的action 的 name属性保持一致.例如:我这里配置的为 testAction_* ,那么 ,在请求时 testAction_test1.action,testAction_test2.action

便可分别实现对test1,test2方法的访问.


<

如果是2.5或更高版本,请在配置文件的包元素中开启通配符的使用 

<global-allowed-methods>regex:.*</global-allowed-methods>

>


阅读全文
0 0
原创粉丝点击