码迷,mamicode.com
首页 > 其他好文 > 详细

struts2框架学习笔记4:获取参数

时间:2018-02-21 20:04:23      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:put   cti   int   方式   参数   for   main   创建   pre   

第一种参数获取方式:

 

编写一个前端页面,提交表单,做示例:

    <form action="${pageContext.request.contextPath}/Demo1Action">
        用户名:<input type="text" name="name" /><br>
        年龄:<input type="text" name="age" /><br>
        生日:<input type="text" name="birthday" /><br>
        <input type="submit" value="提交" />
    </form>

 

每次访问Action都会创建一个新的实例(线程安全):

package param;

import java.util.Date;

import com.opensymphony.xwork2.ActionSupport;
//第一种方式
public class Demo1Action extends ActionSupport {

    private String name;
    private Integer age;
    private Date birthday;
    
    public String execute() throws Exception{
        System.out.println("name参数值:"+name+",age参数值:"+age+",生日:"+birthday);
        return SUCCESS;
    }

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

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    
}

 

 

第二种方式获取参数:

封装一个实体类:

package domain;

import java.util.Date;

public class User {
    
    private String name;
    private Integer age;
    private Date birthday;
    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;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    @Override
    public String toString() {
        return "User [name=" + name + ", age=" + age + ", birthday=" + birthday + "]";
    }
}

 

表单要修改下:

    <form action="${pageContext.request.contextPath}/Demo2Action">
        用户名:<input type="text" name="user.name" /><br>
        年龄:<input type="text" name="user.age" /><br>
        生日:<input type="text" name="user.birthday" /><br>
        <input type="submit" value="提交" />
    </form>

 

获取参数:

package param;

import com.opensymphony.xwork2.ActionSupport;

import domain.User;

//struts2如何获得参数-方式2
public class Demo2Action extends ActionSupport  {
    //准备user对象
    private User user;

    public String execute() throws Exception { 
        
        System.out.println(user);
        
        return SUCCESS;
    }

    public User getUser() {
        return user;
    }

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

    
}

 

 

第三种方式获取参数:

模型驱动:

前端代码:

    <form action="${pageContext.request.contextPath}/Demo3Action">
        用户名:<input type="text" name="name" /><br>
        年龄:<input type="text" name="age" /><br>
        生日:<input type="text" name="birthday" /><br>
        <input type="submit" value="提交" />
    </form>

获取参数:

package param;

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

import domain.User;

public class Demo3Action extends ActionSupport implements ModelDriven<User> {

    private User user = new User();

    public String execute() throws Exception {

        System.out.println(user);

        return SUCCESS;
    }

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

}

 

第四种获取参数方式:

集合类型封装:

前端表单:

    <form action="${pageContext.request.contextPath}/Demo4Action" method="post" >
        list:<input type="text" name="list" /><br>
        list:<input type="text" name="list[3]" /><br>
        map:<input type="text" name="map[‘haha‘]" /><br>
        <input type="submit" value="提交" />
    </form>

 

获取参数:

package param;

import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;

//struts2 封装集合类型参数
public class Demo4Action extends ActionSupport  {
    //list
    private List<String> list;
    //Map
    private Map<String,String> map;
    
    
    public String execute() throws Exception { 
        
        System.out.println("list:"+list);
        System.out.println("map:"+map);
        
        return SUCCESS;
    }

    public List<String> getList() {
        return list;
    }

    public void setList(List<String> list) {
        this.list = list;
    }

    public Map<String, String> getMap() {
        return map;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }

}

 

这里获得的参数直接封装到集合中

struts2框架学习笔记4:获取参数

标签:put   cti   int   方式   参数   for   main   创建   pre   

原文地址:https://www.cnblogs.com/xuyiqing/p/8456478.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!