这是我些的JSP+Bean结构同大家共同探讨
总体思想是一个基础Bean Common, 提供和JSP配合的基本功能, 包括数据库, 出错处理, 权限, 速度测试, debug, 常用日期, 文字处理.
具体的Bean从Common上扩展, 提供自己的功能, 一般数据库处理不外乎处理用户post new, modify, delete, List. 其它应用简单修改就成.
JSP页面上对Bean引用, 出错转向专门的Error提示页面. POST成功后重新定向本页面, 以避免刷新提示.
很少见有人讨论JSP结构, 这里抛砖引玉, 希望大家共同探讨.
Guestbook.java:
===================================================
package steeven;
import java.sql.*;
import java.util.*;
import java.text.*;
import javax.servlet.http.*;
public class Guestbook extends Common {
public int id; // 数据id
public int type; // 留言分类
public String subject,body; // 标题, 内容
public java.util.Date postat,last; // 留言时间, 最后修改
ResultSet list; //分页显示的记录集
public Guestbook cur; //分页显示的当前记录
String table = "guestbook"; //要操作的表名
public Guestbook() {}
public Guestbook(int id) throws Exception{
read(id);
}
public Guestbook(ResultSet rs) throws Exception{
readFromRs(rs);
}
public void read(int id) throws Exception{
String q = "select * from "+table+" where id="+id;
ResultSet rs= Common.query(q);
if (!rs.next())
throw new Exception("记录没有找到: "+id);
this.readFromRs(rs);
}
public void readFromRs(ResultSet rs) throws Exception{
id = rs.getInt("id");
type = rs.getInt("type");
subject = rs.getString("subject");
body = rs.getString("body");
ip = rs.getString("ip");
host = rs.getString("host");
user = rs.getString("user");
postat = rs.getTimestamp("postat");
last = rs.getTimestamp("last");
}
public void setRequest() throws Exception{
action = myParseInt(request.getParameter("action"));
id = myParseInt(request.getParameter("id"));
subject = echo(request.getParameter("subject"));
body = echo(request.getParameter("body"));
type = myParseInt(request.getParameter("type"));
// 必须检查
// if (type<1)
// info.add("没有指定项目编号");
}
public boolean postInsert() throws Exception{
if (subject.length()==0)
info.add("没有填写主题");
if (body.length()==0)
this.info.add("没有填写内容");
if (!this.info.isEmpty())
return false;
String q = "insert into "+table+" (type,subject,body,ip,host,user,postat) values("+
type+","+
"\""+addSqlSlash(subject)+"\","+
"\""+addSqlSlash(body)+"\","+
"\""+ip+"\","+
"\""+host+"\","+
"\""+addSqlSlash(user)+"\","+
"\""+this.getSQLTime()+"\""+
")";
Common.update(q);
id = Common.lastInsertId();
return true;
}
public boolean postUpdate() throws Exception{
// to be finished
return true;
}
public boolean postDelete() throws Exception{
Guestbook todo = new Guestbook(id);
//不是作者要作权限检查
if (!this.isAuthor(todo))
this.info.add("不是作者, 不能删除");
if (this.id < 1 )
this.info.add("没有指定要删除的编号");
if (this.info.size()>0)
return false;
String q = "delete from "+table+" where id="+id;
update(q);
return true;
}
public void getList() throws Exception{
String query = "select * from "+table+" where type="+type+" order by postat desc";
list = query(query);
}
public boolean next() throws Exception{
if (this.list == null)
this.getList();
if (list.next()){
cur = new Guestbook(list);
return true;
}else
return false;
}
public String toString(){
return
"[ id:" +id
+",type:"+type
+",subject:"+subject
+",body:"+body
+",ip:"+ip
+",host:"+host
+",user:"+user
+",postat:"+postat
+",last:"+last
+",action:"+action
+"]";
}
public boolean checkAction() throws Exception{
this.setRequest();
// checkPriv(table+":"+type);
if (this.action == this.INSERT)
return this.postInsert();
if (this.action == this.UPDATE)
return this.postUpdate();
if (this.action == this.DELETE)
return this.postDelete();
return false;
}
public static void main(String[] args) throws Exception{}
}