---
title: "【转载】<s:doubleselect>标签的使用"
author: "demojava"
date: "2011-01-05T08:49:56Z"
tags: []
categories: ["企业架构"]
slug: "s-doubleselect"
draft: false
lastmod: "2011-01-05T08:56:27Z"
id: 859263
url: "https://demojava.iteye.com/blog/859263"
---

本文装载自：[http://karidyang.iteye.com/blog/611096](http://karidyang.iteye.com/blog/611096)

代码很经典

POJO

Java代码 [![复制代码](http://karidyang.iteye.com/images/icon_copy.gif)](http://karidyang.iteye.com/blog/611096 "复制代码")

@Entity  

@Table(name = "P\_AREA")   

@Cache(usage = CacheConcurrencyStrategy.READ\_WRITE)   

**public** **class** Area {   

@Id  

        **private** Long id;   

    **private** String areaName;   

  

        // getter and setter   

}  

```
@Entity
@Table(name = "P_AREA")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Area {
        @Id
        private Long id;
	private String areaName;

        // getter and setter
}
```

 

Java代码 [![复制代码](http://karidyang.iteye.com/images/icon_copy.gif)](http://karidyang.iteye.com/blog/611096 "复制代码")

@Entity  

@Table(name = "P\_HOUSE")   

@Cache(usage = CacheConcurrencyStrategy.READ\_WRITE)   

**public** **class** House {   

        @Id  

        **private** Long id;   

    **private** String houseCode;   

  

        //getter and setter   

}  

```
@Entity
@Table(name = "P_HOUSE")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class House {
        @Id
        private Long id;
	private String houseCode;

        //getter and setter
}
```

 

Java代码 [![复制代码](http://karidyang.iteye.com/images/icon_copy.gif)](http://karidyang.iteye.com/blog/611096 "复制代码")

@Entity  

@Table(name = "P\_PERSON")   

@Cache(usage = CacheConcurrencyStrategy.READ\_WRITE)   

**public** **class** Person {   

        @Id  

        **private** Long id;   

    **private** String name;   

  

        //getter and setter   

}  

```
@Entity
@Table(name = "P_PERSON")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Person {
        @Id
        private Long id;
	private String name;

        //getter and setter
}

```

 

 

Action如下：

Java代码 [![复制代码](http://karidyang.iteye.com/images/icon_copy.gif)](http://karidyang.iteye.com/blog/611096 "复制代码")

@Namespace("/")   

@Results( { @Result(name = CrudActionSupport.RELOAD, location = "person.action", type = "redirect") })   

**public** **class** PersonAction **extends** CrudActionSupport{   

       

    **private** List&lt;Person&gt; persons;   

    **private** Long areaId;   

    **private** Long houseId;   

       

    **private** List&lt;Area&gt; areaList;   

    **private** Map&lt;Long, List&lt;House&gt;&gt; houseMap;   

    //服务层接口   

    @Autowired  

    **private** ServiceManage serviceManage;   

       

    @Override  

    **public** String list() **throws** Exception {   

           

           

        //从数据库查询所有的区域   

        areaList = serviceManage.searchArea();   

        houseMap = **new** HashMap&lt;Long, List&lt;House&gt;&gt;();   

        //封装HouseMap   

        **for** (Area area : areaList) {   

            houseMap.put(area.getId(), area.getHouseList());   

        }   

        **if** (houseId == **null**) {   

            addActionError("请选择房号");   

            **return** SUCCESS;   

        }   

        persons = serviceManage.searchPerson(houseId);   

        **return** SUCCESS;   

    }   

       

    //getter and setter   

  

}  

```
@Namespace("/")
@Results( { @Result(name = CrudActionSupport.RELOAD, location = "person.action", type = "redirect") })
public class PersonAction extends CrudActionSupport{
	
	private List<Person> persons;
	private Long areaId;
	private Long houseId;
	
	private List<Area> areaList;
	private Map<Long, List<House>> houseMap;
	//服务层接口
	@Autowired
	private ServiceManage serviceManage;
	
	@Override
	public String list() throws Exception {
		
		
		//从数据库查询所有的区域
		areaList = serviceManage.searchArea();
		houseMap = new HashMap<Long, List<House>>();
		//封装HouseMap
		for (Area area : areaList) {
			houseMap.put(area.getId(), area.getHouseList());
		}
		if (houseId == null) {
			addActionError("请选择房号");
			return SUCCESS;
		}
		persons = serviceManage.searchPerson(houseId);
		return SUCCESS;
	}
	
	//getter and setter

}
```

 

页面: person.jsp

Java代码 [![复制代码](http://karidyang.iteye.com/images/icon_copy.gif)](http://karidyang.iteye.com/blog/611096 "复制代码")

&lt;s:form id="mainForm" action="person.action" method="post"&gt;   

        &lt;div id="filter"&gt;   

            &lt;div style="float: left"&gt;   

            请选择房间:&lt;s:doubleselect list="areaList"    

                listKey="id"    

                listValue="areaName"  

                doubleName="houseId"  

                name="areaId"  

                doubleList="houseMap.get(top.id)" //此处top指第一个列表当前选择到的对象   

                doubleListKey="id"  

                doubleListValue="houseCode"  

                theme="custom"  //custom是自定义的模板类型，取消了原来模板中的&lt;br /&gt;   

 /&gt;   

            &lt;/div&gt;   

            &lt;input type="button" value="搜索" onclick="search();"/&gt;   

        &lt;/div&gt;   

           

        &lt;div id="content"&gt;   

            &lt;table id="contentTable"&gt;   

                &lt;tr&gt;   

                    &lt;th&gt;&lt;房号&lt;/th&gt;   

                    &lt;th&gt;姓名&lt;/th&gt;   

                &lt;/tr&gt;   

                &lt;s:iterator value="person"&gt;   

                    &lt;tr&gt;   

                        &lt;td&gt;${house.houseCode }&nbsp;&lt;/td&gt;   

                        &lt;td&gt;${name }&nbsp;&lt;/td&gt;   

                    &lt;/tr&gt;   

                &lt;/s:iterator&gt;   

            &lt;/table&gt;   

        &lt;/div&gt;   

    &lt;/s:form&gt;  

```
<s:form id="mainForm" action="person.action" method="post">
		<div id="filter">
			<div style="float: left">
			请选择房间:<s:doubleselect list="areaList" 
				listKey="id" 
				listValue="areaName"
                doubleName="houseId"
                name="areaId"
                doubleList="houseMap.get(top.id)" //此处top指第一个列表当前选择到的对象
                doubleListKey="id"
                doubleListValue="houseCode"
                theme="custom"  //custom是自定义的模板类型，取消了原来模板中的<br />
 />
            </div>
			<input type="button" value="搜索" onclick="search();"/>
		</div>
		
		<div id="content">
			<table id="contentTable">
				<tr>
					<th><房号</th>
					<th>姓名</th>
				</tr>
				<s:iterator value="person">
					<tr>
						<td>${house.houseCode }&nbsp;</td>
						<td>${name }&nbsp;</td>
					</tr>
				</s:iterator>
			</table>
		</div>
	</s:form>
```

 

 

自定义模板

在classpath下新建template/custom目录，拷贝struts2-core-2.1.8.jar\\template\\xhtml\\doubleselect.ftl到该目录下，删除文件中的&lt;br /&gt;

或者创建css样式

.doubleselect br{

 display:none;  
}

jsp页面：

 &lt;div class="doubleselect"&gt;

&lt;s:doubleselect list="areaList"  
listKey="id"  
listValue="areaName"  
                doubleName="houseId"  
                name="areaId"  
                doubleList="houseMap.get(top.id)" //此处top指第一个列表当前选择到的对象  
                doubleListKey="id"  
                doubleListValue="houseCode"  
                theme="simple"  /&gt;

 

&lt;/div&gt;