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;
    }

}

 

posted @ 2017-07-29 20:18  发福大叔  阅读(1020)  评论(0编辑  收藏  举报