Struts2复杂类型的数据封装,List封装和Map封装
在实际开发当中,有可能遇到批量向数据库中插入记录,需要在页面中将数据封装到集合中。类似页面表达式方法
List封装:
前端JSP:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <h1>Struts2的复杂类型的数据封装</h1> 11 <h3>封装到List集合中,批量插入商品</h3> 12 <form action="${pageContext.request.contextPath }/productAction1.action" method="post"> 13 商品名称:<input type="text" name="products[0].name"><br/> 14 商品价格:<input type="text" name="products[0].price"><br/> 15 商品名称:<input type="text" name="products[1].name"><br/> 16 商品价格:<input type="text" name="products[1].price"><br/> 17 商品名称:<input type="text" name="products[2].name"><br/> 18 商品价格:<input type="text" name="products[2].price"><br/> 19 <input type="submit" value="提交"> 20 </form> 21 </body> 22 </html>
Action类:
1 package com.itheima.struts2.demo3; 2 3 import java.util.List; 4 5 import com.itheima.struts2.domain.Product; 6 import com.opensymphony.xwork2.ActionSupport; 7 8 public class ProductAction1 extends ActionSupport { 9 10 private List<Product> products; 11 //提供集合的set和get方法,获得list集合 12 public void setProducts(List<Product> products) { 13 this.products = products; 14 } 16 public List<Product> getProducts() { 17 return products; 18 } 19 20 public String execute() throws Exception{ 21 22 for (Product product : products) { 23 System.out.println(product); 24 }26 return NONE; 27 } 28 }
Map封装:
前端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>Struts2的复杂类型的数据封装</h1> <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>
Action类:
1 package com.itheima.struts2.demo3; 2 3 import java.util.Map; 4 5 import com.itheima.struts2.domain.Product; 6 import com.opensymphony.xwork2.ActionSupport; 7 /** 8 * 复杂类型的封装,封装到Map集合 9 * 10 */ 11 public class ProductAction2 extends ActionSupport { 12 private Map<String,Product> map; 13 14 public Map<String, Product> getMap() { 15 return map; 16 } 17 18 public void setMap(Map<String, Product> map) { 19 this.map = map; 20 } 21 22 public String execute() throws Exception{ 23 for (String key : map.keySet()) { 24 Product product = map.get(key); 25 System.out.println(key+" "+product); 26 } 27 return NONE; 28 } 29 }