Struts2的数据封装
在很多的实际开发场景中,页面提交请求参数Action ,在Action中接收参数并对接收的数据进行封装。封装到一个JavaBean中,将JavaBean传递给业务层中。Struts2数据封装分为两类:属性驱动,模型驱动。
1.模型驱动
通过实现ModelDriven接口来接收请求参数。实现接口并且重写getModel()方法
Action类代码如下:
package com.huan.web.action; import com.huan.domain.Customer;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{ private Customer customer=new Customer(); public String add(){
System.out.println(customer); return "saveSuccess";
} @Override
public Customer getModel() { return customer;
} }
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>
<h1>处理请求参数</h1>
<form action="${pageContext.request.contextPath}/Demo10Action.action" >
姓名:<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>
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="param" namespace="/" extends="struts-default">
<action name="Demo10Action" class="com.sturts2.day02.c_param.Demo10Action" method="execute">
<result name="success" type="dispatcher">/form3.jsp</result>
</action> </package>
</struts>
2.属性驱动(很少使用)
在Action中定义java数据类型字段并与表单数据对应,利用这些字段进行数据传递
2.1属性驱动之提供属性的set方法(做为了解)
这中方法要在Action中定义属性并提供属性的set方法,当传递数据变多,Action的属性和set方法也随之变多。会让Action变臃肿,不简洁。
form1.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}/Demo8Action">
用户名:<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>
Demo8Action.java 即Action类
package com.sturts2.day02.c_param;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class Demo8Action extends ActionSupport {
private String name;
private Integer age;
private Date birthday;
public Demo8Action() {
super();
System.out.println("Demo8Action对象创建了");
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String execute() throws Exception {
System.out.println("name参数值:"+name+"age参数值:"+age+"birthday参数值"+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="param" namespace="/" extends="struts-default"> <action name="Demo8Action" class="com.sturts2.day02.c_param.Demo8Action" method="execute">
<result name="success" type="dispatcher">/form1.jsp</result>
</action> </package>
</struts>
打开form1.jsp页面

输上数据并提交,控制台显示

浏览器显示: URL不变请求转发到form.jsp页面

2.2页面提供表达式方式
在页面表单上显示表达式:
<form action="${pageContext.request.contextPath}/Demo9Action">
用户名:<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>
Action类
public class Demo9Action extends ActionSupport{
private User user;
@Override
public String execute() throws Exception {
System.out.println(user);
return SUCCESS;
}
//需要提供get方法
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
还需要提供User的实体类:
属性值要和表单上name属性值对应
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() {
// TODO Auto-generated method stub
return "User [ name="+name+"\n age="+age+"\n birthday="+birthday+"]";
}
}
Struts.xml 的action配置就不做显示
把程序在服务器运行,打开页面填上数据:

控制台显示

浏览器请求转发到form2.jsp页面

3.Struts2中封装集合类型的数据
在开发中,有时我们需要批量插入用户或者批量插入其他对象。在Action中需要接收多个Action中封装的对象然后传递给业务层。这个时候就需要把表单的信息封装到集合中。一般我们通常使用集合 List或Map
3.1 封装到List集合中
编写页面:
<form action="${pageContext.request.contextPath}/Demo9Action.action" >
姓名:<input type="text" name="list[0].name"><br/>
年龄:<input type="text" name="list[0].age"><br/>
生日:<input type="text" name="list[0].birthday"><br/>
姓名:<input type="text" name="list[1].name"><br/>
年龄:<input type="text" name="list[1].age"><br/>
生日:<input type="text" name="list[1].birthday"><br/>
<input type="submit" value="提交">
</form>
编写Action:
public class Demo9Action extends ActionSupport{
private List<User> list;
@Override
public String execute() throws Exception {
for(User user :list){
System.out.println(user);
}
return SUCCESS;
}
public List<User> getUser() {
return list;
}
public void setUser(List<User> list) {
this.list = list;
}
3.2 封装数据到Map集合:
页面:
<form action="${pageContext.request.contextPath}/Demo10Action.action" >
姓名:<input type="text" name="map['one'].name"><br/>
年龄:<input type="text" name="map['one'].age"><br/>
生日:<input type="text" name="map['one'].birthday"><br/>
姓名:<input type="text" name="map['two'].name"><br/>
年龄:<input type="text" name="map['two].age"><br/>
生日:<input type="text" name="map['two'].birthday"><br/>
<input type="submit" value="提交">
</form>
Action类:
public class Demo10Action extends ActionSupport{
private Map<String ,User> map;
@Override
public String execute() throws Exception {
for(Stirng key : map.keySet()){
User user=map.get(key);
System.out.println(key+" "+user);
}
return SUCCESS;
}
public Map<String ,User> getMap() {
return map;
}
public void setMap(Map<String ,User> map) {
this.map = map;
}
Struts2的数据封装的更多相关文章
- Struts2中数据封装机制
Struts2当中数据封装的三种机制:属性驱动.标签驱动.模型驱动.下面来一一介绍. 一.属性驱动 1.需要提供对应属性的set方法进行数据的封装. 2.表单的哪些属性需要封装数据,那么在对应的Act ...
- 十一 三种Struts2的数据封装方式,封装页面传递的数据
Struts2的数据封装:Struts2是一个web层框架,框架是软件的半成品.提供了数据封装的基本功能. 注:Struts2底层(核心过滤器里面的默认栈里面的拦截器,具体见struts-defaul ...
- Struts2中类数据封装的方式
第一种方式:属性驱动提供对应属性的set方法进行数据的封装.表单的哪些属性需要封装数据,那么在对应的Action类中提供该属性的set方法即可.表单中的数据提交,最终找到Action类中的setXxx ...
- Struts2中数据封装方式
一.通过ActionContext类获取 public class ActionContextDemo extends ActionSupport { @Override public S ...
- Struts2把数据封装到集合中之封装到map中
struts框架封装数据可以封装到集合中也可以封装到map中,该篇博客主要讲解将数据封装到map中. 1. 封装复杂类型的参数(集合类型 Collection .Map接口等) 2. 需求:页面中有可 ...
- Struts2把数据封装到集合中之封装到Collection中
数据封装到集合中,可以封装到集合中,也可以封装到Map中.该篇博客主要讲解数据封装到集合中的封装到Collection中. 1. 封装复杂类型的参数(集合类型 Collection .Map接口等) ...
- Struts2类数据封装
- struts2学习笔记(四)——访问Servlet的API&结果跳转&数据封装
一.Struts2访问Servlet的API 前面已经对Struts2的流程执行完成了,但是如果表单中有参数如何进行接收?又或者我们需要向页面保存一些数据,又要如何完成呢?我们可以通过学习Struts ...
- Struts2之Action接收请求参数和拦截器
技术分析之在Struts2框架中使用Servlet的API 1. 在Action类中也可以获取到Servlet一些常用的API * 需求:提供JSP的表单页面的数据,在Ac ...
随机推荐
- HDU 4372 Count the Buildings [第一类斯特林数]
有n(<=2000)栋楼排成一排,高度恰好是1至n且两两不同.现在从左侧看能看到f栋,从右边看能看到b栋,问有多少种可能方案. T组数据, (T<=100000) 自己只想出了用DP搞 发 ...
- C 洛谷 P3599 Koishi Loves Construction [构造 打表观察]
题目描述 Koishi决定走出幻想乡成为数学大师! Flandre听说她数学学的很好,就给Koishi出了这样一道构造题: Task1:试判断能否构造并构造一个长度为的的排列,满足其个前缀和在模的意义 ...
- VUE 2.0 引入高德地图,自行封装组件
1. 高德地图官网 申请帐号, 申请相应(JavaScript API)的 Key 2. 在项目中引入, 这里和其他的引入不同的是 直接在 index.html, 不是在 main.js 引入, 博主 ...
- javaweb重定向的两种方式
第一种 import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.Htt ...
- css页面布局之左侧定宽,右侧自适应
二列布局的特征是侧栏固定宽度,主栏自适应宽度.三列布局的特征是两侧两列固定宽度,中间列自适应宽度. 之所以将二列布局和三列布局写在一起,是因为二列布局可以看做去掉一个侧栏的三列布局,其布局的思想有异曲 ...
- openvpn 客户端一键脚本安装
#!/bin/bash dir=/etc/openvpn#file=AD00012basepath=$(cd `dirname $0`; pwd)PACKAGE_DIR="${basepat ...
- dnspython模块安装
wget http://www.dnspython.org/kits/1.12.0/dnspython-1.12.0.tar.gz tar -zxvf dnspython-1.12.0.tar.gz ...
- 一个例子理解break和continue的区别
结论:break用于终止整个循环,而continue用于终止某一次循环.public class Test { public static void main(String[] args) { for ...
- Mybatis学习之道(一)
本例子为采用的mysql+maven+mybatis构建. 初步学习mybatis: mybatis为一个半自动框架,相对于hibernate来说他更加轻巧,学习成本更低. 1.新建一个maven工程 ...
- Office 365 共享链接直接进入编辑
首先在Word online共享文档(不多赘述) 但这个链接打开的是预览视图,要点击右上角的"在浏览器中编辑"才能真正编辑. 但是很多情况都是没必要进入这个预览界面再编辑的.这多点 ...