前面咱們已經完成了條件查詢的功能,能夠根據用戶給出的條件進行查詢數據。可是呢,仍是有一些小毛病的。咱們來看看:java
當咱們查詢數據時候,對查詢出來的數據進行操做。操做完畢後,它回到的不是咱們查詢後的數據,而是咱們的初始化數據。這明顯是不合適的,當用戶操做完後,咱們應該返回的仍是條件查詢出來的數據。sql
還有一點的就是:咱們的分頁還沒寫……所以,本文主要解決這兩個問題。apache
首先,咱們來分析一下爲何咱們操做完畢後,獲得的是初始化的數據。咱們按照用戶的操做來看看到底哪裏出了問題。數組
那麼在這個過程,咱們遇到什麼問題呢???ruby
在Action中使用一個變量封裝着查詢條件markdown
/************獲取查詢條件的值*************************/ private String selectCondition; public String getSelectCondition() { return selectCondition; } public void setSelectCondition(String selectCondition) { this.selectCondition = selectCondition; }
當請求到Action時,咱們將查詢條件的值取出來,發給對應的JSP頁面jsp
public String editUI() { //獲得全部的信息類型 ActionContext.getContext().getContextMap().put("infoTypeMap", Info.INFO_TYPE_MAP); //外邊已經傳了id過來了,咱們要找到id對應的Info if (info != null && info.getInfoId() != null) { //把查詢條件發給JSP頁面 ActionContext.getContext().getContextMap().put("selectCondition", info.getTitle()); //直接獲取出來,後面JSP會根據Info有getter就能讀取對應的信息! info = infoServiceImpl.findObjectById(info.getInfoId()); } return "editUI"; }
JSP頁面把發送過來的值存儲好,經過隱藏域發送給Actionpost
<%--把查詢條件帶過去給Action--%> <s:hidden name="selectCondition"/>
從新抵達到Action的時候,因爲Struts2有自動封裝的功能,因此能夠把查詢條件封裝到selectCondition變量中。最後操做完重定向到listUI界面this
因爲是重定向,因此咱們須要在struts配置文件中把咱們的查詢條件帶過去:編碼
<result name="list" type="redirectAction"> <param name="actionName">info_listUI</param> <!--重定向回去的時候,把查詢條件帶上--> <param name="info.title">${selectCondition}</param> </result>
固然啦,在刪除的時候,把查詢條件記錄下來就好了。
//刪除 public String delete() { selectCondition = info.getTitle(); String id = info.getInfoId(); infoServiceImpl.delete(id); return "list"; }
上面咱們的確解決了查詢後數據回顯的狀況,可是若是咱們的查詢條件是中文的話,會怎麼樣??
變成了亂碼了…..在解決它以前,咱們又來分析一下爲何出現亂碼了….
那就是在重定向的時候,中文參數的數據搞亂了。
咱們在配置文件上,要傳遞參數的時候,設置編碼:
<!--傳輸數據的時候須要編碼--> <param name="encode">true</param>
if (info != null) { if (StringUtils.isNotBlank(info.getTitle())) { selectCondition = URLDecoder.decode(info.getTitle(),"UTF-8"); info.setTitle(selectCondition); queryHelper.addCondition(" i.title like ? ", "%" + info.getTitle() + "%"); } }
分頁對咱們來講也不是陌生的事情了,我曾經在寫JDBC博文的時候就講解過度頁了:http://blog.csdn.net/hon_3y/article/details/53790092
分頁的複用代碼http://blog.csdn.net/hon_3y/article/details/70051541
咱們這一次仍是使用回咱們的分頁複用代碼,具體不一樣的需求,在上面修改便可了。
/** * * @param queryHelper 查詢助手,條件查詢都交給查詢助手來幹 * @param currentPage 當前頁數 * @return */ PageResult getPageResult(QueryHelper queryHelper, int currentPage);
/** * * @return 返回查詢總記錄數的sql語句 */ public String getTotalRecordSql() { return "SELECT COUNT(*) " + fromClause + whereClause; }
daoImpl實現:
先查詢總記錄數
public PageResult getPageResult(QueryHelper queryHelper, int currentPage) { //查詢總記錄數 Query queryCount = getSession().createQuery(queryHelper.getTotalRecordSql()); if (queryHelper.getObjectList() != null) { int i =0; for (Object o : queryHelper.getObjectList()) { queryCount.setParameter(i, o); i++; } } Long totalRecord = (Long) queryCount.uniqueResult(); //初始化PageResult對象 PageResult pageResult = new PageResult(currentPage, totalRecord); //查詢具體模塊的數據【有查詢條件的也能夠處理】 Query query = getSession().createQuery(queryHelper.returnHQL()); if (queryHelper.getObjectList() != null) { int i =0; for (Object o : queryHelper.getObjectList()) { query.setParameter(i, o); i++; } } //設置分頁開始和末尾 query.setFirstResult(pageResult.getStartIndex()); query.setMaxResults(pageResult.getLineSize()); List dataList = query.list(); //將條件查詢出來的數據設置到Page對象中 pageResult.setList(dataList); return pageResult; }
PageResult getPageResult(QueryHelper queryHelper, int currentPage);
public PageResult getPageResult(QueryHelper queryHelper, int currentPage) { return baseDao.getPageResult(queryHelper, currentPage); }
設置咱們須要用到的分頁屬性。
private int currentPageCount; private PageResult pageResult; public int getCurrentPageCount() { return currentPageCount; } public void setCurrentPageCount(int currentPageCount) { this.currentPageCount = currentPageCount; } public PageResult getPageResult() { return pageResult; } public void setPageResult(PageResult pageResult) { this.pageResult = pageResult; }
判斷咱們的當前頁是否爲0【若是爲0,就表明着剛初始化值,咱們設置爲1】,調用service的方法獲得分頁對象
//當前頁數沒有值,那麼賦值爲1 if (currentPageCount == 0) { currentPageCount = 1; } pageResult = infoServiceImpl.getPageResult(queryHelper,currentPageCount);
在JSP頁面中,咱們遍歷分頁對象的集合就能夠獲取分頁的數據了。
<s:iterator value="pageResult.list" status="st">
咱們的分頁屬性和查詢條件數據不僅僅只有Info模塊用的,因而咱們將分頁數據抽取到BaseAction中
/************分頁屬性*************************/ protected int currentPageCount; protected PageResult pageResult; public int getCurrentPageCount() { return currentPageCount; } public void setCurrentPageCount(int currentPageCount) { this.currentPageCount = currentPageCount; } public PageResult getPageResult() { return pageResult; } public void setPageResult(PageResult pageResult) { this.pageResult = pageResult; } /************獲取查詢條件的值*************************/ protected String selectCondition; public String getSelectCondition() { return selectCondition; } public void setSelectCondition(String selectCondition) { this.selectCondition = selectCondition; }
修改其餘的模塊,也可以擁有條件查詢和分頁查詢這兩個功能:以用戶模塊爲例。
//拋出Action異常 public String listUI() throws ServiceException, UnsupportedEncodingException { QueryHelper queryHelper = new QueryHelper(User.class, "u"); //根據info是否爲null來判斷是不是條件查詢。若是info爲空,那麼是查詢全部。 if (user != null) { if (org.apache.commons.lang.StringUtils.isNotBlank(user.getName())) { selectCondition = URLDecoder.decode(user.getName(),"UTF-8"); user.setName(selectCondition); queryHelper.addCondition(" u.name like ? ", "%" + user.getName() + "%"); } } //當前頁數沒有值,那麼賦值爲1 if (currentPageCount == 0) { currentPageCount = 1; } pageResult = userServiceImpl.getPageResult(queryHelper,currentPageCount); return "listUI"; }
<s:iterator value="pageResult.list"> <jsp:include page="/common/pageNavigator.jsp"/>
處理查詢後數據回顯的問題:
public String editUI() { //把全部的角色查詢出來,帶過去給JSP頁面顯示 ActionContext.getContext().getContextMap().put("roleList", roleServiceImpl.findObjects()); //外邊已經傳了id過來了,咱們要找到id對應的User if (user != null &&user.getId() != null ) { //獲得查詢條件 selectCondition = user.getName(); //直接獲取出來,後面JSP會根據User有getter就能讀取對應的信息! user = userServiceImpl.findObjectById(user.getId()); //經過用戶的id獲得所擁有UserRole List<UserRole> roles = userServiceImpl.findRoleById(user.getId()); //把用戶擁有角色的id填充到數組中,數組最後回顯到JSP頁面 int i=0; userRoleIds = new String[roles.size()]; for (UserRole role : roles) { userRoleIds[i++] = role.getUserRoleId().getRole().getRoleId(); } } return "editUI"; }
<%--把查詢條件經過隱藏域帶過去給Action--%> <s:hidden name="selectCondition"></s:hidden>
<action name="user_*" class="zhongfucheng.user.action.UserAction" method="{1}"> <result name="{1}" >/WEB-INF/jsp/user/{1}.jsp</result> <!--返回列表展現頁面,重定向到列表展現--> <result name="list" type="redirectAction"> <param name="actionName">user_listUI</param> <param name="user.name">${selectCondition}</param> <param name="encode">true</param> </result> </action>
//刪除 public String delete() { if (user != null && user.getId() != null) { //記載着查詢條件 selectCondition = user.getName(); userServiceImpl.delete(user.getId()); } return "list"; }