• struts 页面调用Action的指定方法并传递参数


    如果为action配置了类,那么默认就会执行Action类的excute方法,Action类的写法三种:

    public class Action1 {

        public String execute(){
            return "abc";//根据返回结果,去xml文件中找name与之匹配的result
        }
       
    }

    import com.opensymphony.xwork2.Action;//ActionSupport类实现了该接口

    public class Action2 implements Action {

        @Override
        public String execute() throws Exception {
            // TODO Auto-generated method stub
            return "success";
        }

    }

    import com.opensymphony.xwork2.ActionSupport;

    public class Action3 extends ActionSupport {

        @Override
        public String execute(){
            // TODO Auto-generated method stub
            return "success";
        }
    }

    推荐使用第三种。

    调用Action类的指定方法可以在xml中配置(以下文件三处蓝字代表三种方法):

    struts.xml:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

    <struts>

        <constant name="struts.devMode" value="true" />
        <constant name="struts.i18n.encoding" value="GBK" /> <!-- internationalization -->

        <package name="front" namespace="/" extends="struts-default">

            <action name="hello"><!-- 当不配置class的时候,会调用默认的ActionSupport类 (xwork框架中的类)-->
                <result>
                    /Hello.jsp
                </result>
            </action>
           
            <action name="act1" class="test.Action1">
                <result name="success">/ActionS.jsp</result>
                <result name="failure">/ActionF.jsp</result>
                <result name="abc">/Hello.jsp</result>
            </action>
           
           <action name="act2" class="test.Action2">
                <result name="success">/ActionS.jsp</result>
                <result name="failure">/ActionF.jsp</result>
                <result name="abc">/Hello.jsp</result>
            </action>
        </package>

        <package name="user" extends="struts-default" namespace="/user">
            <action name="userAdd" class="test.UserAction"method="add">
            <!--
           调用方法由execute改为add方法,在jsp页面中链接后边加上!add也可以达到同样效果。推荐后者。
            后者又称DMI Dynamic Method Invoke 动态方法调用
            在jsp中写法例子:
                <% String context = request.getContextPath(); %>
                <a href="<%=context %>/user/user!add">添加用户</a>
             -->
                <result>/userAddSuccess.jsp</result>
            </action>
        </package>
       
        <!-- 通配符示例,使用通配符要遵守“约定优于配置”的原则 –>

        <!-- 关于*号的匹配:如果地址栏中的action名在配置文件中找不到同名的action名,

               则对有*号的可以匹配的action名,优先匹在配置文件中书写位置靠上的action -->
        <package name="actions" extends="struts-default" namespace="/actions">
            <action name="Student*" class="test.struts2.action.StudentAction" method="{1}">
                <result>/Student{1}_success.jsp</result>
            </action>
           <!-- {1}就代表name中的第一个*,以此类推 -->
            <action name="*_*" class="test.struts2.action.{1}Action" method="{2}">
                <result>/{1}_{2}_success.jsp</result>
                <!-- {0}_success.jsp -->
            </action>
        </package>
    </struts>

    使用Action类中指定的方法如果需要参数有以下三种方式:

    直接在jsp的链接中写:“/user!add?name=a&age=8”就可以把参数传递到下面的Action中

    import com.opensymphony.xwork2.ActionSupport;

    public class UserAction extends ActionSupport {
       
        private String name;
        private int age;
       
        public String add() {
            System.out.println("name=" + name);
            System.out.println("age=" + age);
            return SUCCESS;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }
    }

    使用Domain Model接收参数<a href="user/user!add?user.name=a&user.age=8">添加用户</a>

    这种方式对应的Action:

    import com.bjsxt.struts2.user.model.User;
    import com.opensymphony.xwork2.ActionSupport;

    public class UserAction extends ActionSupport {
       
       private User user;
        //private UserDTO userDTO;
        public String add() {
            System.out.println("name=" + user.getName());
            System.out.println("age=" + user.getAge());
            return SUCCESS;
        }

        public User getUser() {
            return user;
        }

        public void setUser(User user) {
            this.user = user;
        }
       
    }

    public class User {
        private String name;
        private int age;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
    }

    使用ModelDriven接收参数<a href="user/user!add?name=a&age=8">添加用户</a>

    也可以用下面的Action


    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.ModelDriven;

    public class UserAction extends ActionSupport implements ModelDriven<User>{//此处要指定清楚
        
        private User user = new User(); //需要自己new   
        
        public String add() {
            System.out.println("name=" + user.getName());
            System.out.println("age=" + user.getAge());
            return SUCCESS;
        }

        @Override
        public User getModel() {
            return user;
        }
       
    }

    public class User {
        private String name;
        private int age;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
    }

  • 相关阅读:
    css js 解除网页无法选择进而复制的限制,bd文库无法复制
    Git命令简记
    DDD基本概念-未完成
    多线程隙-IO模型(BIO、NIO、AIO)
    RabbitMQ笔记-保证消息队列高可用
    关于fiddler手机抓包
    spring控制反转是谁在何时何地反转给谁?依赖注入??
    Cookie、Session、Token的区别
    详解Redis中两种持久化机制RDB和AOF
    Java中线程池的抛出策略、阻塞队列、内存溢出
  • 原文地址:https://www.cnblogs.com/flying607/p/3469295.html
Copyright © 2020-2023  润新知