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

  • 相关阅读:
    hdu 5119 Happy Matt Friends
    hdu 5128 The E-pang Palace
    hdu 5131 Song Jiang's rank list
    hdu 5135 Little Zu Chongzhi's Triangles
    hdu 5137 How Many Maos Does the Guanxi Worth
    hdu 5122 K.Bro Sorting
    Human Gene Functions
    Palindrome(最长公共子序列)
    A Simple problem
    Alignment ( 最长上升(下降)子序列 )
  • 原文地址:https://www.cnblogs.com/zhai1997/p/12200501.html
Copyright © 2011-2022 走看看