zoukankan      html  css  js  c++  java
  • Struts 获得前台传参几种方式

    struts.xml统一

    <struts>
        <package name="result" namespace="/" extends="struts-default" >
        <!--  转发 -->
            <action name="Demo6Action" class="com.future.api.Demo6Action" method="execute" >
                <result name="success">/index.jsp</result>
            </action>
        </package>
    </struts>

    1、Action中定义属性然后定义属性的get set 方法获得

    hello.jsp

    <body>
      <form action="${pageContext.request.contextPath }/Demo6Action">
        姓名:<input type="text" name="name" /><br/>
        年龄:<input type="text" name="age" /><br/>
        <input type="submit" value="提交" /> 
      </form>
    </body>

    Demo6Action.java

    public class Demo6Action extends ActionSupport {
    
        private String name;//必须和jsp中的input标签中的name命名一样
        private Integer age;
        
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        @Override
        public String execute() throws Exception {
            System.out.println("姓名:" + name);
            System.out.println("年龄:" + age);
            return SUCCESS;
        }
    
    }

    2、定义Bean对象的方式获得

    hello.jsp

    <form action="${pageContext.request.contextPath }/Demo6Action">
      <!-- 必须要和Action中定义的类对象名字一样 -->
      姓名:<input type="text" name="user.name" /><br/>
      年龄:<input type="text" name="user.age" /><br/>
      <input type="submit" value="提交" /> 
    </form>

    User.java

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

    Demo6Action.java

    public class Demo6Action extends ActionSupport {
    
      //参数
        private User user;
    
        public User getUser() {
            return user;
        }
    
        public void setUser(User user) {
            this.user = user;
        }
    
        @Override
        public String execute() throws Exception {
            System.out.println("姓名:" + user.getName());
            System.out.println("年龄:" + user.getAge());
            return SUCCESS;
        }
    
    }

    3、模型驱动方式

    hello.jsp

    <form action="${pageContext.request.contextPath }/Demo6Action">
      姓名:<input type="text" name="user.name" /><br/>
      年龄:<input type="text" name="user.age" /><br/>
      <input type="submit" value="提交" /> 
    </form>

    User.java

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

    Demo6Action.java

    public class Demo6Action extends ActionSupport implements ModelDriven<User> {
    
        //成员变量
        private User user = new User();    
    
        @Override
        public String execute() throws Exception {
            System.out.println("姓名:" + user.getName());
            System.out.println("年龄:" + user.getAge());
            return SUCCESS;
        }
    
        public User getModel() {
            
            return user;
        }
    
        
    }

     4、使用集合方式获取

    hello.jsp

    <body>
      <form action="${pageContext.request.contextPath }/Demo6Action">
        姓名:<input type="text" name="list" /><br/>
        年龄:<input type="text" name="list[1]" /><br/>
        地址:<input type="text" name="map['address']" /><br/>
        电话:<input type="text" name="map['phone']" /><br/>
        <input type="submit" value="提交" /> 
      </form>
    </body>

    Demo6Action.java

    public class Demo6Action extends ActionSupport {
    
        private List<String> list;
    
        public List<String> getList() {
            return list;
        }
    
        public void setList(List<String> list) {
            this.list = list;
        }
    
        private Map<String, String> map;
        
        public Map<String, String> getMap() {
            return map;
        }
    
        public void setMap(Map<String, String> map) {
            this.map = map;
        }
    
        @Override
        public String execute() throws Exception {
            System.out.println("list:" + list);
            System.out.println("map:" + map);
            return SUCCESS;
        }
    
    }
  • 相关阅读:
    windows下jmeter安装配置
    golang读取json配置文件
    磁盘分区、扇区等概念理解
    linux 命令笔记
    jvm静态分派和动态分派
    java实例初始化顺序
    计算机网络
    文件对比工具Meld
    IaaS、PaaS、SaaS
    USB 驱动常见名词解释
  • 原文地址:https://www.cnblogs.com/ms-grf/p/7257344.html
Copyright © 2011-2022 走看看