Struts2数据封装
首先是简单数据类型的封装
jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>Sturts2的数据封装</h1>
<h3>方式一:属性驱动-提供set方法的方式</h3>
<s:fielderror/>
<form action="${pageContext.request.contextPath}/userAction1.action" method="post">
用户名:<input type="text" name="username"><br/>
密码:<input type="password" name="password"><br/>
年龄:<input type="text" name="age"><br/>
生日:<input type="date" name="birthday"><br/>
工资:<input type="text" name="salary"><br/>
<input type="submit" value="提交">
</form> <h3>方式二:属性驱动-在页面中提供表达式的方式</h3>
<form action="${pageContext.request.contextPath}/userAction2.action" method="post">
用户名:<input type="text" name="user.username"><br/>
密码:<input type="password" name="user.password"><br/>
年龄:<input type="text" name="user.age"><br/>
生日:<input type="date" name="user.birthday"><br/>
工资:<input type="text" name="user.salary"><br/>
<input type="submit" value="提交">
</form> <h3>方式三:模型驱动的方式</h3>
<form action="${pageContext.request.contextPath}/userAction3.action" method="post">
用户名:<input type="text" name="username"><br/>
密码:<input type="password" name="password"><br/>
年龄:<input type="text" name="age"><br/>
生日:<input type="date" name="birthday"><br/>
工资:<input type="text" name="salary"><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.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd"> <struts>
<package name="demo2" extends="struts-default" namespace="/">
<global-results>
<result name="input">/</result>
</global-results>
<action name="userAction1" class="com.jinke.domain.UserAction1"/>
<action name="userAction2" class="com.jinke.domain.UserAction2"/>
<action name="userAction3" class="com.jinke.domain.UserAction3"/>
</package>
</struts>
实体类
import java.util.Date;
public class User {
private String username;
private String password;
private Integer age;
private Date birthday;
private Double salary;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
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;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
", birthday=" + birthday +
", salary=" + salary +
'}';
}
}
简单数据封装的三种方式
import com.opensymphony.xwork2.ActionSupport; import java.util.Date; /**
* 数据封装的方式一:提供属性的set方法
*/
public class UserAction1 extends ActionSupport { private String username;
private String password;
private Integer age;
private Date birthday;
private Double salary; @Override
public String execute() throws Exception {
//接收数据
System.out.println(username);
System.out.println(password);
System.out.println(age);
System.out.println(birthday);
System.out.println(salary);
//封装数据
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.setAge(age);
user.setBirthday(birthday);
user.setSalary(salary);
return NONE;
} public void setUsername(String username) {
this.username = username;
} public void setPassword(String password) {
this.password = password;
} public void setAge(Integer age) {
this.age = age;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} public void setSalary(Double salary) {
this.salary = salary;
}
}
import com.opensymphony.xwork2.ActionSupport; /**
* 数据封装的方式二:属性驱动-在页面中提供表达式的方式
*/
public class UserAction2 extends ActionSupport {
//提供一个User对象
private User user; public void setUser(User user) {
this.user = user;
} public User getUser() {
return user;
} @Override
public String execute() throws Exception {
System.out.println(user.toString());
return NONE;
}
}
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; /**
* 数据封装的方式三:模型驱动的方式
*/
public class UserAction3 extends ActionSupport implements ModelDriven<User> {
private User user = new User();//手动实例化User @Override
public String execute() throws Exception {
System.out.println(user.toString());
return NONE;
} //模型驱动需要使用的方法
@Override
public User getModel() {
return user;
}
}
结果

下面是复杂数据类型的封装
jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>Struts2复杂类型的数据封装</h1>
<h3>封装到List集合中:批量插入商品</h3>
<form action="${pageContext.request.contextPath}/productAction1.action" method="post">
商品名称:<input type="text" name="products[0].name"><br/>
商品价格:<input type="text" name="products[0].price"><br/>
商品名称:<input type="text" name="products[1].name"><br/>
商品价格:<input type="text" name="products[1].price"><br/>
商品名称:<input type="text" name="products[2].name"><br/>
商品价格:<input type="text" name="products[2].price"><br/>
<input type="submit" value="提交">
</form> <h3>封装到Map集合中:批量插入商品</h3>
<form action="${pageContext.request.contextPath}/productAction2.action" method="post">
商品名称:<input type="text" name="map['one'].name"><br/>
商品价格:<input type="text" name="map['one'].price"><br/>
商品名称:<input type="text" name="map['two'].name"><br/>
商品价格:<input type="text" name="map['two'].price"><br/>
商品名称:<input type="text" name="map['three'].name"><br/>
商品价格:<input type="text" name="map['three'].price"><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.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd"> <struts>
<package name="demo3" extends="struts-default" namespace="/">
<action name="productAction1" class="com.jinke.product.ProductAction1"/>
<action name="productAction2" class="com.jinke.product.ProductAction2"/>
</package>
</struts>
实体类
public class Product {
private String name;
private Double price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
@Override
public String toString() {
return "Product{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
}
数据保存到List中
import com.opensymphony.xwork2.ActionSupport; import java.util.List; /**
* 负责类型数据封装到List中
*/
public class ProductAction1 extends ActionSupport {
private List<Product> products; public void setProducts(List<Product> products) {
this.products = products;
} public List<Product> getProducts() {
return products;
} @Override
public String execute() throws Exception {
for (Product product : products) {
System.out.println(product);
}
return NONE;
}
}
数据保存到Map中
import com.opensymphony.xwork2.ActionSupport; import java.util.Map; /**
* 负责类型数据封装到Map中
*/
public class ProductAction2 extends ActionSupport {
private Map<String, Product> map; public Map<String, Product> getMap() {
return map;
} public void setMap(Map<String, Product> map) {
this.map = map;
} @Override
public String execute() throws Exception {
for (String key : map.keySet()) {
Product product = map.get(key);
System.out.println(product.toString());
}
return NONE;
}
}
结果

欢迎关注我的微信公众号:安卓圈

Struts2数据封装的更多相关文章
- JAVA框架Struts2 数据封装
一.strust2封装数据包含二部分: 数据在到Action类的时候,会经过拦截器,拦截器会有很多功能:比如数据封装.类型转换等. 我可以查看下strust-default.xml文件,看那些拦截器默 ...
- Struts2的数据封装
在很多的实际开发场景中,页面提交请求参数Action ,在Action中接收参数并对接收的数据进行封装.封装到一个JavaBean中,将JavaBean传递给业务层中.Struts2数据封装分为两类: ...
- Struts2基础学习(三)—Result和数据封装
一.Result Action处理完用户请求后,将返回一个普通的字符串,整个普通字符串就是一个逻辑视图名,Struts2根据逻辑视图名,决定响应哪个结果,处理结果使用<result&g ...
- Struts2中类数据封装的方式
第一种方式:属性驱动提供对应属性的set方法进行数据的封装.表单的哪些属性需要封装数据,那么在对应的Action类中提供该属性的set方法即可.表单中的数据提交,最终找到Action类中的setXxx ...
- java框架之Struts2(2)-访问Servlet API及请求数据封装
准备 为后面测试示例编写代码及配置如下: package com.zze.bean; import java.util.Date; public class User { private String ...
- Struts2框架的数据封装一之属性封装(属性封装的第二种方式:封装成javaBean)
Struts2中提供了两类数据封装的方式? 第一种方式:属性驱动(有两种方式:一个对属性,另外一个是将参数封装到javaBean中) B. 在页面上,使用OGNL表达式进行数据封装.(将参数封装到ja ...
- Struts2框架的数据封装一之属性封装(属性封装的第一种方式:对参数进行封装)
request带着参数来,aciton对其进行处理.在学习action之前,使用的是servlet对request进行处理.request请求时会带有参数,所以我们要对这些参数进行封装. 1. 为什么 ...
- struts2学习笔记(四)——访问Servlet的API&结果跳转&数据封装
一.Struts2访问Servlet的API 前面已经对Struts2的流程执行完成了,但是如果表单中有参数如何进行接收?又或者我们需要向页面保存一些数据,又要如何完成呢?我们可以通过学习Struts ...
- Struts2中数据封装机制
Struts2当中数据封装的三种机制:属性驱动.标签驱动.模型驱动.下面来一一介绍. 一.属性驱动 1.需要提供对应属性的set方法进行数据的封装. 2.表单的哪些属性需要封装数据,那么在对应的Act ...
随机推荐
- Codeforces A. Kyoya and Colored Balls(分步组合)
题目描述: Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- crontab每小时运行一次(转)
https://blog.csdn.net/liu0808/article/details/80668705 先给出crontab的语法格式 对于网上很多给出的每小时定时任务写法,可以说绝大多数都是错 ...
- Dart编译技术与平台
Flexible compiler technology lets you run Dart code in different ways, depending on your target plat ...
- SQL注入学习
本次实验环境用的是Xampp,搭建的sqli-labs 配置环境: 下载路径https://github.com/Audi-1/sqli-labs,下载源代码,将解压好的文件夹放在xampp\htdo ...
- The Ultimate Guide to handling JWTs on frontend clients (GraphQL)
转自:https://blog.hasura.io/best-practices-of-using-jwt-with-graphql/ hasura 团队关于jwt 的实践 JWTs (JSON We ...
- 如何保证最少消费一次redis的list队列数据
简使用pop,不能保证最少消费一次,比如pop超时可能中途丢失,或者消费者处理过程中异常而未能处理完. 解决此问题有多种方法: 1) 方法一:使用rpoplpush替代pop 这种方法相当于建立了一个 ...
- 【BIEE】使用BIPublisher做报表时,选择多个参数使用IN的问题
在使用BIPublisher做报表的时候,报表出现xml数据加载错误的情况 环境描述 仪表盘提示是表示变量,并且支持多选 报表使用xdo方式制作的,直接使用JDBC直连数据库获取数据 数据集中的SQL ...
- UDS的使用
我们通过对导热微分方程式的求解,并与Fluent自己的求解结果进行对比,介绍一下Fluent当中UDS(自定义标量)的具体使用方法. 首先Fluent当中的UDS主要针对下面这样形式的方程: 其中: ...
- 查看大图、html查看大图、js查看大图
$(".pimg").click(function(){ var _this = $(this);//将当前的pimg元素作为_this传入函数 imgShow("#ou ...
- cv2 的用法
转载:https://www.cnblogs.com/shizhengwen/p/8719062.html 一.读入图像 使用函数cv2.imread(filepath,flags)读入一副图片 fi ...