首页 > 其他 > 详细

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

时间:2020-01-16 13:30:20      阅读:63      评论:0      收藏:0      [点我收藏+]

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)访问方式:

技术分享图片

技术分享图片

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

原文:https://www.cnblogs.com/zhai1997/p/12200501.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!