十三 Struts2复杂类型的数据封装,List封装和Map封装
在实际开发当中,有可能遇到批量向数据库中插入记录,需要在页面中将数据封装到集合中。类似页面表达式方法
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封装的更多相关文章
- Struts2中将表单数据封装到List和Map集合中
一.将表单数据封装到Map集合中 1.创建MapAction类 import cn.entity.User; import com.opensymphony.xwork2.ActionSupport; ...
- struts2 获取表单数据封装到list和map集合
一.获取封装表单数据到list集合 示例 获取用户输入的用户名和密码并输出用户名. jsp页面 list[0]表示list中的第一个user对象 Java代码 二.封装表单数据到map集合 示例 获取 ...
- struts2(三)之表单参数自动封装与参数类型自动转换
前言 对struts2的使用不外乎这几点,参数自动封装,拦截器的使用,数据校验,ognl表达(值栈和actionContext的讲解),struts2的标签,struts2的国际化, struts2的 ...
- Struts2把数据封装到集合中之封装到map中
struts框架封装数据可以封装到集合中也可以封装到map中,该篇博客主要讲解将数据封装到map中. 1. 封装复杂类型的参数(集合类型 Collection .Map接口等) 2. 需求:页面中有可 ...
- Struts2把数据封装到集合中之封装到Collection中
数据封装到集合中,可以封装到集合中,也可以封装到Map中.该篇博客主要讲解数据封装到集合中的封装到Collection中. 1. 封装复杂类型的参数(集合类型 Collection .Map接口等) ...
- Struts2框架的数据封装一之属性封装(属性封装的第二种方式:封装成javaBean)
Struts2中提供了两类数据封装的方式? 第一种方式:属性驱动(有两种方式:一个对属性,另外一个是将参数封装到javaBean中) B. 在页面上,使用OGNL表达式进行数据封装.(将参数封装到ja ...
- struts2学习笔记之十一:struts2的类型转换器
Struts2的类型转换器 如何实现Struts2的类型转换器? * 继承StrutsTypeConverter * 覆盖convertFromString和convertToString 注 ...
- struts2结果类型
struts2结果类型: 结果类型 描述 前request域属性是否丢失 1 dispatcher 用于与jsp整合的结果类型.默认结果类型. 2 chain Action链式处理结果类型.前一个Ac ...
- struts2自定义类型转换器
首先,何为struts2的类型转换器? 类型转换器的作用是将请求中的字符串或字符串数组参数与action中的对象进行相互转换. 一.大部分时候,使用struts2提供的类型转换器以及OGNL类型转换机 ...
随机推荐
- HTML5地理定位(已知经纬度,计算两个坐标点之间的距离)
事实上,地球上任意两个坐标点在地平线上的距离并不是直线,而是球面的弧线. 下面介绍如何利用正矢公式计算已知经纬度数据的两个坐标点之间的距离.半正矢公式也成为Haversine公式,它最早时航海学中的重 ...
- 201771010135 杨蓉庆《面对对象程序设计(java)》第十八周学习总结
1.实验目的与要求 (1) 综合掌握java基本程序结构: (2) 综合掌握java面向对象程序设计特点: (3) 综合掌握java GUI 程序设计结构: (4) 综合掌握java多线程编程模型: ...
- SPAN, RSPAN, ERSPAN
该文档摘自:Home > CCIE Routing and Switching Study Group > Discussions 由 Deben 于 2015-2-6 上午6:50 创建 ...
- MySQL 5.5.62 安装方法(标准配置版)
1.此安装方法适用于绝大多数MySQL版本,首先在MySQL官网上下载好所需版本. 2.(官网可能不太好找)在我的博客列表中有一篇是MySQL官网下载链接,直达下载界面,方便. 3.下载.(安装版 ...
- js中的局部函数和全局函数的调用
//局部函数和全局函数的特点 function fc1(){ var name ="chenhao"; function fc2(){ var age = 30; alert(na ...
- MySQL死锁1
MySQL行级排他锁的使用及死锁解除技巧 这篇笔记存粹是做学习记录之用,方便将来查阅,老鸟请跳过.关于MySQL排他锁的具体使用. 使用排他锁 假设有一张user表如下: id name age 1 ...
- vue-lazyload 的使用(vue图片懒加载)
github地址:https://github.com/hilongjw/vue-lazyload npm i vue-lazyload -S 或者 <script src="http ...
- QLabel设置伙伴关系和快捷键(Alt+首字母)
Qlabe中设置伙伴关系是使用Setbuddy函数: Qlabel.Setbuddy(QLineEdit) #将Qlabel和QLineEdit之间设置伙伴关系 另外,需要配合热键(快捷键)进行使用, ...
- windows 删除进程
win+R打开doc窗口 netstat -ano |findstr "8888" tskill 10120 结束进程
- Kubernetes的pod控制器及ReplicaSet控制器类型的pod的定义
为什么需要Pod Kubernetes项目之所以这么做的原因: 因为Kubernetes是谷歌公司基于Borg项目做出来的,谷歌工程师发现,他们部署的应用往往存在这进程与进程组的关系.具体说呢,就是这 ...