上周接受了一个新任务,实现cms新内容管理模块的多条件查询及内容分页
首先,我先实现了html页面的隐藏与显示条件输入框。
网上一般的方法就是用js显示或隐藏层实现
htmlz中:<input type="button" id="searchMore" value="更多搜索条件" οnclick="moreCondition()">
JS代码:
function moreCondition(){
var showMore = document.getElementById("hiddenMore");
if(showMore.style.display=="none"){
showMore.style.display="block";
}
else{
showMore.style.display="none";
}
}
然后页面用struts2标签+JSP+EL表达式实现
后台最重要的其实就是查询和分页的sql语句:
代码如下:
@Override
public List getSearchArticle(int pageNo, int pageSize, String nodeId,String childNodeId, String title, String editor, String author,
String beginTime, String endTime) throws AppException {
String sql = "select top "+pageSize+" * from (select b.*,a.nodeId,a.nodeIdLevel3,a.nodeIdLevel2, ROW_NUMBER() OVER(ORDER BY b.insertedTime desc) AS ROWNUMBER ";
sql+="from cms_FK_nodeArticle as a join cms_article as b on a.articleId = b.id where a.mainFlag = '"+Constance.MAIN_FLAG+"' and b.status!= '"+Constance.DATA_STATUS_STOP+"'";
if(childNodeId != null && !childNodeId.equals("")){
sql += " and (nodeIdLevel4='"+childNodeId+"') ";
}else{
if( nodeId != null && nodeId.trim().length() > 0 )
sql += " and (nodeIdLevel3='"+nodeId+"') ";
}
if(title != null && title.trim().length() > 0){
sql += " and title like '%"+title+"%' ";
}
if(editor != null && editor.trim().length() > 0){
sql += " and editor ='"+editor+"'";
}
if(author != null && author.trim().length() > 0){
sql += " and author = '"+author+"'";
}
if(beginTime != null && beginTime.trim().length() > 0 && endTime != null && endTime.trim().length() > 0){
sql += " and insertedTime between '"+beginTime+"' and '"+endTime+" 23:59:59'";
}
sql+=") F where F.rownumber >"+(pageNo-1)*pageSize;
JdbcHelper jdbcUtil=(JdbcHelper)SpringBeanUtil.getBean("jdbcBaseTransaction");
return jdbcUtil.queryForList(sql);
}
查询出分页结果,在前端读取,其中最麻烦的是每当点下一页后的参数传递问题。
网上有两种方法,一种是提交action后面参数拼接法 为 Parametername="value" ¶m....等
另一种方法是用js控制form提交,就是在form里隐藏写一个存储页码的隐藏域,然后用js控制随表单提交,我是受别人网上说的,在页面里用session存储传递的参数
但是session有其局限性,一是session有一定的时效,过一段时间session会失效,另外session由于浏览器不关会一直存在的,所以有时候会需要清除缓存
反正我做的最后查询的时候,有时候会出乱子,希望朋友们给我指点个更好的方法实现多条件自由分页查询。