struts2.1.8 中怎么做级联下拉列表
1个回答
2013-04-08
展开全部
doubleselect算是Struts2中稍微复杂点的表单标签, 尤其官方示例比较简单, 没有太大的实用价值.<s:doubleselectlabel="doubleselect test2" name="menu"list="#{'fruit':'Nice Fruits', 'other':'Other Dishes'}"doubleName="dishes" doubleList="top == 'fruit' ? {'apple','orange'} : {'monkey', 'chicken'}" />从这个官方示例中可以看到基本用法, list开头的代表的是第一级下拉框的属性, doubleList开头的则是第二级下拉框. 与select标签的属性基本是对应的.在实际应用中, 我们比较关注的就是list和doubleList这两个属性了, list是存放的第一级下拉框的数据集合, 比如List<FatherBean>, 而doubleList就可以用Map<String,List<SonBean>>来存储, 其中map的键名String与第一级下拉框相关联.现在来展示一个我写的示例:先写两个javaBean,一般情况下是跟数据库的表相关联的:package com.dxm.entity;import java.util.HashSet;import java.util.Set; public class Item implements java.io.Serializable { private static final long serialVersionUID = 1L; private int itemid; private String itemname; private int itemcode; private Set subitems = new HashSet(0); //set和get方法} package com.dxm.entity; public class Subitem implements java.io.Serializable { private static final long serialVersionUID = 1L; private int subid; private Item item; private String subname; private int subcode;//set和get方法}再来写Action,用来往web页面发送数据:package com.dxm.action;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import com.dxm.dao.ItemDAO;import com.dxm.entity.Item;import com.dxm.entity.Subitem;import com.opensymphony.xwork2.ActionSupport; public class QuestionItemAction extends ActionSupport { private static final long serialVersionUID = 1L; private List<Item> itemList; private Map<Integer, List<Subitem>> subitemMap; private ItemDAO itemDao; private int itemid;//get/set方法 public String execute() { // TODO Auto-generatedmethod stub itemList = new ArrayList<Item>(); try { itemList = this.itemDao.getAll(); } catch (Exception e) { e.printStackTrace(); } subitemMap = new HashMap<Integer, List<Subitem>>(); for (int i = 0; i < itemList.size(); i++) { List subitemList = new ArrayList(itemList.get(i).getSubitems()); itemid = itemList.get(i).getItemid(); subitemMap.put(itemid, subitemList); } return SUCCESS; }}再写接口package com.dxm.dao; import java.util.List; import com.dxm.entity.Item; public interface ItemDAO { //列出所有栏目 public List<Item> getAll() throws Exception; }再写实现类:package com.dxm.dao.impl; import java.util.List;import org.hibernate.Query;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.dxm.dao.ItemDAO;import com.dxm.entity.Item; public class ItemDAOImplextends HibernateDaoSupport implements ItemDAO { public List getAll() throws Exception { String hql="FROM Item ASitem"; List all=super.getSession().createQuery(hql).list(); return all; }}现在写配置文件applicationContext.xml<bean name="questionitemActionBean" class="com.dxm.action.QuestionItemAction"> <property name="itemDao"> <ref bean="itemDao"/> </property> </bean>Struts.xml<action name="questionitem"class="questionitemActionBean"> <result name="success">/jsp/ques/question.jsp</result> <result name="error">/jsp/errors.jsp</result> </action>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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file>index.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>*.action</url-pattern> <dispatcher>FORWARD</dispatcher> <dispatcher>REQUEST</dispatcher> </filter-mapping> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.jsp</url-pattern> <dispatcher>FORWARD</dispatcher> <dispatcher>REQUEST</dispatcher> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:applicationContext*.xml </param-value> </context-param></web-app>此处要注意:必须要加上<dispatcher>FORWARD</dispatcher><dispatcher>REQUEST</dispatcher>不然后面转向页面会出错最后写显示级联下拉菜单的web页面:Index.jsp<%@ page language="java"import="java.util.*" pageEncoding="utf-8"%><%String path =request.getContextPath();String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <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> <jsp:forward page="questionitem"/> </body></html> Question.jsp<%@ page language="java"contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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>struts2级联下拉菜单示例</title>
</head>
<body><s:formname="f1">
栏目 <s:doubleselect name="itemid"list="itemList" listKey="itemid"listValue="itemname" doubleName="subitemid" doubleList="subitemMap.get(top.itemid)"doubleListKey="itemid" doubleListValue="subname"/> <br>
</s:form>
</body>
</html>此处要注意的是top的用法,top代表的就是list当前选中的对象, 所以top.id对应的就是当前选中的Province对象中的ID, cityMap.get(top.id)即根据当前选中的Province对象中的ID来取出第二级下拉框的数据集合
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!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>struts2级联下拉菜单示例</title>
</head>
<body><s:formname="f1">
栏目 <s:doubleselect name="itemid"list="itemList" listKey="itemid"listValue="itemname" doubleName="subitemid" doubleList="subitemMap.get(top.itemid)"doubleListKey="itemid" doubleListValue="subname"/> <br>
</s:form>
</body>
</html>此处要注意的是top的用法,top代表的就是list当前选中的对象, 所以top.id对应的就是当前选中的Province对象中的ID, cityMap.get(top.id)即根据当前选中的Province对象中的ID来取出第二级下拉框的数据集合
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询