Struts2复杂数据的封装(封装到List和Map)

 

本次没什么小坑

----------------------------------------------------------------以下是代码实例--------------------------------------------

struts.xml引入以下代码

以下是demo1.jsp代码

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</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="提交"><br>
</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>

以下是ProductAction1的代码

package demo3;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

import domin.Product;

public class ProductAction1 extends ActionSupport {

private static final long serialVersionUID = 8876088464977928417L;

private List<Product> products;

public List<Product> getProducts() {
return products;
}

public void setProducts(List<Product> products) {
this.products = products;
}

@Override
public String execute() throws Exception {
for(Product product : products) {
System.out.println(product);
}
return super.execute();
}
}

以下是ProductAction2的代码

 

package demo3;

import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;

import domin.Product;

public class ProductAction2 extends ActionSupport {

private static final long serialVersionUID = 8876088464977928417L;

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(key + " " + product);
}
return super.execute();
}
}

struts_demo03.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts SYSTEM "http://struts.apache.org/dtds/struts-2.5.dtd" >
<struts>
<package name="demo3" extends="struts-default" namespace="/">
<global-results>
<result>/demo1/demo2.jsp</result>
</global-results>
<action name="productAction1" class="demo3.ProductAction1">
</action>
<action name="productAction2" class="demo3.ProductAction2">
</action>
</package>
</struts>

posted @ 2019-01-14 13:19  李大鹏的博客  阅读(233)  评论(0编辑  收藏  举报