zoukankan      html  css  js  c++  java
  • 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)访问方式:

  • 相关阅读:
    2018-04-13Java编程夯实学习心得(3)
    2018-03-28JavaScript学习心得
    2018-03-27mysql学习心得
    JavaScript-作用域
    样式切换图
    购物车结算
    Visual Studio Code快捷键操作
    复选框
    win10锁屏界面无法设置隐藏
    轮播图
  • 原文地址:https://www.cnblogs.com/zhai1997/p/12200501.html
Copyright © 2011-2022 走看看