Struts2的复杂数据类型的封装,封装数据到List集合中,封装数据到Map集合当中
发布日期:2021-06-29 15:01:57 浏览次数:2 分类:技术文章

本文共 1765 字,大约阅读时间需要 5 分钟。

Struts2的复杂数据类型的封装

在实际的开发当中有可能会遇到批量的向数据库当中插入记录,这个时候需要在页面当中将数据封装到集合当中。
Struts2的复杂数量类型的数据封装。

一、封装数据到List集合中

1、搭建JSP页面

<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%>
Insert title here

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

封装到List集合当中:批量插入商品

商品名称:
商品价格:
商品名称:
商品价格:
商品名称:
商品价格:

2、编写Action

package com.itzheng.struts2.demo3;import java.util.List;import com.itzheng.struts2.domain.Product;import com.opensymphony.xwork2.ActionSupport;/* * 复杂类型的数据封装:封装到List集合 */public class ProductAction1 extends ActionSupport {
private List
products; // 提供集合的set方法 public List
getProducts() {
return products; } public void setProducts(List
products) {
this.products = products; } @Override public String execute() throws Exception {
// TODO Auto-generated method stub System.out.println("=================="); for (Product product : products) {
System.out.println(product); } return NONE; }}

3、相关配置文件

在这里插入图片描述

测试:

在这里插入图片描述
在这里插入图片描述

二、封装数据到Map集合当中

1、搭建JSP页面

在这里插入图片描述

2、创建Action

package com.itzheng.struts2.demo3;import java.util.Map;import com.itzheng.struts2.domain.Product;import com.opensymphony.xwork2.ActionSupport;/* * 复杂数据类型的封装,封装到Map集合 */public class ProductAction2 extends ActionSupport {
private Map
map; public Map
getMap() {
return map; } public void setMap(Map
map) {
this.map = map; } @Override public String execute() throws Exception {
// TODO Auto-generated method stub for (String key : map.keySet()) {
Product product = map.get(key); System.out.println(key + " " + product); } return NONE; }}

3、设置好配置文件以后进行测试

在这里插入图片描述

在这里插入图片描述

转载地址:https://code100.blog.csdn.net/article/details/106764888 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Mysql计算月份差
下一篇:Struts的数据的封装,属性驱动:提供属性set方法的方式,属性驱动:页面中提供一种表达式,模型驱动:采用模型驱动的方式,INPUT的逻辑视图的配置

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月09日 21时03分57秒