1. 程式人生 > >使用c:forEach模擬s:select標籤

使用c:forEach模擬s:select標籤

一、兩種實現方式的效果圖

二、關鍵部分的程式碼

Struts2方式

<s:select name="student.province" list="dictionary.provinceList" listKey="key" listValue="value"></s:select>

JSTL方式
<select name="student.province">
	<c:forEach items="${dictionary.provinceList }" var="list">
		<option value="${list.key }">${list.value }</option>
	</c:forEach>
</select>

三、完整步驟

1.程式碼結構


(1)配置檔案

web.xml,struts.xml

(2)相關jar包

位於目錄WebRoot/WEB-INF/lib目錄下

(3)後臺程式碼 com.compname包

action部分:StudentAction.java

po部分:SelectData.java(存放單個下拉列表的鍵和值),Dictionary.java(將多個下拉列表封裝到一個實體類中)

(4)前端頁面相關

student_list.jsp,jquery-1.11.0.js

2.實現程式碼

(1)配置檔案

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>
  		org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
  	</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<constant name="struts.ui.theme" value="simple" />
	<package name="basicStruts" extends="struts-default" namespace="/studentMgmt">
		<action name="initQueryPage" class="com.compname.action.StudentAction"
			method="initQueryPage">
			<result name="success">/studentMgmt/student_list.jsp</result>
		</action>	
	</package>
</struts>
(2)用到的jar包(需要Struts2和jstl的相應支援jar包)

(3)後臺程式碼

StudentAction.java

package com.compname.action;

import java.util.ArrayList;
import java.util.List;

import com.compname.po.Dictionary;
import com.compname.po.SelectData;

public class StudentAction {
	private Dictionary	dictionary;
	
	/**
	 * 初始化查詢頁面
	 * 
	 * @return
	 */
	public String initQueryPage() {
		dictionary = new Dictionary();
		List<SelectData> list = new ArrayList<SelectData>();
		list.add(new SelectData("1", "北京"));
		list.add(new SelectData("2", "上海"));
		list.add(new SelectData("3", "廣州"));
		list.add(new SelectData("4", "深圳"));
		list.add(new SelectData("5", "河北"));
		list.add(new SelectData("6", "河南"));
		list.add(new SelectData("7", "山西"));
		list.add(new SelectData("8", "山東"));
		list.add(new SelectData("9", "江蘇"));
		list.add(new SelectData("10", "浙江"));
		dictionary.setProvinceList(list);
		return "success";
	}
	
	public Dictionary getDictionary() {
		return dictionary;
	}
	
	public void setDictionary(Dictionary dictionary) {
		this.dictionary = dictionary;
	}
}

SelectData.java

package com.compname.po;

/**
 * 
 * @ClassName: SelectData
 * @Description: TODO(存放鍵值對)
 * @date 2017-10-26 下午10:47:36
 */
public class SelectData {
	private String	key;	// 鍵
	private String	value;	// 值
							
	public SelectData(String key, String value) {
		super();
		this.key = key;
		this.value = value;
	}
	
	public String getKey() {
		return key;
	}
	
	public void setKey(String key) {
		this.key = key;
	}
	
	public String getValue() {
		return value;
	}
	
	public void setValue(String value) {
		this.value = value;
	}
	
}
Dictionary.java
package com.compname.po;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @ClassName: Dictionary
 * @Description: TODO(用於存放所有的鍵值對資料)
 * @date 2017-10-26 下午10:47:36
 */
public class Dictionary {
	private transient List<SelectData>	provinceList	= new ArrayList<SelectData>();
	
	public List<SelectData> getProvinceList() {
		return provinceList;
	}
	
	public void setProvinceList(List<SelectData> provinceList) {
		this.provinceList = provinceList;
	}
}
(4)前端頁面相關

student_list.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
	<title>My JSP 'student_list.jsp' starting page</title>
	<script type="text/javascript" src="<%=path%>/js/jquery/jquery-1.11.0.js"></script>
	<style type="text/css">
		table tr td {
			border: 1px solid black;
		}
	</style>
  </head>
  <body>
  	<form action="">
	  	<table>
  			<tr>
	  			<td>struts2中的select標籤</td>
	  			<td>使用jstl中的foreach模擬實現strusts2中的select標籤</td>
  			</tr>
  			<tr>
	  			<td>
	  				<s:select name="student.province" list="dictionary.provinceList" listKey="key" listValue="value"></s:select>
	  			</td>
	  			<td>
					<select name="student.province">
						<c:forEach items="${dictionary.provinceList }" var="list">
							<option value="${list.key }">${list.value }</option>
						</c:forEach>
					</select>
	  			</td>
  			</tr>
	  	</table>
  	</form>
  </body>
</html>

說明:如果不想在struts.xml中全域性配置struts標籤的樣式(第6行)

<constant name="struts.ui.theme" value="simple" />
只需要將指定 前端頁面student_list.jsp中的select標籤

(第29行)

<s:select name="student.province" list="dictionary.provinceList" listKey="key" listValue="value"></s:select>

改成

<s:select name="student.province" list="dictionary.provinceList" listKey="key" listValue="value" theme="simple"></s:select>