這段時間忙的要死,做項目,學框架,時間根本不夠用,只能盡量抽出時間記錄自己學過的東西。
1.1、doubleSelect
在之前學習中,我們使用過二級列表,相信很多人都理解其原理,在struts中,同樣也為我們准備了二級列表,使用doubleSelect就能夠時間
進入主題
1、搭建環境(這裡筆者使用的struts框架是2.3的,傳送門)

2、配置struts2.3環境
2.1、web.xml配置過濾器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>LearStruts2_4</display-name>
<welcome-file-list>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<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>
2.2、在WEB-INF中新建class文件夾,文件夾下新建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>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="doubleselectTag" class="com.struts.ui.action.DoubleSelectTagAction">
<result name="success">/doubleSelectTag.jsp</result>
</action>
</package>
</struts>
3、新建實體類

package com.struts.ui.action;
public class City {
private int id;
private String name;
public City(){
}
public City(int id,String name){
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
City

package com.struts.ui.action;
public class Provice {
private int id;
private String name;
public Provice(){
}
public Provice(int id,String name){
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Provice
4、新建DoubleSelectTagAction類繼承ActionSupport
package com.struts.ui.action;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.ValueStack;
/**
* 使用doubleSelect 二級聯下拉,配置環境是struts2.3 tomcat8.0
* 相關的類有:
* City.java
* Provice.java
* doubleSelectTag.jsp
* DoubleSelectTagAction.java
* 訪問:http://localhost:8080/LearStruts2_4/doubleselectTag
* 目的:簡單就是選擇一級列表之後出現二級菜單選項
* 這裡使用的是傳遞對象的id到jsp頁面,之後通過id查找
* */
public class DoubleSelectTagAction extends ActionSupport {
private List<Provice> provices;// Provice對象的列表
private Map<Integer, List<City>> cityMap;// 以Provice對象為key,Provice對應的City對象的列表作為value
public Map<Integer, List<City>> getCityMap() {
return cityMap;
}
public DoubleSelectTagAction(){
provices = new ArrayList<Provice>();
Provice provice1 = new Provice(1,"四川省");
Provice provice2 = new Provice(2,"山東省");
Provice provice3 = new Provice(3,"湖南省");
Provice provice4 = new Provice(4,"廣東省");
provices.add(provice1);
provices.add(provice2);
provices.add(provice3);
provices.add(provice4);
List<City> cities1 = new ArrayList<City>();
List<City> cities2 = new ArrayList<City>();
List<City> cities3 = new ArrayList<City>();
List<City> cities4 = new ArrayList<City>();
cities1.add(new City(1,"成都市"));
cities1.add(new City(2,"綿陽市"));
cities2.add(new City(1,"濟南市"));
cities2.add(new City(2,"青島市"));
cities3.add(new City(1,"長沙市"));
cities3.add(new City(2,"郴州市"));
cities4.add(new City(1,"廣州市"));
cities4.add(new City(2,"深圳市"));
cityMap = new HashMap<Integer,List<City>>();
cityMap.put(provice1.getId(), cities1);
cityMap.put(provice2.getId(), cities2);
cityMap.put(provice3.getId(), cities3);
cityMap.put(provice4.getId(), cities4);
}
public String execute() throws Exception {
return SUCCESS;
}
public List<Provice> getProvices(){
return provices;
}
}
5、新建jsp視圖頁面顯示數據
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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>
<s:form action="doubleselectTag">
<s:doubleselect label="請選擇所在省市" name="provice" list="provices"
listKey="id" listValue="name" doubleList="cityMap.get(top.id)" doubleListKey="id"
doubleListValue="name" doubleName="city"
headerValue="----請選擇----" emptyOption="true" />
</s:form>
<!--
需要注意的點:
1、list 是doubleSelectTagAction中第一個泛型集合的名稱
2、listKey 是City類的屬性id
3、listValue 是City類的屬性name
4、doubleList 是讀取map集合,top是當前list中集合的對象,注意 cityMap.get(top.id)中的id和listkey的值必須相同,cityMap是集合名稱
5、doublelistkey 是map集合對象的id
如果DoubleSelectTagAction中 map集合存放的鍵是對象的話,那麼doubleList就必須改為對象【doubleList="cities"】
-->
</body>
</html>
注意點:此處是map集合的鍵是id,所以使用到cityMap.get(top.id),下面這種是map集合為對象的時候,代碼只是有點不同。
DoubleSelectTagAction
jsp頁面
doubleSelectTag.jsp
效果如下:效果是有的,是筆者的錄制軟件的問題

總結:
原理跟二級列表類似,需要注意的點是:當使用id時,必須保證listkey=id 和 doubleList= citymap.get(top.id) 中的top.id相同。代碼親測,可以使用。
筆者自己看的博客