Struts2配置文件——动态方法调用

1、准备工作:

(1)在一个action中写多个方法:

public class HelloAction {
    public String add(){
        System.out.println("添加");
        return "success";
    }

    public String delete(){
        System.out.println("删除");
        return "success";
    }

    public String update(){
        System.out.println("修改");
        return "success";
    }

    public String selete(){
        System.out.println("查询");
        return "success";
    }
}

(2)为了配置文件的简洁明了,设置两个配置文件,主文件引入另一个配置文件:

<struts>
    <include file="zhb/hello/struts.xml"></include>
</struts>

另外一个配置文件:

<struts>
    <package name="hello" namespace="/hello" extends="struts-default">
        <action name="HelloAction_add" class="pers.zhb.hello.HelloAction" method="add">
            <result name="success">/hello.jsp</result>
        </action>

        <action name="HelloAction_delete" class="pers.zhb.hello.HelloAction" method="delete">
            <result name="success">/hello.jsp</result>
        </action>

        <action name="HelloAction_update" class="pers.zhb.hello.HelloAction" method="update">
            <result name="success">/hello.jsp</result>
        </action>

        <action name="HelloAction_selete" class="pers.zhb.hello.HelloAction" method="selete">
            <result name="success">/hello.jsp</result>
        </action>
    </package>
</struts>

这样书写起来比较麻烦。

(3)测试结果:

 

 2、动态方法调用(方式一):

(1)配置动态方法调用是否开启常量(默认是关闭的):

<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
    <package name="hello" namespace="/hello" extends="struts-default">
        <action name="HelloAction" class="pers.zhb.hello.HelloAction">
            <result name="success">/hello.jsp</result>
        </action>
    </package>
</struts>

(2)访问方式:

 

 3、动态方法调用(方式二):

(1)配置文件:

<struts>
    <package name="hello" namespace="/hello" extends="struts-default">
        <action name="HelloAction_*" class="pers.zhb.hello.HelloAction" method="{1}">
            <result name="success">/hello.jsp</result>
        </action>
    </package>
</struts>
method="{1}"中的“1”可以取出“*”中的内容。
(2)访问方式:

猜你喜欢

转载自www.cnblogs.com/zhai1997/p/12200501.html
今日推荐