struts2 简单的 省–市二级级联菜单
学了struts2的朋友一定知道,在struts2的标签库中有一个<s:doubleselect>标签,该标签为我们提供了一个
简单的二级级联菜单。有朋友肯定就要问了,既然标签库已经提供了实现这个功能的方法,那么为什么还要啰
嗦呢。的确,struts2是为我们提供了实现这个功能的方法,但它提供的方法只是一个静态的方法,说具体一
点呢,就是<doubleselect>所提供的方法中,它顶部的下拉列表只有两个。也就是说,<doubleselect>只能让
我们实现二选一的级联,显然,这一点是不能让我们满足的。这里简单的介绍了一个实现省–市的二级级联
菜单,当然,这里并没有将所有的省及其相对应的市都写出来,但相应的功能大致实现了。朋友们可以参照下
面的方法将菜单完善。好了,废话不多说,下面我们进入正题:
1.先创建两个文件,Provice.java和City.java两个bean文件,用于设置和获取省、市。代码如下:
————————————————————————————
Provice.java
package com.caott.tags;
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; } }
————————————————————————————
City.java
package com.caott.tags;
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; }
} *******************************************************************
2.创建实现控制和响应的Action文件:DoubleSelectTagAction.java 完整代码如下:
————————————————————————–
DoubleSelectTagAction.java
package com.caott.tags;
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.Action; import com.opensymphony.xwork2.util.ValueStack;
public class DoubleSelectTagAction implements Action {
private List<Provice> provices;// Provice对象的列表
private Map<Provice, List<City>> cityMap;// 以Provice对象为key,Provice对应的City对象的列表作为value 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<Provice,List<City>>(); cityMap.put(provice1, cities1); cityMap.put(provice2, cities2); cityMap.put(provice3, cities3); cityMap.put(provice4, cities4); }
public String execute() throws Exception { return SUCCESS; } public List<Provice> getProvices(){ return provices; } @SuppressWarnings(“unchecked”) public List<City> getCities(){ ValueStack stack = ServletActionContext.getValueStack(ServletActionContext.getRequest()); Object provice = stack.findValue(“top”);//获取栈顶对象 if(provice != null && provice instanceof Provice){ List<City> lst = (List<City>)cityMap.get(provice); return lst; } return Collections.EMPTY_LIST; }
} ——————————————————————————
3.接下来就要创建一个视图页面了:doubleseletTag.jsp 代码如下:
<%@ page language=”java” import=”java.util.*” pageEncoding=”utf-8″%> <%@taglib prefix=”s” uri=”/struts-tags”%> <% 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> <base href=”<%=basePath%>”>
<title>省份–城市级联菜单</title> <s:head /> <meta http-equiv=”pragma” content=”no-cache”> <meta http-equiv=”cache-control” content=”no-cache”> <meta http-equiv=”expires” content=”0″> <meta http-equiv=”keywords” content=”keyword1,keyword2,keyword3″> <meta http-equiv=”description” content=”This is my page”> <!– <link rel=”stylesheet” type=”text/css” href=”styles.css”> –>
</head>
<body> <s:form action=”doubleselectTag”> <s:doubleselect label=”请选择所在省市” name=”provice” list=”provices” listKey=”id” listValue=”name” doubleList=”cities” doubleListKey=”id” doubleListValue=”name” doubleName=”city” headerKey=”-1″ headerValue=”—-请选择—-” emptyOption=”true” /> </s:form> </body> </html> —————————————————————-
4.视图页面完成后,不要忘记在struts中配置相应的action 。关键代码如下:
<action name=”doubleselectTag” > <result> /doubleSelectTag.jsp </result> </action>
————————————————————-
至此,我们基本上完成了级联菜单的功能了。接下来我们要查看一下功能是否真的实现了。启动tomcat服务器,部署工程。(当然,不要忘记查看一下web.xml文件中是否配置了struts的过滤器)
以上工作都完成之后,打开浏览器,在地址栏输入:
http://localhost:8080/ch04/doubleselectTag
注意,这里的ch04是本人建立的工程名。
页面显示结果如下图所示:
图1
![struts2 <wbr>简单的 <wbr> <wbr>省--市二级级联菜单 struts2 <wbr>简单的 <wbr> <wbr>省--市二级级联菜单]()
图2: