• struts2 之 Action的优化配置


    总结:struts2种action的配置文件会随着业务的增加而增加,导致配置文件膨胀。struts2中提供了三种方案来解决这个问题:

      1. 动态方法调用来实现。

          2. 通配符配置来解决。

      3. 使用注解的方式来实现。

    实例:传统的action配置方法:

    一 . Action处理类中有4个处理方法:

    public class UserAction {
        public String add(){
            System.out.println("添加");
            return Action.SUCCESS;
        }
        public String delete(){
            System.out.println("删除");
            return Action.SUCCESS;
        }
        public String find(){
            System.out.println("查询");
            return Action.SUCCESS;
        }
        public String update(){
            System.out.println("修改");
            return Action.SUCCESS;
        }
    }

    Action的配置:

    <package name="default" namespace="/" extends="struts-default">
            <!-- 传统的配置方式 -->
            <action name="add" class="cn.sxt.action.UserAction" method="add">
                <result>/index.jsp</result>
            </action>
            <action name="find" class="cn.sxt.action.UserAction" method="find">
                <result>/index.jsp</result>
            </action>
            <action name="delete" class="cn.sxt.action.UserAction" method="delete">
                <result>/index.jsp</result>
            </action>
            <action name="update" class="cn.sxt.action.UserAction" method="update">
                <result>/index.jsp</result>
            </action>
        </package>

    二. 动态方法  优化实例:

    1. 动态方法调用首先需要将下面常量配置为 true

    <!-- 配置动态方法调用为true,不同版本默认配置不一致 -->
        <constant name="struts.enable.DynamicMethodInvocation" value="true"/>

    2. 在struts.xml配置文件中,只需要配置一个action即可:

    <action name="userAction" class="cn.sxt.action.UserAction">
         <result>/index.jsp</result>
     </action>

    3. 再调用action时,需要按照一下规定进行调用:ActionName!methodName.action

      如:http://localhost:8080/action_cfg/userAction!delete.action

    三 . 使用通配符配置类解决action配置优化问题。

    <!-- 使用通配符进行action的配置
                 * 表示匹配多个字符
                 {1} 表示占位符 1表示的第一个*的内容
                 如果请求的是add.action,那么* 表示add  {1}表示add
              -->
              <action name="*" class="cn.sxt.action.UserAction" method="{1}">
                  <result>/index.jsp</result>
              </action>

    注意:如果使用通配符进行配置action,需要注意不同的处理方法返回值的问题。如果有指定名称的action,那么首先匹配指定名称的action,没有再匹配通配符。

    四. 使用注解来进行action的配置:

    1.导入struts2-convention-plugin-2.3.4.jar

    2.在action类上配置注解 如:

    @ParentPackage("struts-default") 
    @Namespace("/") 
    public class UserAction {
        @Action(value="/add",
                results={@Result(name="success", location="/index.jsp", type="redirect"),
                @Result(name="error", location="/error.jsp", type="redirect")})
        public String add(){
            System.out.println("添加");
            return "success";
        }
        @Action("/delete")
        public String delete(){
            System.out.println("删除");
            return "success";
        }
        @Action("/find")
        public String find(){
            System.out.println("查询");
            return "success";
        }
        @Action("/update")
        public String update(){
            System.out.println("修改");
            return "success";
        }
    }

       

  • 相关阅读:
    Charles:rewrite重写功能
    Vue中provide和inject 用法
    vue中install方法
    vue自定义组件(通过Vue.use()来使用)即install的使用
    Eelectron 中的remote模块
    理解Vue中的Render渲染函数
    Vue.js中this.$nextTick()的使用
    postman请求本地接口Error: connect ECONNREFUSED 127.0.0.1:8083
    1016 Phone Bills (25 分)
    CF842E Nikita and game
  • 原文地址:https://www.cnblogs.com/forever2h/p/6708673.html
Copyright © 2020-2023  润新知