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

List封装:

前端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>封装到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>
</body>
</html>

Action类:

 package com.itheima.struts2.demo3;

 import java.util.List;

 import com.itheima.struts2.domain.Product;
import com.opensymphony.xwork2.ActionSupport; public class ProductAction1 extends ActionSupport { private List<Product> products;
//提供集合的set和get方法,获得list集合
public void setProducts(List<Product> products) {
this.products = products;
}
public List<Product> getProducts() {
return products;
} public String execute() throws Exception{ for (Product product : products) {
System.out.println(product);
} return NONE;
}
}

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类:

 package com.itheima.struts2.demo3;

 import java.util.Map;

 import com.itheima.struts2.domain.Product;
import com.opensymphony.xwork2.ActionSupport;
/**
* 复杂类型的封装,封装到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;
} public String execute() throws Exception{
for (String key : map.keySet()) {
Product product = map.get(key);
System.out.println(key+" "+product);
}
return NONE;
}
}

十三 Struts2复杂类型的数据封装,List封装和Map封装的更多相关文章

  1. Struts2中将表单数据封装到List和Map集合中

    一.将表单数据封装到Map集合中 1.创建MapAction类 import cn.entity.User; import com.opensymphony.xwork2.ActionSupport; ...

  2. struts2 获取表单数据封装到list和map集合

    一.获取封装表单数据到list集合 示例 获取用户输入的用户名和密码并输出用户名. jsp页面 list[0]表示list中的第一个user对象 Java代码 二.封装表单数据到map集合 示例 获取 ...

  3. struts2(三)之表单参数自动封装与参数类型自动转换

    前言 对struts2的使用不外乎这几点,参数自动封装,拦截器的使用,数据校验,ognl表达(值栈和actionContext的讲解),struts2的标签,struts2的国际化, struts2的 ...

  4. Struts2把数据封装到集合中之封装到map中

    struts框架封装数据可以封装到集合中也可以封装到map中,该篇博客主要讲解将数据封装到map中. 1. 封装复杂类型的参数(集合类型 Collection .Map接口等) 2. 需求:页面中有可 ...

  5. Struts2把数据封装到集合中之封装到Collection中

    数据封装到集合中,可以封装到集合中,也可以封装到Map中.该篇博客主要讲解数据封装到集合中的封装到Collection中. 1. 封装复杂类型的参数(集合类型 Collection .Map接口等) ...

  6. Struts2框架的数据封装一之属性封装(属性封装的第二种方式:封装成javaBean)

    Struts2中提供了两类数据封装的方式? 第一种方式:属性驱动(有两种方式:一个对属性,另外一个是将参数封装到javaBean中) B. 在页面上,使用OGNL表达式进行数据封装.(将参数封装到ja ...

  7. struts2学习笔记之十一:struts2的类型转换器

    Struts2的类型转换器   如何实现Struts2的类型转换器? * 继承StrutsTypeConverter * 覆盖convertFromString和convertToString   注 ...

  8. struts2结果类型

    struts2结果类型: 结果类型 描述 前request域属性是否丢失 1 dispatcher 用于与jsp整合的结果类型.默认结果类型. 2 chain Action链式处理结果类型.前一个Ac ...

  9. struts2自定义类型转换器

    首先,何为struts2的类型转换器? 类型转换器的作用是将请求中的字符串或字符串数组参数与action中的对象进行相互转换. 一.大部分时候,使用struts2提供的类型转换器以及OGNL类型转换机 ...

随机推荐

  1. 动手实现CapsNet系列——2 实现(未完待续)

    执行后返回如下信息: Loading complete. Training started! [epoch 0][iter 0] loss: 0.8082, acc: 14.0000% (14/100 ...

  2. 每天进步一点点------Altium Designer集成库简介及创建

    一.集成库概述    Altium Designer 采用了集成库的概念.在集成库中的元件不仅具有原理图中代表元件的符号,还集成了相应的功能模块.如Foot Print 封装,电路仿真模块,信号完整性 ...

  3. 陆金所退出市场,我说:趁现在,抓紧离开P2P市场,你赞同吗?

    编辑 | 于斌 出品 | 于见(mpyujian) 18日,也就是前天,陆金所退出P2P市场的消息就像颗"重磅炸弹"一样,一波激起千层浪,陆金所作为全国最大财富平台之一,这次退出, ...

  4. 【Vue常用指令】

    目录 v-html v-text v-for v-if v-show v-bind v-on v-model 指令修饰符 计算与侦听属性 自定义属性 获取DOM元素 "@ *** Vue.j ...

  5. JS中的解构

    先看看数组解构: function fn(){ return [1,2,3]; } var [a,b,c] = fn(); console.log(a,b,c); // 1 2 3 var [d,,f ...

  6. Flask 学习之flask入门

    一.Flask的简单介绍 Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请 ...

  7. IOS上给body和html绑定click事件的坑

    场景: 在ios上(包括iPhone和ipad) 给window ,html,document,body绑定click事件,点击不会触发 由于ios浏览器都用的safari内核,所以ios浏览器全部中 ...

  8. 吴裕雄 python 神经网络——TensorFlow 训练过程的可视化 TensorBoard的应用

    #训练过程的可视化 ,TensorBoard的应用 #导入模块并下载数据集 import tensorflow as tf from tensorflow.examples.tutorials.mni ...

  9. Android学习02

    今天学了ScrollView&HorizontalScrollView和WebView 一.ScrollView(垂直滚动),HorizontalScrollView(水平滚动) Scroll ...

  10. P1426

    和上次的小鱼题差不多,但多了一些条件. 先把游到 $ s - x $ 米是第 $ a_i $ 秒求出来,然后判断之后在第 $ a_{i + 1} $ 秒内游的距离是否 $ \geq 2x $ ,大于就 ...