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

Posted xuyiqing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了struts2框架学习笔记4:获取参数相关的知识,希望对你有一定的参考价值。

第一种参数获取方式:

 

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

    <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:获取参数的主要内容,如果未能解决你的问题,请参考以下文章

Struts2学习笔记二:Struts2重定向和转发配置Struts2获取表单参数域对象

[原创]java WEB学习笔记61:Struts2学习之路--通用标签 property,uri,param,set,push,if-else,itertor,sort,date,a标签等(代码片段

struts2学习笔记—— 在用户注冊程序中使用验证框架

struts2学习笔记

Struts 2 学习笔记

Struts2-整理笔记Action生命周期如何获取参数(3种)集合类型参数封装