这个demo供大家一起探讨学习struts,因为工作太累,没精力给大家解释实现原理。如果看不懂,没关系。只是说明jsp基础还没有到火候,不要心急,回去强化下jsp+servlet,基础扎实了,自然能够看懂我写的代码。这个demo借鉴了网上很多前人的经验,在此一并谢谢。
web.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<!doctype web-app public "-//sun microsystems, inc.//dtd web application 2.3//en" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>bookshopmod</display-name>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.actionservlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/web-inf/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>application</param-name>
<param-value>applicationresources</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>/web-inf/struts-bean.tld</taglib-uri>
<taglib-location>/web-inf/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/web-inf/struts-html.tld</taglib-uri>
<taglib-location>/web-inf/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/web-inf/struts-logic.tld</taglib-uri>
<taglib-location>/web-inf/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/web-inf/struts-template.tld</taglib-uri>
<taglib-location>/web-inf/struts-template.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/web-inf/struts-tiles.tld</taglib-uri>
<taglib-location>/web-inf/struts-tiles.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/web-inf/struts-nested.tld</taglib-uri>
<taglib-location>/web-inf/struts-nested.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/web-inf/camel-define.tld</taglib-uri>
<taglib-location>/web-inf/camel-define.tld</taglib-location>
</taglib>
</web-app>
struts-config.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<!doctype struts-config public "-//apache software foundation//dtd struts configuration 1.1//en" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="bookform" type="com.bookshop.form.bookform"/>
<form-bean name="operatorform" type="com.bookshop.form.operatorform"/>
<form-bean name="findrecordform" type="com.bookshop.form.findrecordform"/>
</form-beans>
<global-forwards>
<forward name="index" path="/index.jsp"/>
<forward name="browser" path="/show.jsp"/>
<forward name="global_error" path="/error.jsp"/>
</global-forwards>
<action-mappings>
<action input="/show.jsp" name="bookform" parameter="operator" path="/operatoraction" scope="session" type="com.bookshop.action.operatoraction" validate="false">
<forward name="operatorok" path="/success.jsp" redirect="true"/>
<forward name="showfirstpage" path="/operatoraction.do?operator=showfirstpage"/>
<forward name="showpreviouspage" path="/operatoraction.do?operator=showpreviouspage"/>
<forward name="shownextpage" path="/operatoraction.do?operator=shownextpage"/>
<forward name="showlastpage" path="/operatoraction.do?operator=showlastpage"/>
<forward name="showaddrecord" path="/editrecord.jsp?operator=addrecord" redirect="true"/>
<forward name="showmodifyrecord" path="/editrecord.jsp?operator=modifyrecord"/>
<forward name="showfindrecord" path="/findrecord.jsp" redirect="true"/>
</action>
<action input="/findrecord.jsp" name="findrecordform" path="/findrecordaction" scope="session" type="com.bookshop.action.findrecordaction" validate="false"/>
</action-mappings>
<plug-in classname="org.apache.struts.tiles.tilesplugin">
<set-property property="definitions-config" value="/web-inf/tiles-defs.xml"/>
</plug-in>
</struts-config>
tiles-defs文件:
<?xml version="1.0" encoding="utf-8"?>
<!doctype tiles-definitions public "-//apache software foundation//dtd tiles configuration 1.1//en" "http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd">
<tiles-definitions>
<definition name="base-definition" path="layout.jsp">
<put name="head" value="head.jsp" />
<put name="left" value="left.jsp" />
<put name="body" />
<put name="foot" value="foot.jsp" />
</definition>
<definition extends="base-definition" name="index-definition">
<put name="body" value="index_body.jsp" />
</definition>
<definition extends="base-definition" name="show-definition">
<put name="body" value="show_body.jsp" />
</definition>
<definition extends="base-definition" name="edit-definition">
<put name="body" value="edit_body.jsp" />
</definition>
<definition extends="base-definition" name="find-definition">
<put name="body" value="find_body.jsp"/>
</definition>
<definition extends="base-definition" name="success-definition">
<put name="body" value="success_body.jsp" />
</definition>
<definition extends="base-definition" name="error-definition">
<put name="body" value="error_body.jsp"/>
</definition>
</tiles-definitions>
camel-define文件:
<?xml version="1.0" encoding="utf-8"?>
<!doctype taglib public "-//sun microsystems, inc.//dtd jsp tag library 1.1//en" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>camel</shortname>
<uri>http://jakarta.apache.org/struts/tags-bean</uri>
<tag>
<name>islastpage</name>
<tagclass>com.bookshop.util.islasttag</tagclass>
<bodycontent>jsp</bodycontent>
<attribute>
<name>page</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
上面几个文件和struts-bean.tld,struts-html.tld,struts-tiles.tld,struts-logic.tld都一起位于web-inf的根目录下面。
以下是三个action文件:
/*findrecordaction.java*/
package com.bookshop.action;
import org.apache.struts.action.actionmapping;
import org.apache.struts.action.actionform;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.apache.struts.action.actionforward;
import com.bookshop.form.findrecordform;
import org.apache.struts.action.action;
import java.util.list;
import java.util.arraylist;
import com.bookshop.model.operator;
import com.bookshop.util.pageinfo;
import org.apache.struts.action.actionerrors;
import org.apache.struts.action.actionerror;
public class findrecordaction
extends action {
public actionforward execute(actionmapping actionmapping,
actionform actionform,
httpservletrequest servletrequest,
httpservletresponse servletresponse) {
findrecordform findrecordform = (findrecordform) actionform;
string key = findrecordform.getfindbykey().trim();
string value = findrecordform.getfindbyvalue().trim();
list list = new arraylist();
list = operator.getrecords(key, value, 0);
servletrequest.getsession().setattribute("books", list);
if (!list.isempty()) {
servletrequest.getsession().setattribute("pageinfo",
new pageinfo(operator.
getrecordsnumber(), 1));
}
else {
actionerrors messages = new actionerrors();
messages.add(actionerrors.global_message,
new actionerror("findrecord.jsp.notfound"));
servletrequest.getsession().setattribute("pageinfo",
new pageinfo(0, 1));
}
return actionmapping.findforward("browser");
}
}
/*genericaction.java*/
package com.bookshop.action;
import org.apache.struts.action.actionmapping;
import org.apache.struts.action.actionform;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.apache.struts.action.actionforward;
import org.apache.struts.actions.dispatchaction;
import org.apache.struts.action.actionerrors;
import org.apache.struts.action.actionerror;
public class genericaction
extends dispatchaction {
/*
public actionforward execute(actionmapping actionmapping,
actionform actionform,
httpservletrequest servletrequest,
httpservletresponse servletresponse) {
throw new java.lang.unsupportedoperationexception(
"method $execute() not yet implemented.");
}
*/
public void saveglobalerrors(httpservletrequest httpservletrequest,
string errorkey) {
actionerrors errors = new actionerrors();
errors.add(actionerrors.global_error, new actionerror(errorkey));
if (errors != null) {
saveerrors(httpservletrequest, errors);
}
}
public actionforward getindexforward(actionmapping actionmapping) {
return actionmapping.findforward("index");
}
public actionforward getbrowserforward(actionmapping actionmapping) {
return actionmapping.findforward("browser");
}
public actionforward showdeleteforward(actionmapping actionmapping) {
return actionmapping.findforward("showdelete");
}
public actionforward getoperatorokforward(actionmapping actionmapping) {
return actionmapping.findforward("operatorok");
}
public actionforward geterrorforward(actionmapping actionmapping) {
return actionmapping.findforward("global_error");
}
public actionforward getshowaddforward(actionmapping actionmapping) {
return actionmapping.findforward("showaddrecord");
}
public actionforward getshowmodifyforward(actionmapping actionmapping) {
return actionmapping.findforward("showmodifyrecord");
}
public actionforward getshowdeleteforward(actionmapping actionmapping) {
return actionmapping.findforward("showdeleterecord");
}
public actionforward getshowfindforward(actionmapping actionmapping) {
return actionmapping.findforward("showfindrecord");
}
}
/*operatoraction.java*/
package com.bookshop.action;
import org.apache.struts.action.actionmapping;
import org.apache.struts.action.actionform;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.apache.struts.action.actionforward;
import com.bookshop.form.operatorform;
import org.apache.struts.action.action;
import java.util.list;
import org.apache.struts.globals;
import com.bookshop.util.dbutil;
import com.bookshop.util.applicationutil;
import com.bookshop.model.operator;
import java.util.arraylist;
import com.bookshop.util.pageinfo;
import org.apache.struts.actions.dispatchaction;
import java.util.map;
import java.util.hashmap;
import com.bookshop.form.bookform;
import java.util.locale;
import org.apache.struts.action.actionerrors;
import org.apache.struts.action.actionerror;
import com.bookshop.util.bookbean;
public class operatoraction
extends genericaction {
/*
public actionforward execute(actionmapping actionmapping,
actionform actionform,
httpservletrequest servletrequest,
httpservletresponse servletresponse) {
throw new java.lang.unsupportedoperationexception(
"method $execute() not yet implemented.");
}
*/
//转换为中文页面
public actionforward changech(actionmapping actionmapping,
actionform actionform,
httpservletrequest servletrequest,
httpservletresponse servletresponse) {
servletrequest.getsession().setattribute(globals.locale_key, locale.china);
return this.getindexforward(actionmapping);
}
//转换为英文页面
public actionforward changeen(actionmapping actionmapping,
actionform actionform,
httpservletrequest servletrequest,
httpservletresponse servletresponse) {
servletrequest.getsession().setattribute(globals.locale_key, locale.english);
return this.getindexforward(actionmapping);
}
//链接到首页记录
public actionforward showfirstpage(actionmapping actionmapping,
actionform actionform,
httpservletrequest httpservletrequest,
httpservletresponse httpservletresponse) {
list list = new arraylist();
list = operator.getrecords(0);
httpservletrequest.getsession().setattribute("books", list);
httpservletrequest.getsession().setattribute("pageinfo",
new pageinfo(operator.
getrecordsnumber(), 1));
return this.getbrowserforward(actionmapping);
}
//链接到上一页记录
public actionforward showpreviouspage(actionmapping actionmapping,
actionform actionform,
httpservletrequest httpservletrequest,
httpservletresponse httpservletresponse) {
list list = new arraylist();
pageinfo pageinfo = (pageinfo) httpservletrequest.getsession().getattribute(
"pageinfo");
list = operator.getrecords( (pageinfo.getpreviouspagenumber() - 1) *
applicationutil.recordperpage);
httpservletrequest.getsession().setattribute("books", list);
httpservletrequest.getsession().setattribute("pageinfo",
new pageinfo(operator.
getrecordsnumber(), pageinfo.getpreviouspagenumber()));
return this.getbrowserforward(actionmapping);
}
//链接到下一页记录
public actionforward shownextpage(actionmapping actionmapping,
actionform actionform,
httpservletrequest httpservletrequest,
httpservletresponse httpservletresponse) {
list list = new arraylist();
pageinfo pageinfo = (pageinfo) httpservletrequest.getsession().getattribute(
"pageinfo");
list = operator.getrecords(pageinfo.getcurrentlypage() *
applicationutil.recordperpage);
httpservletrequest.getsession().setattribute("books", list);
httpservletrequest.getsession().setattribute("pageinfo",
new pageinfo(operator.
getrecordsnumber(), pageinfo.getnextpagenumber()));
return this.getbrowserforward(actionmapping);
}
//链接到末页记录
public actionforward showlastpage(actionmapping actionmapping,
actionform actionform,
httpservletrequest httpservletrequest,
httpservletresponse httpservletresponse) {
list list = new arraylist();
pageinfo pageinfo = (pageinfo) httpservletrequest.getsession().getattribute(
"pageinfo");
list = operator.getrecords( (pageinfo.getpagecountnumber() - 1) *
applicationutil.recordperpage);
httpservletrequest.getsession().setattribute("books", list);
httpservletrequest.getsession().setattribute("pageinfo",
new pageinfo(operator.
getrecordsnumber(), pageinfo.getlastpagenumber()));
return this.getbrowserforward(actionmapping);
}
//取消操作的转向
public actionforward cancel(actionmapping actionmapping,
actionform actionform,
httpservletrequest httpservletrequest,
httpservletresponse httpservletresponse) {
if (iscancelled(httpservletrequest)) {
return this.getoperatorokforward(actionmapping);
}
return null;
}
//查看所有记录
public actionforward browser(actionmapping actionmapping,
actionform actionform,
httpservletrequest httpservletrequest,
httpservletresponse httpservletresponse) {
return this.showfirstpage(actionmapping, actionform, httpservletrequest,
httpservletresponse);
}
//执行添加记录
public actionforward addrecord(actionmapping actionmapping,
actionform actionform,
httpservletrequest httpservletrequest,
httpservletresponse httpservletresponse) {
bookform bookform = (bookform) actionform;
if (operator.addrecord(bookform.loadbook()) >= 1) {
return this.getoperatorokforward(actionmapping);
}
else {
this.saveglobalerrors(httpservletrequest, "editrecord.jsp.adderror");
return this.geterrorforward(actionmapping);
}
}
//提交更新操作
public actionforward submitrecord(actionmapping actionmapping,
actionform actionform,
httpservletrequest httpservletrequest,
httpservletresponse httpservletresponse) {
string str = (string) httpservletrequest.getsession().getattribute("method");
if (str.equals("addrecord")) {
return addrecord(actionmapping, actionform, httpservletrequest,
httpservletresponse);
}
if (str.equals("modifyrecord")) {
return modifyrecord(actionmapping, actionform, httpservletrequest,
httpservletresponse);
}
else {
this.saveglobalerrors(httpservletrequest, "edit.body.error");
return this.geterrorforward(actionmapping);
}
}
//执行修改操作
public actionforward modifyrecord(actionmapping actionmapping,
actionform actionform,
httpservletrequest httpservletrequest,
httpservletresponse httpservletresponse) {
bookform bookform = (bookform) actionform;
if (operator.modifyrecord(bookform.loadbook()) != -1) {
return this.getoperatorokforward(actionmapping);
}
else {
this.saveglobalerrors(httpservletrequest, "editrecord.jsp.modifyerror");
return this.geterrorforward(actionmapping);
}
}
//跳转到添加记录编辑页面
public actionforward showadd(actionmapping actionmapping,
actionform actionform,
httpservletrequest httpservletrequest,
httpservletresponse httpservletresponse) {
httpservletrequest.getsession().setattribute("bookbean", new bookform());
httpservletrequest.getsession().setattribute("method",
new string("addrecord"));
return this.getshowaddforward(actionmapping);
}
//跳转到修改记录编辑页面
public actionforward showmodify(actionmapping actionmapping,
actionform actionform,
httpservletrequest httpservletrequest,
httpservletresponse httpservletresponse) {
bookbean book = new bookbean();
string str = httpservletrequest.getparameter("bookid").tostring();
book = operator.getrecord(str);
httpservletrequest.getsession().setattribute("bookbean",
new bookform(book.getbookid(),
book.getbookname(), book.getauthor(), book.getpublish(), book.getprice()));
httpservletrequest.getsession().setattribute("method",
new string("modifyrecord"));
return this.getshowmodifyforward(actionmapping);
}
//删除记录
public actionforward showdelete(actionmapping actionmapping,
actionform actionform,
httpservletrequest httpservletrequest,
httpservletresponse httpservletresponse) {
string str = httpservletrequest.getparameter("bookid").tostring();
if (operator.deleterecord(str) != -1) {
return this.getoperatorokforward(actionmapping);
}
else {
this.saveglobalerrors(httpservletrequest, "edit.body.error");
return this.geterrorforward(actionmapping);
}
}
public actionforward showfind(actionmapping actionmapping,
actionform actionform,
httpservletrequest httpservletrequest,
httpservletresponse httpservletresponse) {
//传递参数
httpservletrequest.getsession().setattribute("bookbean", new bookform());
httpservletrequest.getsession().setattribute("method",
new string("findrecord"));
return this.getshowfindforward(actionmapping);
}
}
以下是三个actionform文件:
package com.bookshop.form;
import org.apache.struts.action.actionform;
import org.apache.struts.action.actionerrors;
import org.apache.struts.action.actionerror;
import org.apache.struts.action.actionmapping;
import javax.servlet.http.httpservletrequest;
import java.util.map;
import java.util.hashmap;
public class bookform
extends actionform {
private string author;
private string bookid;
private string bookname;
private string price;
private string publish;
private string beanid;
public bookform() {
this.bookid = "";
this.bookname = "";
this.author = "";
this.publish = "";
this.price = "";
this.beanid = "";
}
public bookform(string id, string name, string author, string publish,
string price) {
this.bookid = id;
this.bookname = name;
this.author = author;
this.publish = publish;
this.price = price;
this.beanid = id;
}
public string getauthor() {
return author;
}
public void setauthor(string author) {
this.author = author;
}
public void setpublish(string publish) {
this.publish = publish;
}
public void setprice(string price) {
this.price = price;
}
public void setbookname(string bookname) {
this.bookname = bookname;
}
public void setbookid(string bookid) {
this.bookid = bookid;
}
public string getbookid() {
return bookid;
}
public string getbookname() {
return bookname;
}
public string getprice() {
return price;
}
public string getpublish() {
return publish;
}
public string getbeanid() {
return this.beanid;
}
public void setbeanid(string beanid) {
this.beanid = beanid;
}
public map loadbook() {
map record = new hashmap();
record.put("column1", this.getbookid().trim());
record.put("column2", this.getbookname().trim());
record.put("column3", this.getauthor().trim());
record.put("column4", this.getpublish().trim());
record.put("column5", this.getprice().trim());
return record;
}
public actionerrors validate(actionmapping actionmapping,
httpservletrequest httpservletrequest) {
actionerrors errors = new actionerrors();
if (this.bookid == null || this.bookid.equals("") ||
this.bookid.length() < 1) {
errors.add(actionerrors.global_error, new actionerror("book.bookid.error"));
}
if (this.bookname == null || this.bookname.equals("") ||
this.bookname.length() < 1) {
errors.add(actionerrors.global_error,
new actionerror("book.bookname.error"));
}
if (this.author == null || this.author.equals("") ||
this.author.length() < 1) {
errors.add(actionerrors.global_error, new actionerror("book.author.error"));
}
if (this.publish == null || this.publish.equals("") ||
this.publish.length() < 1) {
errors.add(actionerrors.global_error,
new actionerror("book.publish.error"));
}
// if ( (float.isnan(this.price)) && (this.price < 0)) {
if ( (float.isnan(float.parsefloat(this.price))) &&
(float.parsefloat(this.price) < 0)) {
errors.add(actionerrors.global_error, new actionerror("book.price.error"));
}
return errors;
}
}
/**/
package com.bookshop.form;
import org.apache.struts.action.actionform;
import org.apache.struts.action.actionerrors;
import org.apache.struts.action.actionerror;
import org.apache.struts.action.actionmapping;
import javax.servlet.http.httpservletrequest;
public class findrecordform
extends actionform {
private string findbykey;
private string findbyvalue;
public string getfindbykey() {
return findbykey;
}
public void setfindbykey(string findbykey) {
this.findbykey = findbykey;
}
public void setfindbyvalue(string findbyvalue) {
this.findbyvalue = findbyvalue;
}
public string getfindbyvalue() {
return findbyvalue;
}
public actionerrors validate(actionmapping actionmapping,
httpservletrequest httpservletrequest) {
/** @todo: finish this method, this is just the skeleton.*/
actionerrors errors = null;
if (this.findbykey.equals("") || this.findbyvalue.equals("")) {
errors = new actionerrors();
errors.add(actionerrors.global_error,
new actionerror("find.jsp.error"));
}
return errors;
}
public void reset(actionmapping actionmapping,
httpservletrequest servletrequest) {
}
}
/**/
package com.bookshop.form;
import org.apache.struts.action.actionform;
import org.apache.struts.action.actionerrors;
import org.apache.struts.action.actionmapping;
import javax.servlet.http.httpservletrequest;
import org.apache.struts.action.actionerror;
public class operatorform
extends actionform {
private string operator;
public string getoperator() {
return operator;
}
public void setoperator(string operator) {
this.operator = operator;
}
public actionerrors validate(actionmapping actionmapping,
httpservletrequest httpservletrequest) {
actionerrors errors = new actionerrors();
if (httpservletrequest.getparameter("operator") != null) {
string lang = httpservletrequest.getparameter("operator");
/* if ( (lang.length() < 6) || (lang.length() > 6)) {
errors.add(actionerrors.global_error, new actionerror("index.jsp.operator.error"));
}
*/
}
else {
errors.add(actionerrors.global_error,
new actionerror("index.jsp.operator.null"));
}
return errors;
}
public void reset(actionmapping actionmapping,
httpservletrequest servletrequest) {
}
}
以下是业务类和数据库访问类:
package com.bookshop.model;
import java.util.map;
import java.util.list;
import com.bookshop.util.*;
public class operator {
private static int recordperpage = applicationutil.recordperpage;
//动态sql
private static string sqlnumber = "";
//留出接口设置每页显示多少条记录
public static void setrecordperpage(int number) {
recordperpage = number;
}
public operator() {
}
//获得所有记录集(只查询一页记录)
public static list getrecords(int startindex) {
string sql = "select * from booktab limit ?,?";
sqlnumber = "select count(*) from booktab";
return dbutil.executequery(sql, startindex, recordperpage);
}
//按条件查找记录集(只查询一页记录)
public static list getrecords(string key, string value, int startindex) {
string sql = "select * from booktab where " + key + "='" + value +
"' limit ?,?";
sqlnumber = "select count(*) from booktab where " + key + "='" + value +
"'";
return dbutil.executequery(sql, startindex, recordperpage);
}
//查询单条记录 用于修改
public static bookbean getrecord(string value) {
string sql = "select * from booktab where bookid='" + value + "'";
bookbean book = new bookbean();
book = dbutil.execquery(sql);
return book;
}
//添加一条新记录
public static int addrecord(map newrecord) {
string sql =
"insert into booktab(bookname,author,publish,price,bookid)values(?,?,?,?,?)";
return dbutil.execupdate(sql, newrecord);
}
//修改指定的记录
public static int modifyrecord(map newrecord) {
string sql =
"update booktab set bookname=?,author=?,publish=?,price=? where bookid=?";
return dbutil.execupdate(sql, newrecord);
}
//删除指定的记录
public static int deleterecord(string value) {
string sql =
"delete from booktab where bookid='" + value + "'";
return dbutil.execupdate(sql);
}
//获得表中所有记录的条数
public static int getrecordsnumber() {
return dbutil.executequery(sqlnumber);
}
/*
public static void main(string[] args) {
operator operator = new operator();
}
*/
}
/**/
package com.bookshop.util;
import javax.servlet.http.httpservletrequest;
public class applicationutil {
public applicationutil() {
}
public static final string driver = "org.gjt.mm.mysql.driver";
public static final string url ="jdbc:mysql://localhost/bookshop";
public static final string user = "root";
public static final string password = "";
public static final int recordperpage = 5;
public static string togbk(string s) {
try {
return new string(s.getbytes("iso-8859-1"), "gb2312");
}
catch (exception ex) {
return "";
}
}
public static string getselfurl(httpservletrequest req) {
string s1 = req.getrequesturi();
string s2 = req.getquerystring();
if (s2 != null) {
s1 = s1 + "?" + s2;
}
return s1;
}
public static void setparam(string param, string value, java.util.hashmap map) {
string[] p = param.split(";");
string[] v = value.split(";");
for (int i = 0; i < p.length; i++) {
map.put(p[i], v[i]);
}
}
}
/**/
package com.bookshop.util;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.jspexception;
import javax.servlet.jsp.pagecontext;
public class islasttag
extends tagsupport {
private string page = "";
public islasttag() {
}
public void setpage(string page) {
this.page = page;
}
public string getpage() {
return this.page;
}
public int dostarttag() throws jspexception {
if (this.page != null) {
//从session里面取出来的是pageinfo对象的引用
pageinfo pagebean = new pageinfo();
pagebean = (pageinfo) (pagecontext.getsession().getattribute(this.
page));
//只要该pageinfo对象的总页数等于当前页数就不处理主体部分
if (pagebean.getpagecountnumber() <= pagebean.getcurrentlypage()) {
return this.skip_body;
}
else {
return this.eval_page;//否则继续处理主体部分
}
}
else {
return this.skip_body;
}
}
}
/**/
package com.bookshop.util;
import java.util.list;
import java.util.arraylist;
import java.sql.drivermanager;
import java.sql.connection;
import java.sql.statement;
import java.sql.preparedstatement;
import java.sql.resultset;
import java.sql.sqlexception;
import java.util.hashmap;
import java.util.map;
public class dbutil {
public dbutil() {
}
private static string driver = applicationutil.driver;
private static string url = applicationutil.url;
private static string user = applicationutil.user;
private static string password = applicationutil.password;
private static list list = null;
private static connection con = null;
private static statement sta = null;
private static preparedstatement psta = null;
private static resultset res = null;
//获得满足查询条件的记录行数
public static int executequery(string sql) {
int countnum = 0;
try {
execute(sql);
while (res.next()) {
countnum = res.getint(1);
}
}
catch (exception e) {
e.printstacktrace();
}
finally {
close();
return countnum;
}
}
//删除记录
public static int execupdate(string sql) {
int i = -1;
try {
getstatement();
i = sta.executeupdate(sql);
}
catch (exception e) {
e.printstacktrace();
}
finally {
close();
return i;
}
}
//添加新记录
public static int execupdate(string sql, map newrecord) {
int i = -1;
try {
getpreparedstatement(sql);
if (newrecord != null && !newrecord.isempty()) {
psta.setstring(1, (string) newrecord.get("column2"));
psta.setstring(2, (string) newrecord.get("column3"));
psta.setstring(3, (string) newrecord.get("column4"));
psta.setstring(4, (string) newrecord.get("column5"));
psta.setstring(5, (string) newrecord.get("column1"));
}
i = psta.executeupdate();
}
catch (exception e) {
e.printstacktrace();
}
finally {
close();
return i;
}
}
//查询单个记录(用于修改)
public static bookbean execquery(string sql) {
bookbean book = null;
try {
execute(sql);
while (res.next()) {
book = new bookbean(applicationutil.togbk(res.getstring(1)),
applicationutil.togbk(res.getstring(2)),
applicationutil.togbk(res.getstring(3)),
applicationutil.togbk(res.getstring(4)),
applicationutil.togbk(res.getstring(5))
);
}
}
catch (exception e) {
e.printstacktrace();
}
finally {
close();
return book;
}
}
//查询记录(只查询指定页要显示的记录)
public static list executequery(string sql, int startindex, int count) {
try {
list = new arraylist();
getresultset(sql, startindex, count);
while (res.next()) {
list.add(new bookbean(applicationutil.togbk(res.getstring(1)),
applicationutil.togbk(res.getstring(2)),
applicationutil.togbk(res.getstring(3)),
applicationutil.togbk(res.getstring(4)),
applicationutil.togbk(res.getstring(5))
));
}
}
catch (exception e) {
e.printstacktrace();
}
finally {
close();
return list;
}
}
private static void getconnection() throws exception {
class.forname(driver);
con = drivermanager.getconnection(url, user, password);
//con.setautocommit(false);
}
private static void getpreparedstatement(string sql) throws exception {
getconnection();
psta = con.preparestatement(sql);
}
private static void execute(string sql) throws exception {
getstatement();
res = sta.executequery(sql);
}
private static void getstatement() throws exception {
getconnection();
sta = con.createstatement();
}
private static void getresultset(string sql, int startindex, int count) throws
exception {
getpreparedstatement(sql);
psta.setint(1, startindex);
psta.setint(2, count);
res = psta.executequery();
}
//释放资源
private static void close() {
try {
/*
if(con!=null){
con.commit();
}
*/
if (res != null) {
res.close();
}
if (psta != null) {
psta.close();
}
if (sta != null) {
sta.close();
}
if (con != null) {
con.close();
}
}
catch (sqlexception e) {
e.printstacktrace();
}
}
}
/**/
package com.bookshop.util;
import java.io.serializable;
public class bookbean
implements serializable {
private string author = "";
private string bookid = "";
private string bookname = "";
private string price = "";
private string publish = "";
public bookbean() {
this.bookid = "";
this.bookname = "";
this.author = "";
this.publish = "";
this.price = "";
}
public bookbean(string bookid, string bookname, string author, string publish,
&
新闻热点
疑难解答