java使用中的一些小技巧


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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1,设置面板不可拖动变化 java.awt.Frame.setResizable(false); 2,设置一组单选RadioButton 先声明: ButtonGroup jButtonGroup = new ButtonGroup(); JRadioButton jRadioButton1 = new JRadioButton(); JRadioButton jRadioButton2 = new JRadioButton(); JRadioButton jRadioButton3 = new JRadioButton(); 后捆绑: jButtonGroup.add(jRadioButton1); jButtonGroup.add(jRadioButton2); jButtonGroup.add(jRadioButton3); 3,弹出一个提示框 JOptionPane.showMessageDialog(this,) 4,获取一个列表对象的总长度 getItemCount(); 5,判断一个列表对象一个Item是否被选 isIndexSelected(int index); 1,Jtable加到JScroll,默认只实现了垂直滚动(VERTICAL),而水平滚动(Horizontal)没有实现,这时候会使得无论格子数量,总长度=界面宽度。 解决:jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); 2,Swing常常显示文乱码,对于使用了UIManager.getSystemLookAndFeelClassName() 的程序,可以采用如下方法; 解决:在UIManager.getSystemLookAndFeelClassName() 下方添加代码 java.util.Enumeration enum = UIManager.getDefaults().keys(); String str; while(enum.hasMoreElements()){ if((str = (String)enum.nextElement()).endsWith("font")){ //只要与字体相关 UIManager.put(str,new Font("宋体",0,12)); } } 3,改变菜单栏等大小 设置合适的字体 4,对于En_US编码的informix,JTable文显示乱码 对于这类情况,JDBC在取出数据而未显示时,已经是乱码,解决方法: 在连接语句的最后,加上;NEWCODESET=gbk,8859-1,819 5, 1、JFrame在屏幕显示,只须在主类的构造方法里面加上一句: setLocationRelativeTo(null); 2、若要让JDialog居显示,可以加上一句: JDialogname.setLocationRelativeTo(null); setLocationRelativeTo()方法一定要JDialog的SetSize()方法或者是pack()的后面,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值