Struts2数据封装
- 2019 年 10 月 7 日
- 笔记
首先是简单数据类型的封装
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; } }
结果