1. 程式人生 > >struts2[2.3]引數獲得方式-(4)集合型別引數封裝(list和map)

struts2[2.3]引數獲得方式-(4)集合型別引數封裝(list和map)

1.學習路線

今天咱們來學struts2引數獲得方式,let`go!

                                                                                           圖1.學習路線

                                                                                           圖2.類和配置檔案

form4.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>
		<form action="${pageContext.request.contextPath}/Demo11Action" method="post">
			list:<input type="text" name="list"><br>
			<!-- 可提交到集合的指定索引上 -->
			list:<input type="text" name="list[1]"><br>
			<!-- 把表單上的值提交到鍵為:haha的map上 -->
			map:<input type="text" name="map['haha']"><br>
			
			<input type="submit" value="提交" />
		</form>
</body>
</html>

2.獲得引數

新建一個Demo11Action類,繼承ActionSupport,再建立一個execute()方法,return SUCCESS(建立集合型別屬性的Getters()和Setters()方法)。

package cn.aisino.c_param;

import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.aisino.domain.User;	
//struts2封裝集合型別引數
public class Demo11Action extends ActionSupport{

	private List<String> list;
	private Map<String,String> map;
	
	public String execute() throws Exception {
	
		System.out.println("list:"+list);
		System.out.println("map:"+map);
		return SUCCESS;
	}
	
	public List<String> getList() {
		return list;
	}

	public void setList(List<String> list) {
		this.list = list;
	}
	
	public Map<String, String> getMap() {
		return map;
	}

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

}

    在包下的配置檔案struts.xml中配置Action,

    在主配置檔案struts.xml中配置,

    啟動伺服器,在位址列中訪問form4.jsp,輸入資料:

    提交,控制檯可檢視資料成功提交: