码迷,mamicode.com
首页 > 其他好文 > 详细

十三 Struts2复杂类型的数据封装

时间:2018-10-21 20:05:20      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:pat   port   actions   pen   cep   col   form   类型   lis   

在实际开发当中,有可能遇到批量向数据库中插入记录,需要在页面中将数据封装到集合中。类似页面表达式方法

前端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 }

 

十三 Struts2复杂类型的数据封装

标签:pat   port   actions   pen   cep   col   form   类型   lis   

原文地址:https://www.cnblogs.com/ltfxy/p/9826438.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!