新闻发布系统笔记一

26 篇文章 0 订阅

1.如果字符串太长,想实现鼠标放上去提示出所有的文字,默认显示一部分可以使用这种方法:

<td><a title="${commentBack.content }">${fn:substring(commentBack.content,0,15) }...</a></td>    a标签的title属性可以满足需要,fn是jstl的函数标签。


2.分页时要先查询出记录总条数,有时候需要根据条件查询出总条数,所有条件使用and连接,执行sql时会出错,要把第一个and替换为where才可以。

StringBuffer sb=new StringBuffer("select count(*) as total from t_comment");
		if(s_comment.getNewsId()!=-1){
			sb.append(" and newsId="+s_comment.getNewsId());
		}
		if(StringUtil.isNotEmpty(bCommentDate)){
			sb.append(" and TO_DAYS(commentDate)>=TO_DAYS('"+bCommentDate+"')");
		}
		if(StringUtil.isNotEmpty(aCommentDate)){
			sb.append(" and TO_DAYS(commentDate)<=TO_DAYS('"+aCommentDate+"')");
		}
		PreparedStatement pstmt=con.prepareStatement(sb.toString().replaceFirst("and", "where"));
		ResultSet rs=pstmt.executeQuery();
		if(rs.next()){
			return rs.getInt("total");
		}else{
			return 0;
		}
3.分页util  

public static String getPagation(String targetUrl,int totalNum,int currentPage,int pageSize){} 

targetUrl是要传给分页控件显示的地址,totalNum是上面查询出来的总条数,currentPage当前的页数,pageSize页大小

首先需要计算出总页数:int totalPage=totalNum%pageSize==0?totalNum/pageSize:totalNum/pageSize+1;

如果可以整除,总页数就不会加1,否则总页数要加1.

拼接上一页时,要判断是不是第一页,要分开处理

if(currentPage==1){
pageCode.append("<li class='disabled'><a href='#'>上一页</a></li>");
}else{
pageCode.append("<li><a href='"+targetUrl+"&page="+(currentPage-1)+"'>上一页</a></li>");
}

拼接下一页时,类似处理。

我们最多只显示5页,所以要拼接当前页的前后二条数据。代码如下

for(int i=currentPage-2;i<=currentPage+2;i++){
if(i<1 || i>totalPage){
continue;
}
if(i==currentPage){
pageCode.append("<li class='active'><a href='#'>"+i+"</a></li>");
}else{
pageCode.append("<li><a href='"+targetUrl+"&page="+i+"'>"+i+"</a></li>");
}

}

下图是一个拼接的简单图:


下面是完整示例代码:

public static String getPagation(String targetUrl,int totalNum,int currentPage,int pageSize){
		int totalPage=totalNum%pageSize==0?totalNum/pageSize:totalNum/pageSize+1;
		StringBuffer pageCode=new StringBuffer();
		pageCode.append("<li><a href='"+targetUrl+"&page=1'>首页</a></li>");
		if(currentPage==1){
			pageCode.append("<li class='disabled'><a href='#'>上一页</a></li>");
		}else{
			pageCode.append("<li><a href='"+targetUrl+"&page="+(currentPage-1)+"'>上一页</a></li>");
		}
		
		for(int i=currentPage-2;i<=currentPage+2;i++){
			if(i<1 || i>totalPage){
				continue;
			}
			if(i==currentPage){
				pageCode.append("<li class='active'><a href='#'>"+i+"</a></li>");
			}else{
				pageCode.append("<li><a href='"+targetUrl+"&page="+i+"'>"+i+"</a></li>");
			}
			
		}
		
		if(currentPage==totalPage){
			pageCode.append("<li class='disabled'><a href='#'>下一页</a></li>");
		}else{
			pageCode.append("<li><a href='"+targetUrl+"&page="+(currentPage+1)+"'>下一页</a></li>");
		}
		pageCode.append("<li><a href='"+targetUrl+"&page="+totalPage+"'>尾页</a></li>");
		return pageCode.toString();
	}
4.分页后台查询处理dao层

需要在dao查询时传入一个PageBean参数,然后再拼接上

if(pageBean!=null){
sb.append(" limit "+pageBean.getStart()+","+pageBean.getPageSize());
}

limit  为分页准备的关键字。下面的查询还包含了其他一些条件。commentdate是前台通过时间控件传回的日期值,TO_DAYS()

mysql内部函数。date的形式为2015-07-06。TO_DAYS('2015-06-7')返回736121, 返回一个天数 (从年份0开始的天数 )

完整代码:

public List<Comment> commentList(Connection con,Comment s_comment,PageBean pageBean,String bCommentDate,String aCommentDate)throws Exception{
		List<Comment> commentList=new ArrayList<Comment>();
		StringBuffer sb=new StringBuffer("select * from t_comment t1,t_news t2 where t1.newsId=t2.newsId");
		if(s_comment.getNewsId()!=-1){
			sb.append(" and t1.newsId="+s_comment.getNewsId());
		}
		if(StringUtil.isNotEmpty(bCommentDate)){
			sb.append(" and TO_DAYS(t1.commentDate)>=TO_DAYS('"+bCommentDate+"')");
		}
		if(StringUtil.isNotEmpty(aCommentDate)){
			sb.append(" and TO_DAYS(t1.commentDate)<=TO_DAYS('"+aCommentDate+"')");
		}
		sb.append(" order by t1.commentDate desc ");
		if(pageBean!=null){
			sb.append(" limit "+pageBean.getStart()+","+pageBean.getPageSize());
		}
		PreparedStatement pstmt=con.prepareStatement(sb.toString());
		ResultSet rs=pstmt.executeQuery();
		while(rs.next()){
			Comment comment=new Comment();
			comment.setCommentId(rs.getInt("commentId"));
			comment.setNewsId(rs.getInt("newsId"));
			comment.setNewsTitle(rs.getString("title"));
			comment.setContent(rs.getString("content"));
			comment.setUserIP(rs.getString("userIP"));
			comment.setCommentDate(DateUtil.formatString(rs.getString("commentDate"), "yyyy-MM-dd HH:mm:ss"));
			commentList.add(comment);
		}
		return commentList;
	}
5.form表单中的东西 通过submit按钮提交之后,就会消失,如果要保留,需要把他们在后台存到session之中,然后再前台通过el表达式获取。 也可以通过这个区分是查询还是,通过分页请求的后台。获取不同的参数。

if(StringUtil.isEmpty(page)){
			page="1";
			session.setAttribute("s_bCommentDate", s_bCommentDate);
			session.setAttribute("s_aCommentDate", s_aCommentDate);
		}else{
			s_bCommentDate=(String) session.getAttribute("s_bCommentDate");
			s_aCommentDate=(String) session.getAttribute("s_aCommentDate");
		}

6.My97日期控件

  设置开始日期小于结束日期:

开始日期的input id = "startDate"

结束日期的input id = "endDate"

日期 从:<input id="startDate" class="Wdate" type="text" onFocus="var endDate=$dp.$('endDate');WdatePicker({onpicked:function(){endDate.focus();},maxDate:'#F{$dp.$D(\'endDate\')}'})"/>

<input id="endDate" class="Wdate" type="text" onFocus="WdatePicker({minDate:'#F{$dp.$D(\'startDate\')}'})"/>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值