利用HIBERNAGTE要實現如下功能:
也就是在下方選擇要顯式的項目,則在上方列表顯式.
具體實現:
Struts2實現:
<table width="100%" cellspacing="1" class="l_table">
<tr class="l_title">
<s:iterator value="showItems">
<td>
<s:property value="value" />
</td>
</s:iterator>
</tr>
<s:iterator value="pager.result" status="st" id="list">
<tr class="l_tr">
<s:iterator value="list" status="st1">
<td>
<s:if test="#st1.count==1">
<input type="checkbox" vlaue="<s:property value="#list[0]" />" />
</s:if>
<s:elseif test="#st1.count==2">
<a href="#"
onclick="openWindow ('customer.action?cusId=<s:property value="#list[0]" />',800,600,'cusW<s:property value="#list[0]" />') "><s:property />
</a>
</s:elseif>
<s:else>
<s:property />
</s:else>
</td>
</s:iterator>
</tr>
</s:iterator>
</table>
<table width="100%" algin="center" id="selectT">
<tr>
<td align="center">
<b>顯示項目</b>
</td>
</tr>
<tr>
<td>
<s:iterator value="selectItems">
<s:property value="value" />
<input type="checkbox" value="<s:property value="key" />" />
</s:iterator>
</td>
</tr>
<tr>
<td align="center">
<input type="button" id="btn_refresh" value="更 新" />
</td>
</tr>
</table>
public String execute() throws Exception {
page = page == null ? 1 : page;
pageSize = pageSize == null ? 10 : pageSize;
pager = new Page<Object[]>();
pager.setPageNo(page);
pager.setPageSize(pageSize);
pager.setStyleType(2);
pager.setTarget("list_customers.asp?pageSize=" + pageSize);
/**//* 定制列表項 */
boolean isExistCookie = false;
Cookie[] cookies = getRequest().getCookies();
for (Cookie cookie : cookies) {
if (cookie.getName().equals("showItemsStr")) {
// Cookie存在
isExistCookie = true;
if (showItemsStr != null && !showItemsStr.trim().equals ("")) {
// 刷新Cookie
cookie.setValue(showItemsStr);
cookie.setMaxAge(60 * 60 * 24 * 30);
getResponse().addCookie(cookie);
} else {
// 從Cookie中讀取
showItemsStr = cookie.getValue();
}
break;
}
}
// Cookie不存在
if (!isExistCookie) {
if (showItemsStr != null && !showItemsStr.trim().equals ("")) {
// 創建Cookie
Cookie c = new Cookie("showItemsStr", showItemsStr);
c.setPath("/");
c.setMaxAge(60 * 60 * 24 * 30);
getResponse().addCookie(c);
}
}
if (showItemsStr == null || showItemsStr.trim().equals("")) {
// 為空 ,默認
showItemsStr = "cusNation|cusCity|cusTelNum|cusEmail";
}
showItemsStr = "id|cusName|" + showItemsStr;// id cusName 必須
showItems = getMapFromStr(showItemsStr);
pager = customerManager.getCustomers(pager, showItemsStr);
selectItems = getCusStringMap();
selectItems.remove("id");
selectItems.remove("cusName");
searchItems = getCusStringMap();
searchItems.remove("id");
return SUCCESS;
}
private Map<String, String> getMapFromStr(String str) {
str = str.trim();
if (str == null || str.equals(""))
return null;
Map<String, String> map = new LinkedHashMap<String, String> ();
String[] strs = str.split("\\|");
for (String s : strs) {
map.put(s, getCusStringMap().get(s));
}
return map;
}
private Map<String, String> getCusStringMap() {
Map<String, String> map = new LinkedHashMap<String, String> ();
map.put("id", "序號");
map.put("cusName", "客戶名稱");
map.put("cusEmail", "電子郵件");
/*省略具體項目*/
return map;
}
public Page<Object[]> getLinkmans(Page<Object[]> page, String showItemsStr) {
String hql = "select " + showItemsStr.trim().replaceAll ("\\|", ",")
+ " from Linkman";
page.setAutoCount(false);
int totalCount = linkmanDao.sum("select count(*) from Linkman")
.intValue();
page.setTotalCount(totalCount);
page.setResult(linkmanDao
.find(hql, page.getFirst(), page.getPageSize()));
return page;
}