出家如初,成佛有余

Struts2 DoubleSelect标签的使用

Posted in Uncategorized by chuanliang on 2008/04/02

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>

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; } 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>

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

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>

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, “Courier New”, courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

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 标签: ,,,

Tagged with: , , ,

No Responses Yet

Subscribe to comments with RSS.

  1. 丁香社区 said, on 2008/05/08 at 22:07

    呵呵,说到点子上了,感谢您的资料,也请到丁香社区来看看!

  2. 汽车 said, on 2008/07/08 at 14:58

    写的就是好,网上有些资料不全

  3. KIM said, on 2008/10/05 at 23:23

    Wow… That’s exactly what I’ve looking for~!! Thank you, very much. ^o^

  4. ManFred said, on 2008/11/15 at 15:04

    留的内容空间太少了啊:
    部门/所有人:

  5. tommy said, on 2009/07/25 at 20:50

    X4xBcn gIcbEw9n3n6JdaL72z0Fu


发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: