ITeye博客

java使用中的一些小技巧

GODdaughter · 2008-11-07 17:39 · sqlserver · Markdown


1.java代码可以直接写到${}中

<%
	int pageSize = 16
%>

${selectCount<pageSize}在标签中可以这样比较(selectCount时数据库查询的总条数)


2.使用response.sendRedirect();之后一定要return一下,否则出异常

3.java比较两个日期间的大小

DateFormat df = new SimpleDateFormat("yyy-MM-dd");
Date d1 = null;
Date d2 = null ;
try {
	d1 = df.parse("2000-01-01");
	d2 = df.parse("2000-01-01");
} catch (ParseException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}


String relation = null;
if(d1.equals(d2)){
	relation = "相等啊";
}else if(d1.before(d2)){
	relation = "before";
}else{
	relation = "after";
}
System.out.println(  relation);

5.c:choose c:when标签中不可以使用java代码的if(下面这样用是错误的)

<c:choose>
   		<c:when test="${editLogg==1}">
   		
   		<% 
   			request.getSession().removeAttribute("editLoggID");//销毁编辑页面中的日志ID(功能用到此就ok了,以防之后session中的值混淆)
		 	if("".equals(request.getParameter("retDay"))&&"today".equals(request.getParameter("retDay"))){
   		%>
   		<jsp:forward page="today.jsp"></jsp:forward>
   		
   		<% 
   			}else if("".equals(request.getParameter("retDay"))&&"list".equals(request.getParameter("retDay"))){
   			System.out.println("我要返回list的页面了");
   		%>
   		
   		<jsp:forward page="list.jsp"></jsp:forward>
   		
   		<% 
   			}else if("".equals(request.getParameter("retDay"))&&"day".equals(request.getParameter("retDay"))){
   		%>
   		
   		<jsp:forward page="day.jsp"></jsp:forward>
   		<% 
   			}	
   		%>
   		
   		</c:when>
   		<c:otherwise>
   			编辑日志信息失败了
   		</c:otherwise>
   	</c:choose>


正确的方式是下面这样

<c:choose>
   		<c:when test="${editLogg==1}">
   		
   		<% 
   			request.getSession().removeAttribute("editLoggID");//销毁编辑页面中的日志ID(功能用到此就ok了,一方之后session中的值混淆)if("".equals(request.getParameter("retDay"))&&"today".equals(request.getParameter("retDay"))){
   		%>
   		<c:if test='<%="today".equals(request.getParameter("retDay")) %>'>
   			<jsp:forward page="today.jsp"></jsp:forward>
   		</c:if>
   		
   		<c:if test='<%="list".equals(request.getParameter("retDay")) %>'>
   			<jsp:forward page="list.jsp"></jsp:forward>
   		</c:if>
   		
   		<c:if test='<%="day".equals(request.getParameter("retDay")) %>'>
   			<jsp:forward page="day.jsp"></jsp:forward>
   		</c:if>
   		
   		</c:when>
   		<c:otherwise>
   			编辑日志信息失败了
   		</c:otherwise>
   	</c:choose>


6.a标签中href 和onclick使用时当注意
a标签的onclick事件和href=""不可以同时使用,否则,onclick事件不会被执行(下面的用法是错误的)

<a href="delLogCate.jsp?logID=${row.id}" style="cursor:pointer;text-decoration:none" onclick="return delLogCate()">

7.利用java代码获取指定日期的总条数(代码实例)

/*获取指定日期的总行数*/
	public int getLogCount(String year ,String month,String day){
		System.out.println(year + " " + month + " " + day );
		TestDB db = new TestDB();
		Connection conn = db.getConnect();
		Statement stmt = null;
		ResultSet rs = null;
		String sql = "select count(*) as logCount  from rizhi where datepart(yyyy,ks_time)="+year + 
					 " and datepart(mm,ks_time)="+month + 
					 " and datepart(dd,ks_time)="+day;
		int countLog = 0;
		try {
			System.out.println(sql);
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
			rs = stmt.executeQuery(sql);
			while(rs.next()){				
				countLog = rs.getInt(1);
				System.out.println(countLog);
			}
		} catch (SQLException e) {
			System.out.println("创建statement失败了");
			e.printStackTrace();
		}finally{
			if(rs!=null){
				try {
					rs.close();
				} catch (SQLException e) {
					System.out.println("关闭ResultSet失败了");
					e.printStackTrace();
				}
			}	
			if(stmt!=null){
				try {
					stmt.close();
				} catch (SQLException e) {
					System.out.println("关闭Statement失败了");
					e.printStackTrace();
				}				
			}
			if(rs!=null){
				try {
					rs.close();
				} catch (SQLException e) {
					System.out.println("关闭conn失败了");					
					e.printStackTrace();
				}
			}			
		}
		return countLog;
	}


8.c:if中错误的用法(很少会注意到的)
<c:if test="${selectCount.rowCount-pageSize>0}"></c:if>(这个是正确的)
<c:if test="${selectCount.rowCount>pageSize}">(这样就是不正确的)

9.去除链接中下划线
text-decoration: none;/*去除链接下划线*/

10.    ..是上一级目录,.是根目录

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。