SSH二級聯動
寫了三座省份 每個省份中包含三座城市
CityAction.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import com.demo06.model.City;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ognl.OgnlValueStack;
public class CityAciton extends ActionSupport{
private static final long serialVersionUID = -2707010591753056281L;
/**創建ArrayList集合保存省份*/
private ArrayList<City> citys = new ArrayList<>();
/**創建HashMap集合保存省份對應的城市*/
private HashMap<City,ArrayList<City>> subCitys = new HashMap<>();
/**根據省份獲取對應的城市列表*/
@SuppressWarnings("unchecked")
public ArrayList<City> getSubCity(){
/**獲取當前值棧*/
OgnlValueStack vs = (OgnlValueStack) ActionContext.getContext().getValueStack();
/**獲取棧頂元素*/
Object subcity = vs.findValue("top");
if(subcity != null && subcity instanceof City){
return subCitys.get(subcity);
}
return (ArrayList<City>) Collections.EMPTY_LIST;
}
public ArrayList<City> getCitys() {
return citys;
}
public void setCitys(ArrayList<City> citys) {
this.citys = citys;
}
public HashMap<City, ArrayList<City>> getSubCitys() {
return subCitys;
}
public void setSubCitys(HashMap<City, ArrayList<City>> subCitys) {
this.subCitys = subCitys;
}
@Override
public String execute() throws Exception {
/**創建第一個省份*/
City city1 = new City(1,"山西省");
citys.add(city1);
ArrayList<City> subCity = new ArrayList<>();
subCity.add(new City(11,"太原市"));
subCity.add(new City(12,"大同市"));
subCity.add(new City(13,"陽泉市"));
subCitys.put(city1, subCity);
/**創建第二個省份*/
City city2 = new City(2,"河北省");
citys.add(city2);
subCity = new ArrayList<>();
subCity.add(new City(21,"石家莊市"));
subCity.add(new City(22,"廊坊市"));
subCity.add(new City(23,"保定市"));
subCitys.put(city2, subCity);
/**創建第三個省份*/
City city3 = new City(3,"陝西省");
citys.add(city3);
subCity = new ArrayList<>();
subCity.add(new City(31,"咸陽市"));
subCity.add(new City(32,"西安市"));
subCity.add(new City(33,"寶雞市"));
subCitys.put(city3, subCity);
return "success";
}
}
struts
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="TagLibDemo" extends="struts-default">
<action name="cityAction" class="com.demo06.action.CityAciton">
<result name="success">/city.jsp</result>
</action>
</package>
</struts>
city.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>城市集合</title>
</head>
<body>
<s:form aciton="" method="post" name="form1">
<s:doubleselect doubleList="subCity"
list = "citys"
doubleName="city1"
name="citys1"
formName="form1"
listKey="name"
listValue="name"
doubleListKey="name"
doubleListValue="name"
label="地址"
/>
</s:form>
</body>
</html>
完成最基本的二級聯動