Struts2框架使用(二)之Struts2接收参数

Struts2 通过 get/set 自动获取数据

在Action中获取传递的参数,有两种方式:

1.属性驱动:

基本数据类型驱动,在Action中添加基本数据属性,在jsp上使用${name}会调用get方法获取该属性的值,在Action也可以通过set方法获取传递过来的参数。

在需要接收参数的Action类上定义所需要接收的数据,如下

这是jsp页面。

输入地址http://localhost:8888/HeadFirstStruts2Chap02a/hello?name=struts2,效果如下。

 

Bean 类型属性驱动,首先添加Bean类User

 

然后在Action中添加Bean属性,在jsp页面上使用${user.username}会调用get方法获取该属性的值,在Action也可以通过set方法获取传递过来的参数。注意这里定义User类的时候需要创建对象

需要在jsp页面修改name="user.username" name="user.password",才能从jsp页面获取数据

<%@page pageEncoding="utf-8" contentType="text/html;charset=utf-8"%>
<!DOCTYPE HTML>
<html>
  <head>
    <title></title>
  </head>
  <body>
  <form action="login" method="post">
      用户名:<input type="text" name="user.username" />
      密码:<input type="text" name="user.password" />
      <input type="submit" value="登录" />
  </form>
  </body>
</html>

运行成功。 

2.模型驱动:Action需要实现ModelDriven接口,该接口接收一个泛型,泛型是所接收的参数类型

package com.mrlv.action;

import com.mrlv.pojo.User;
import com.mrlv.service.LoginService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 模型 类型属性驱动
 * 需要实现ModelDriven接口,该接口接收一个泛型,泛型是所接收的参数类型
 * 需要在jsp页面修改name="username" name="password"
 * @author Administrator
 *
 */
public class LoginActionModel extends ActionSupport implements ModelDriven<User>{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private LoginService loginService = new LoginService();
    
    private User user = new User();

    @Override
    public String execute() throws Exception {
        System.out.println("执行3" + user.getUsername());
        if(loginService.login(user)) {
            return SUCCESS;
        } else {
            return ERROR;
        }
    }
    @Override
    public User getModel() {
        return user;
    }
}

 jsp页面修改name属性。

运行效果如下

还有处理传入多个值。如果是以下情况

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="hobby" method="post">
    爱好:
    <input type="checkbox" name="hobby" value="唱歌"/>唱歌
    <input type="checkbox" name="hobby" value="跳舞"/>跳舞
    <input type="checkbox" name="hobby" value="睡觉"/>睡觉
    <input type="checkbox" name="hobby" value="玩CF"/>玩CF
    <input type="submit" value="提交"/>
</form>
</body>
</html>

 如果想接收所有hobby的参数,Struts2提供了数组来接收

package com.mrlv.action;

import com.opensymphony.xwork2.Action;

/**
 * struts2通过拦截器采用数组拦截同名的属性
 * @author Administrator
 *
 */
public class HobbyAction implements Action{

    private String[] hobby;
    
    public String[] getHobby() {
        return hobby;
    }

    public void setHobby(String[] hobby) {
        this.hobby = hobby;
    }

    @Override
    public String execute() throws Exception {
        System.out.println("执行HobbyAction");
        if(hobby!=null){
            for(String h:hobby){
                System.out.println(h);
            }
        }
        return SUCCESS;
    }

如果需要用到添加多个Bean的时候,如下

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="student" method="post">
    <table>
        <tr>
            <th>姓名</th>
            <th>性别</th>
            <th>年龄</th>
        </tr>
        <tr>
            <td><input type="text" name="students[0].name"/></td>
            <td><input type="text" name="students[0].sex"/></td>
            <td><input type="text" name="students[0].age"/></td>
        </tr>
        <tr>
            <td><input type="text" name="students[1].name"/></td>
            <td><input type="text" name="students[1].sex"/></td>
            <td><input type="text" name="students[1].age"/></td>
        </tr>
        <tr>
            <td colspan="3">
                <input type="submit" value="提交"/>
            </td>
        </tr>
    </table>
</form>
</body>
</html>

注意,这里的name命名是要有规范。第一个是[0],第二个是[1].....。

接收的Action通过List数组来接收,如下。

package com.mrlv.action;

import java.util.List;

import com.mrlv.pojo.Student;
import com.opensymphony.xwork2.Action;

public class StudentAction implements Action{

    private List<Student> students;
    
    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    @Override
    public String execute() throws Exception {
        System.out.println("执行StudentAction");
        for(Student s:students){
            System.out.println(s);
        }
        return SUCCESS;
    }

}

这里就是Struts2接收多个参数的方法

转载于:https://www.cnblogs.com/lvshiyu/p/8034477.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值