Struts2的复杂类型的数据封装

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="提交">
</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>

Product.java

package com.cherry.struts2.domain;
/**
 * 商品的实体
 * @author zhang
 *
 */
public class Product {
	private String name;
	private Double price;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Double getPrice() {
		return price;
	}
	public void setPrice(Double price) {
		this.price = price;
	}
	@Override
	public String toString() {
		return "Product [name=" + name + ", price=" + price + "]";
	}
	
	
}

ProductAction1.java

package com.cherry.struts2.demo3;

import java.util.List;

import com.cherry.struts2.domain.Product;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 复杂类型的数据封装:封装到List集合
 * @author zhang
 *
 */
public class ProductAction1 extends ActionSupport {

	private List<Product> products;
	//提供集合的set方法
	public void setProducts(List<Product> products) {
		this.products = products;
	}
	
	
	public List<Product> getProducts() {
		return products;
	}


	@Override
	public String execute() throws Exception {
		for (Product product : products) {
			System.out.println(product);
		}
		return NONE;
	}

	
}

ProductAction2.java

package com.cherry.struts2.demo3;

import java.util.Map;

import com.cherry.struts2.domain.Product;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 复杂数据类型的封装:封装到Map集合
 * @author zhang
 *
 */
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;
	}


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

struts_demo3.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
	<package name="demo3" extends="struts-default" namespace="/">
		<action name="productAction1" class="com.cherry.struts2.demo3.ProductAction1">
		</action>
		<action name="productAction2" class="com.cherry.struts2.demo3.ProductAction2">
		</action>
	</package>
	
</struts>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
	<!-- 配置Struts2的常量 -->
	<constant name="struts.action.extendsion" value="action"></constant>
	
	
	<include file="com/cherry/struts2/demo3/struts_demo3.xml"></include>
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>struts2_day02</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- 配置核心过滤器 -->
  <filter>
  		<filter-name>struts</filter-name>
  		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  
  <filter-mapping>
  		<filter-name>struts</filter-name>
  		<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值