<!--
MyBBS项目中的经验
-->
1,使用了iframe后获取当前页面的URL:
parent.location.herf
2,在model中或类中的字段如果首字母(前面几个字母)大写,在EL表达式取值时会出现问题!
3,在使用了Hibernate后,当使用cascade="delete"属性时,要实现连带关系的删除,需要现将这个对象查询出来,然后在调用session.delete(obj)
来进行连带删除!
4,在使用cascade="delete"属性时,最好不要在<many-to-one>中加入该属性,不然只要含有该列的都会进行删除(我就悲剧了一次)!
5,当生成验证码时,如果在客户端进行验证码验证,那么就会出现一个问题,服务器端的验证码总会早于客户端一步,
这个我也不大清楚怎么回事,向Sir给我讲了半天,似懂非懂的!再说了~在客户端进行验证码验证,是不好的,因为安全性不高!
6,验证码的显示,直接在src中写入action路径即可!eg:<img src="getValidateImagePages.action"/>依然可以实现效果,前提是
ImageIO.write()中是输出流而不是文件流,后面贴上我写的生成验证码的代码(生成验证码到输出流,并返回验证码值)!
7,PageModel将不会再烦恼我,我的PageModel!(后面贴上)
8,Session.createSQLQuery(sql),该方法不支持缓存设置,设置了缓存将会导致数据错误!
9,原创搜索时关键字加亮(仅仅针对WEB项目),代码后面贴上!
10,在使用了Session.createSQLQuery(sql).list()查询后,返回的不是一个对象,而是一个list<Object[]>集合
在界面中输出(双重for循环):
<c:forEach items="${list_obj}" var="obj">
<c:forEach items="${obj}" var="val">
val
</c:forEach>
</c:forEach>
11,<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
${fn:length(list)}
这个就不解释了~量长度专用!
------------------------------验证码生成方法------------------------------
public String getValidateImg(OutputStream os){
//getRandom
Random ran=new Random();
//
BufferedImage bufImg=new BufferedImage(100, 20, BufferedImage.TYPE_3BYTE_BGR);
Graphics gh=bufImg.getGraphics();
gh.setColor(Color.white);
gh.fillRect(0, 0, 100, 30);
String valiStr="";
for(int i=0;i<4;i++){
gh.setFont(new Font("微软雅黑",Font.ITALIC,17));
String temp=((char)(ran.nextInt(26)+65))+"";
gh.setColor(new Color(ran.nextInt(255)+1));
gh.drawString(temp,10+(i*20),15);
valiStr+=temp;
}
for(int i=0;i<ran.nextInt(6)+4;i++){
gh.setColor(new Color(ran.nextInt(255)+1));
gh.drawLine(ran.nextInt(100),ran.nextInt(30),ran.nextInt(100),ran.nextInt(30));
}
try {
ImageIO.write(bufImg,"jpeg",os);
//ImageIO.write(bufImg,"jpeg",new File("E://a.jpg"));
return valiStr;
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
------------------------------My PageModel------------------------------
a)pageModel类
<!--
public class PageModel {
//
private int pageCount;//只有getter~没有settter!
private int currentPage=1;//记得要有初始值
private int perPageCount=20;//记得要有初始值
private int allCount;
private List list;
//
public int getPageCount() {
return pageCount;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPerPageCount() {
return perPageCount;
}
public void setPerPageCount(int perPageCount) {
this.perPageCount = perPageCount;
}
public int getAllCount() {
return allCount;
}
public void setAllCount(int allCount) {
this.allCount = allCount;
//注意这里一定要先把this.perPageCount的值赋了在进行这步操作,不然,就会只有一条记录
this.pageCount=(int)Math.ceil(this.allCount/(double)this.perPageCount);
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
//
}
-->
b)首先是位于WebRoot目录下的WEB-INF/tags/*.tag文件
<!--
<%@ tag pageEncoding="UTF-8"%>
<%@ attribute name="pm" type="com.shu.model.PageModel" required="true"%>
<%@ attribute name="url" type="java.lang.String" required="true"%>
<div style="font-size:12px;text-align:center;color:blue;">
共有『${pm.allCount}』条记录
第『${pm.currentPage}/${pm.pageCount }』页
『<a href="${url}¤tPage=1">首页</a>』
『<a href="${url}¤tPage=${pm.currentPage-1<=1 ? 1 : pm.currentPage-1}">上一页</a>』
『<a href="${url}¤tPage=${pm.currentPage+1>=pm.pageCount ? pm.pageCount : pm.currentPage+1}">下一页</a>』
『<a href="${url}¤tPage=${pm.pageCount}">尾页</a>』
</div>
-->
c)在查询的地方赋值
<!--
PageModel pm=new PageModel();
pm.setPerPageCount(20);
pm.setAllCount(note.getFellowNotes().size());
pm.setCurrentPage(currentPage);
pm.setList(list);//由于在Hibernate中都是set很方便查询,所以我自己写了一个由set数据进行分页的list的方法(后面贴上)
-->
d)在页面中使用:
<%@ taglib tagdir="/WEB-INF/tags" prefix="shu" %>
<shu:page url="getInNotePages.action?nid=${note.nid}" pm="${requestScope.pm}"></shu:page>
---------------------根据Set来进行分页并转为PageModel所需的list---------------------
注意:由于set本身内的数据没有顺序,所以在使用这个方法前最好把Set中的数据先排序
<!--
//这个方法是自己原创的,经过了我自己测试,没有发现问题!呵呵~自豪感!
public List<T> changeSetToList(Set<T> set,int currentIndex,int perPageCount){
List<T> list=new ArrayList<T>();
Iterator<T> it=set.iterator();
//下标从0开始
int i=0;
//开始迭代,迭代条件是有元素并且list的大小要与每页显示的大小相同
while(it.hasNext()&&list.size()<perPageCount){
i++;
//如果该下标不满足我要查询的下标
if(i<=(currentIndex-1)*perPageCount){
//走掉一个在继续迭代
it.next();
continue;
}
//符合条件就加到list
list.add(it.next());
}
return list;
}
-->
---------------------关键字加亮(仅仅针对Web项目)---------------------
其实核心只有一句话:
keyWord="<span style='color:red'>"+keyWords+"</span>"
<!--
Author:lovingshu
Date:2011-11-21 17:07
Remark:The Hard you try~the more you get!
-->