struts action的动态创建

来源:互联网 发布:哈佛大学学术地位知乎 编辑:程序博客网 时间:2024/05/29 14:11

    一个类可以不止一种方法。一个action对应的是一个方法。当一个类中有多个方法时,可以有三种方式指定想要执行的action:

    下面是一个简单的示例:

    struts2 版本 : 2.5.8

    Action类 :

package com.cd.action;public class HelloWordAction { public String helloWord(){System.out.println("hello word !!!!");return "hello";}public String goodByeWord(){System.out.println("good bye word !!!!");return "bye";}}


    方法一: 使用method属性 :


    <package name="default" namespace="/test" extends="struts-default">        <action name="action_hello" class="com.cd.action.HelloWordAction" method="helloWord">   <result name="hello">/success.jsp</result>        </action>        <action name="action_bye" class="com.cd.action.HelloWordAction" method="goodByeWord">            <result name="bye">/goodbye.jsp</result>        </action></package>
    这样访问以action_hello.action结尾的url就会执行helloWord方法。访问action_bye.action结尾的url就会执行goodByeWord方法。


    方法二:在url中使用!号


    这里有两点不同的地方,一个是设置常量DynamicMethodInvocation,另外一个就是在action里添加允许使用!访问的方法:

<struts>    <!--设置允许动态调用-->    <constant name="struts.enable.DynamicMethodInvocation" value="true"/>    <package name="default" namespace="/test" extends="struts-default">        <action name="action" class="com.cd.action.HelloWordAction">   <result name="hello">/success.jsp</result>            <result name="bye">/goodbye.jsp</result>            <!--允许动态调用的方法-->            <allowed-methods>helloWord,goodByeWord</allowed-methods>        </action></package></struts>
访问的结果如下:



        方法三 : 使用通配符的方法(推荐) :


       这里需要配置一项允许通配符

<struts>        <package name="default" namespace="/test" extends="struts-default">                <!--设置允许使用通配符-->        <global-allowed-methods>regex:.*</global-allowed-methods>        <action name="action_*" class="com.cd.action.HelloWordAction" method="{1}">   <result name="hello">/success.jsp</result>            <result name="bye">/goodbye.jsp</result>        </action>        </package></struts>
在这里{1}表示的是第一个' * ' 号,当然也可以有多个 ' * ' 号。访问结果 :


    * 在url中的是helloWord,执行的方法也是helloWord,{1}这种用法不仅仅可以用在method里,class属性和返回值结果里,举个例子:

    在根目录创建一个名为helloWord的jsp页面:(和方法同名)

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>hello</title></head><body> this is helloWord jsp page .</body></html> 
    再创建一个名为HelloWordAction的jsp页面:(和类同名)

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>s</title></head><body>   this is hello word action page !</body></html>

    配置struts.xml :

<struts>    <package name="default" namespace="/test" extends="struts-default">        <!--设置允许使用通配符-->        <global-allowed-methods>regex:.*</global-allowed-methods>        <action name="*_*" class="com.cd.action.{1}" method="{2}">   <result name="hello">/{2}.jsp</result>            <result name="bye">/{1}.jsp</result>        </action></package></struts>

    访问测试 :

    

    可以看见访问的是helloWord方法,返回的是 'hello',但是对应的界面是helloWord.jsp。

  

    访问的是goodByeWord方法,返回的是 'bye',但是返回的界面是HelloWordAction.jsp。

    多使用通配符的话,可以减少action的书写。


0 0