Struts2本身提供了级联下拉选择框标签,这对简化诸如省份/城市这样的典型需求的操作是大有好处的,同时也有利于架构的统一,没有必要引入一大堆的javascript代码,使用方法如下:
1、Action层
DoubleListAction.java
package com.mobilesoft.esales.webapp.action; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import org.apache.log4j.Logger; import com.mobilesoft.framework.tree.model.FileWrapper; import com.mobilesoft.framework.tree.model.OssCategory; import com.mobilesoft.framework.tree.service.TreeService; public class DoubleListAction extends BaseAction { /** * Logger for this class */ private static final Logger logger = Logger.getLogger(DoubleListAction.class); public String execute() { Map map=new HashMap(); ArrayList list1=new ArrayList(); list1.add("11"); list1.add("12"); list1.add("13"); map.put("1", list1); ArrayList list2=new ArrayList(); list2.add("21"); list2.add("22"); list2.add("23"); map.put("2", list2); ArrayList list3=new ArrayList(); list3.add("31"); list3.add("32"); list3.add("33"); map.put("3", list3); getRequest().setAttribute("map", map); return SUCCESS; } public String doubleSelectTest(){ Map map=new HashMap(); ArrayList list1=new ArrayList(); list1.add("11"); list1.add("12"); list1.add("13"); map.put("1", list1); ArrayList list2=new ArrayList(); list2.add("21"); list2.add("22"); list2.add("23"); map.put("2", list2); ArrayList list3=new ArrayList(); list3.add("31"); list3.add("32"); list3.add("33"); map.put("3", list3); getRequest().setAttribute("map", map); return SUCCESS; } public String doubleSelectPost(){ Map map=getRequest().getParameterMap(); Set set=map.entrySet(); Iterator iterator=set.iterator(); while(iterator.hasNext()){ Map.Entry mapEntry=(Map.Entry)iterator.next(); logger.fatal("The key is: "+mapEntry.getKey()+",value is :"+mapEntry.getValue()); } getRequest().setAttribute("map", map); return SUCCESS; } }
2、页面
doubleslect.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <%@ page language="java" errorPage="/error.jsp" pageEncoding="GBK" contentType="text/html;charset=GBK" %> <html> <head> <title>Struts 2 Cool Tags - <s:doubeselect/ ></title> <s:head /> </head> <body> <h2>Doubleselect Tag Example</h2> <s:form action="doubleSelectPost" name="form1"> Doubleselect演示1 <s:set name="technology" value="#{ 'Java': {'Spring', 'Hibernate', 'Struts 2'}, '.Net': {'Linq', ' ASP.NET 2.0'}, 'Database': {'Oracle', 'SQL Server', 'DB2', 'MySQL'} }" /> <s:doubleselect label="技术选择" list="#technology.keySet()" name="doubleselect1" doubleName="techdetail" formName="form1" doubleList="#technology[top]" /> <br/><br/> Doubleselect演示数据演示2: <s:set name="mymap" value="#request.map"/> <s:doubleselect list="#request.map.keySet()" doubleName="mymap1" name="doubleselect2" formName="form1" doubleList="#request.map[top]" label="Map Test" /> <s:submit/> </s:form> </body> </html>
doubleselectpost.jsp
<%@ taglib prefix="s" uri="/struts-tags" %> <%@ page language="java" errorPage="/error.jsp" pageEncoding="GBK" contentType="text/html;charset=GBK" %> <html> <head> <title>Struts 2 Cool Tags - <s:doubeselect/ ></title> <s:head /> </head> <body> <h2>Doubleselect Tag Example</h2> <s:form name="form1"> <table border="0" cellspacing="0" cellpadding="1"> <tr> <th>参数值</th> </tr> <s:iterator value="#request.map.keySet()" status="keys"> <tr> <td><s:property/></td> </tr> </s:iterator> </s:form> </body> </html>
3、struts.xml配置文件
<action name="doubleSelectTest" method="doubleSelectTest" class="com.mobilesoft.esales.webapp.action.DoubleListAction"> <result name="success">test/doubleselect.jsp</result> </action> <action name="doubleSelectPost" method="doubleSelectPost" class="com.mobilesoft.esales.webapp.action.DoubleListAction"> <result name="success">test/doubleselectpost.jsp</result> </action>
4、级联下拉框间的换行<br >问题
缺省情况下,两个级联下拉框间是换行的,Struts2采用Freemarker来做模板,可以通过修改模板来定制标签缺省的树形。参看“Struts2中Datetimepicker控件的中文问题”中的方法,也即:
- 修改struts.mxl,增加如下内容。
<constant name=”struts.serve.static” value=”false” />
<constant name=”struts.ui.templateDir” value=”struts” />
注意在struts2-core-2.0.11.jar/org/apache/struts2/default.properties中有几个与Freemarker对应的配置参数:
struts.ui.theme=xhtml
struts.ui.templateDir=template
#sets the default template type. Either ftl, vm, or jsp
struts.ui.templateSuffix=ftl
- 覆盖缺省的静态文件
在resource目录(与WEB-INF同级或WEB-INF下)创建struts目录,并:
解压struts2-core-2.0.11.jar:/org/apache/struts2/static/ to /struts/
解压struts2-core-2.0.11.jar:/template/simple/ to /struts/simple/
解压struts2-core-2.0.11.jar:/template/xhtml/ to /struts/xhtml/
解压struts2-core-2.0.11.jar:/template/css_xhtml/ to /struts/css_xhtml/
解压struts2-core-2.0.11.jar:/template/ajax/ to /struts/ajax/
解压struts2-core-2.0.11.jar:/template/archive/ to /struts/archive
最后的目录结构如下:
struts/
– ajax/
– archive/
– css_xhtml/
– dojo/
– nls/
– src/
– animation/
– …
– xml/
– struts/
– widget/
– widgets/
– niftycorners/
– simple/
– xhtml/
- 修改doubleselect的模板doubleselect.ftl
修改struts/simple/doubleselect.ftl去除其中的<br />
Technorati 标签: struts2,doubleselect,tag,dojo
转载请注明:出家如初,成佛有余 » Struts2 DoubleSelect标签的使用