本文章系Jarvis原创,转载请注明!

利用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">
                                    
<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;
    }