input.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>
<form action="deal3.action">
商品名字:<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>
</body>
</html>
out.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
商品:<s:property value="products[0].name"></s:property><br>
价格:<s:property value="products[0].price"></s:property><br>
商品:<s:property value="products[1].name"></s:property><br>
价格:<s:property value="products[1].price"></s:property><br>
商品:<s:property value="products[2].name"></s:property><br>
价格:<s:property value="products[2].price"></s:property><br>
</body>
</html>
struts.xml
<action name="deal3" class="Action.deal3" >
<result name="out">/封装到list集合out.jsp</result>
</action>
bean.java
package bean;
public class product {
private String name;
private String price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
action.java
package Action;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import bean.product;
public class deal3 extends ActionSupport {
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 {
return "out";
}
}