Struts之获取表单提交的参数的三种方法

Struts之获取表单提交的参数的三种方法

方法一:属性驱动
我们要自己提供set/get方法(无需构造方法)
使用表单中提交出来的key作为属性名
struts提供了基本数据类型  基本数据类型的包装类 方便我们使用
当我们使用Date类的时候 只支持2018-05-04这个格式的类型转换

操作流程 
form.jsp中建立表单提交数据 -- > Demo08Action.java接收到文件并进行出来 -- > 处理完成后进入
-- > struts.xml文件 请求转发 -- > 进入1.jsp文件 打印提交成功
Demo08Action.java
package com.lanou3g.bean;
import java.util.Date;
import org.omg.CORBA.PRIVATE_MEMBER;
import com.opensymphony.xwork2.ActionSupport;

public class Demo08Action extends ActionSupport{
    private String username;
    private int age;
    private Date birthday;

    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    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 execute() throws Exception {

        System.out.println(username);
        System.out.println(age);
        System.out.println(birthday);
        return SUCCESS;
    }
}
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <package name="test" namespace="/" extends="struts-default" >
        <action name="demo08Action" class="com.lanou3g.bean.Demo08Action" method="execute" >
            <result name="success" type="dispatcher">/1.jsp</result>
        </action>       
    </package>
</struts>
form.jsp
<%@ 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="${pageContext.request.contextPath }/demo08Action" method="get" >
        姓名:<input type="text" name="username" ><br/>
        年龄:<input type="text" name="age" ><br/>
        生日:<input type="text" name="birthday"><br/>
        <input type="submit" value="提交">
    </form>
</body>
</html>
1.jsp
<%@ 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>
    恭喜你提交成功了
</body>
</html>
结果截图

这里写图片描述

方法二:对象驱动
声明一个对象 对象中包含表单中的属性
Demo09Action中起的成员变量名需要和表单中 成员变量名.属性 中的变量名完全一致
只要一致即可 名字不一定非要是user
Demo09Action.java
package com.lanou3g.bean;

import com.opensymphony.xwork2.ActionSupport;

public class Demo09Action 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;
    }
}
user.java
package com.lanou3g.bean;

import java.util.Date;

public class User {
    private String username;
    private int age;
    private Date birthday;
    public User() {
        super();
        // TODO Auto-generated constructor stub
    }
    public User(String username, int age, Date birthday) {
        super();
        this.username = username;
        this.age = age;
        this.birthday = birthday;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    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 [username=" + username + ", age=" + age + ", birthday=" + birthday + "]";
    }
}
struts.java
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <package name="test" namespace="/" extends="struts-default" >
        <action name="demo08Action" class="com.lanou3g.bean.Demo08Action" method="execute" >
            <result name="success" type="dispatcher">/1.jsp</result>
        </action>   

        <action name="demo09Action" class="com.lanou3g.bean.Demo09Action" method="execute" >
            <result name="success" type="dispatcher">/1.jsp</result>
        </action>       
    </package>
</struts>
form.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="${pageContext.request.contextPath }/demo09Action" method="get" >
        姓名:<input type="text" name="user.username" ><br/>
        年龄:<input type="text" name="user.age" ><br/>
        生日:<input type="text" name="user.birthday"><br/>
        <input type="submit" value="提交">
    </form>
</body>
</html>
1.jsp
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    恭喜你提交成功了
</body>
</html>
结果截图

这里写图片描述

方法三:模型驱动
需要实现ModelDriven接口
模型驱动 需要对 接收参数的对象 初始化
Demo10Action
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/*
 * 模型驱动 获取参数
 * 实现ModelDriven 接口
 */
public class Demo10Action extends ActionSupport implements ModelDriven<User>{
    // 声明一个对象
    // 模型驱动 必须要对 接收参数的对象 初始化
    // 注意: 对象驱动 需要修改form表单的name属性
    //      模型驱动 不需要(原来怎么整 现在就怎么整)修改
    private User user = new User();
    @Override
    public String execute() throws Exception {
        System.out.println(user);
        return SUCCESS;
    }
    // 实现接口中的方法
    @Override
    public User getModel() {
        // TODO Auto-generated method stub
        // 直接返回user对象
        return user;
    }
}
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <package name="test" namespace="/" extends="struts-default" >
        <action name="demo08Action" class="com.lanou3g.bean.Demo08Action" method="execute" >
            <result name="success" type="dispatcher">/1.jsp</result>
        </action>   

        <action name="demo09Action" class="com.lanou3g.bean.Demo09Action" method="execute" >
            <result name="success" type="dispatcher">/1.jsp</result>
        </action>       

        <action name="demo10Action" class="com.lanou3g.bean.Demo10Action" method="execute" >
            <result name="success" type="dispatcher">/1.jsp</result>
        </action>   
    </package>
</struts>
form.jsp
<body>
    <form action="${pageContext.request.contextPath }/demo10Action" method="get" >
        姓名:<input type="text" name="username" ><br/>
        年龄:<input type="text" name="age" ><br/>
        生日:<input type="text" name="birthday"><br/>
        <input type="submit" value="提交">
    </form>
</body>
1.jsp
<body>
    恭喜你提交成功了
</body>
结果截图

这里写图片描述
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值