本文装载自:http://karidyang.iteye.com/blog/611096
代码很经典
POJO
@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 }
@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 }
@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如下:
@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
}
@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
<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 } </td>
<td>${name } </td>
</tr>
</s:iterator>
</table>
</div>
</s:form>
<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 } </td> <td>${name } </td> </tr> </s:iterator> </table> </div> </s:form>
自定义模板
在classpath下新建template/custom目录,拷贝struts2-core-2.1.8.jar\template\xhtml\doubleselect.ftl到该目录下,删除文件中的<br />
或者创建css样式
.doubleselect br{
display:none;
}
jsp页面:
<div class="doubleselect">
<s:doubleselect list="areaList"
listKey="id"
listValue="areaName"
doubleName="houseId"
name="areaId"
doubleList="houseMap.get(top.id)" //此处top指第一个列表当前选择到的对象
doubleListKey="id"
doubleListValue="houseCode"
theme="simple" />
</div>