Struts2复杂数据的封装(封装到List和Map)

 

本次没什么小坑

----------------------------------------------------------------以下是代码实例--------------------------------------------

struts.xml引入以下代码

以下是demo1.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>
<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="提交"><br>
</form>
<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>

以下是ProductAction1的代码

package demo3;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

import domin.Product;

public class ProductAction1 extends ActionSupport {

private static final long serialVersionUID = 8876088464977928417L;

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 {
for(Product product : products) {
System.out.println(product);
}
return super.execute();
}
}

以下是ProductAction2的代码

 

package demo3;

import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;

import domin.Product;

public class ProductAction2 extends ActionSupport {

private static final long serialVersionUID = 8876088464977928417L;

private Map<String, Product> map;

public Map<String, Product> getMap() {
return map;
}

public void setMap(Map<String, Product> map) {
this.map = map;
}

@Override
public String execute() throws Exception {
for (String key : map.keySet()) {
Product product = map.get(key);
System.out.println(key + " " + product);
}
return super.execute();
}
}

struts_demo03.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts SYSTEM "http://struts.apache.org/dtds/struts-2.5.dtd" >
<struts>
<package name="demo3" extends="struts-default" namespace="/">
<global-results>
<result>/demo1/demo2.jsp</result>
</global-results>
<action name="productAction1" class="demo3.ProductAction1">
</action>
<action name="productAction2" class="demo3.ProductAction2">
</action>
</package>
</struts>

转载于:https://www.cnblogs.com/PengeStudy/p/10266264.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值