zoukankan      html  css  js  c++  java
  • Struts2 Action接收表单参数

    struts2 Action获取表单传值   
    1.通过属性驱动式   
    JSP:   
        <form action="sys/login.action" method="post">   
            <input type="text" name="username">   
            <input type="submit" value="submit">   
        </form>   
           
    Action:直接通过get、set方法获取。   
      

    public class sysAction extends ActionSupport{   
        private String username;   
      
        public String login() throws Exception {   
            System.out.println(username);   
            return SUCCESS;   
        }   
      
        public String getUsername() {   
            return username;   
        }   
        public void setUsername(String username) {   
            this.username= username;   
        }   
    }   
           



    2.模型驱动方式,必须要实现ModelDriven<T>接口。对于要传入多个model第二种方式不方便   
    JSP:   
        <form action="sys/login.action" method="post">   
            <input type="text" name="username">   
            <input type="submit" value="submit">   
        </form>   
           
    Action:必须实现getModel() 方法   
      

    public class sysAction extends ActionSupport implements ModelDriven<User>{   
        private User user;   
      
        public String login() throws Exception {   
            System.out.println(getModel().getUsername());   
            return SUCCESS;   
        }   
      
        public User getModel() {   
            if (null == user) {   
                return user = new User();   
            }   
            return user;   
        }   
    }   
           



    3.第三种方式可以完全不实现ModelDriven<T>,也可使用多个model对象的属性。   
    JSP:   
        <form action="sys/login.action" method="post">   
            <input type="text" name="user.username">   
            <input type="text" name="teacher.level">   
            <input type="submit" value="submit">   
        </form>   
           
    Action: 必须提供set方法   
      

    public class sysAction extends ActionSupport{   
        private User user;   
        private Teacher teacher;   
      
        public String login() throws Exception {   
            System.out.println(user.getUsername());   
            System.out.println(teacher.getLevel());   
            return SUCCESS;   
        }   
      
        public void setUser(User user) {   
            this.user = user;   
        }   
        public void setTeacher(Teacher teacher) {   
            this.teacher = teacher;   
        }   
    }  



     

  • 相关阅读:
    十大经典排序算法最强总结(含JAVA代码实现)
    Java相关官方链接
    强网杯2020(Funhash)
    强网杯2020(主动)
    强网杯2020(upload)
    (内存取证)46届世界技能大赛湖北省选拔赛
    (流量分析)46届世界技能大赛湖北省选拔赛
    CISCO ASA远程任意文件读取
    OOALV 分割屏幕
    Process Doppelg&#228;nging
  • 原文地址:https://www.cnblogs.com/pangblog/p/3294037.html
Copyright © 2011-2022 走看看