Struts2(6)——Action传递参数

1. 将参数作为Action的成员变量


在Action中给定一个私有属性,并实现其set、get访问器,然后即可直接接收请求中的参数。

package com.imm.demo;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import java.util.Map;

public class Demo8Action extends ActionSupport {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String execute() throws Exception {
        System.out.println("name:"+name);
        return SUCCESS;
    }
}

能够这样实现的原因是在于,Action是线程安全的。而Servlet时代则不可行,因为Servlet是在客户端第一次请求某个Servlet时根据web.xml实例化的,在后续无论多少个请求访问该Servlet,都不会再实例化,而是访问同一个Servlet,因此Servlet只能在方法中接收参数。

2.通过某个实体对象

创建实体类,然后创建Action的私有属性为该实体类的实例。如下:

package com.xiaoyin.demo;

import com.opensymphony.xwork2.ActionSupport;
import com.xiaoyin.entity.User;

public class Demo9Action 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);
        return SUCCESS;
    }
}
package com.xiaoyin.entity;

import java.util.Date;

public class User {
    private String name;
    private int age;
    private Date birthday;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int 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 +
                '}';
    }
}
<%--
  Created by IntelliJ IDEA.
  User: ASCEND
  Date: 2017/9/10
  Time: 1:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html" ;charset="utf-8"/>
    <title>hello</title>
</head>
<body>
<h1>hello</h1>
<form action="${pageContext.request.contextPath}/Demo9Action.action" method="get">
    名字:<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>
</body>
</html>

3.模型驱动传参

创建实体类的实例作为Action的成员变量,Action实现ModelDriven接口,并重写该接口方法,成员对象须被初始化,以免重写的方法返回null值。

package com.xiaoyin.demo;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.xiaoyin.entity.User;

public class Demo10Action extends ActionSupport implements ModelDriven<User> {

    private User user=new User();

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

    public User getModel() {
        return user;
    }
}
<%--
  Created by IntelliJ IDEA.
  User: ASCEND
  Date: 2017/9/10
  Time: 1:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html" ;charset="utf-8"/>
    <title>hello</title>
</head>
<body>
<h1>hello</h1>
<form action="${pageContext.request.contextPath}/Demo10Action.action" method="get">
    名字:<input type="text" name="name"><br/>
    年龄:<input type="text" name="age"><br/>
    出生日期:<input type="text" name="birthday"><br/>
    <input type="submit" value="提交">
</form>
</body>
</html>

4.集合类型的参数封装

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.xiaoyin.entity.User;

import java.util.List;

public class Demo11Action extends ActionSupport {

    private List<String> list;

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

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

    @Override
    public String execute() throws Exception {
        System.out.println("list:"+list);
        return SUCCESS;
    }

}

将集合类型作为Action的一个属性,然后jsp页面

<%--
  Created by IntelliJ IDEA.
  User: ASCEND
  Date: 2017/9/10
  Time: 1:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="utf-8" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html" ;charset="utf-8"/>
    <title>hello</title>
</head>
<body>
<h1></h1>
<form action="${pageContext.request.contextPath}/Demo11Action.action" method="post">
    名字:<input type="text" name="list"><br/>
    年龄:<input type="text" name="list[1]"><br/>
    地址:<input type="text" name="list[3]"><br/>
    <input type="submit" value="提交">
</form>
</body>
</html>

如果提交Map类型的数据,那么jsp页面表单input的name应该是

name=属性名['键']
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值