|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
还是要自己一点一点写代码,然后编译,改错再编译好那。还有最重要的是.net的编译环境非常好,你甚是不需要了解太多工具,对于简单的系统,你可以之了解一些语法就哦了。分页这个DEMO供人人一同切磋进修Struts,由于事情太累,没精神给人人注释完成道理。假如看不懂,不妨。只是申明JSP基本还没有到火候,不要心急,归去强化下JSP+Servlet,基本踏实了,天然可以看懂我写的代码。这个DEMO自创了网上良多后人的履历,在此一并感谢。
web.xml文件:
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEweb-appPUBLIC"-//SunMicrosystems,Inc.//DTDWebApplication2.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文件:
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstruts-configPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration1.1//EN""http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-beanname="bookForm"type="com.bookshop.form.BookForm"/>
<form-beanname="operatorForm"type="com.bookshop.form.OperatorForm"/>
<form-beanname="findRecordForm"type="com.bookshop.form.FindRecordForm"/>
</form-beans>
<global-forwards>
<forwardname="index"path="/index.jsp"/>
<forwardname="browser"path="/show.jsp"/>
<forwardname="global_error"path="/error.jsp"/>
</global-forwards>
<action-mappings>
<actioninput="/show.jsp"name="bookForm"parameter="operator"path="/operatorAction"scope="session"type="com.bookshop.action.OperatorAction"validate="false">
<forwardname="operatorok"path="/success.jsp"redirect="true"/>
<forwardname="showFirstPage"path="/operatorAction.do?operator=showFirstPage"/>
<forwardname="showPreviousPage"path="/operatorAction.do?operator=showPreviousPage"/>
<forwardname="showNextPage"path="/operatorAction.do?operator=showNextPage"/>
<forwardname="showLastPage"path="/operatorAction.do?operator=showLastPage"/>
<forwardname="showAddRecord"path="/editrecord.jsp?operator=addRecord"redirect="true"/>
<forwardname="showModifyRecord"path="/editrecord.jsp?operator=modifyRecord"/>
<forwardname="showFindRecord"path="/findrecord.jsp"redirect="true"/>
</action>
<actioninput="/findrecord.jsp"name="findRecordForm"path="/findRecordAction"scope="session"type="com.bookshop.action.FindRecordAction"validate="false"/>
</action-mappings>
<plug-inclassName="org.apache.struts.tiles.TilesPlugin">
<set-propertyproperty="definitions-config"value="/WEB-INF/tiles-defs.xml"/>
</plug-in>
</struts-config>
tiles-defs文件:
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEtiles-definitionsPUBLIC"-//ApacheSoftwareFoundation//DTDTilesConfiguration1.1//EN""http://jakarta.apache.org/struts/dtds/tiles-config_1_1.dtd">
<tiles-definitions>
<definitionname="base-definition"path="layout.jsp">
<putname="head"value="head.jsp"/>
<putname="left"value="left.jsp"/>
<putname="body"/>
<putname="foot"value="foot.jsp"/>
</definition>
<definitionextends="base-definition"name="index-definition">
<putname="body"value="index_body.jsp"/>
</definition>
<definitionextends="base-definition"name="show-definition">
<putname="body"value="show_body.jsp"/>
</definition>
<definitionextends="base-definition"name="edit-definition">
<putname="body"value="edit_body.jsp"/>
</definition>
<definitionextends="base-definition"name="find-definition">
<putname="body"value="find_body.jsp"/>
</definition>
<definitionextends="base-definition"name="success-definition">
<putname="body"value="success_body.jsp"/>
</definition>
<definitionextends="base-definition"name="error-definition">
<putname="body"value="error_body.jsp"/>
</definition>
</tiles-definitions>
camel-define文件:
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEtaglibPUBLIC"-//SunMicrosystems,Inc.//DTDJSPTagLibrary1.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*/
packagecom.bookshop.action;
importorg.apache.struts.action.ActionMapping;
importorg.apache.struts.action.ActionForm;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.apache.struts.action.ActionForward;
importcom.bookshop.form.FindRecordForm;
importorg.apache.struts.action.Action;
importjava.util.List;
importjava.util.ArrayList;
importcom.bookshop.model.Operator;
importcom.bookshop.util.PageInfo;
importorg.apache.struts.action.ActionErrors;
importorg.apache.struts.action.ActionError;
publicclassFindRecordAction
extendsAction{
publicActionForwardexecute(ActionMappingactionMapping,
ActionFormactionForm,
HttpServletRequestservletRequest,
HttpServletResponseservletResponse){
FindRecordFormfindRecordForm=(FindRecordForm)actionForm;
Stringkey=findRecordForm.getFindByKey().trim();
Stringvalue=findRecordForm.getFindByValue().trim();
Listlist=newArrayList();
list=Operator.getRecords(key,value,0);
servletRequest.getSession().setAttribute("books",list);
if(!list.isEmpty()){
servletRequest.getSession().setAttribute("pageinfo",
newPageInfo(Operator.
getRecordsNumber(),1));
}
else{
ActionErrorsmessages=newActionErrors();
messages.add(ActionErrors.GLOBAL_MESSAGE,
newActionError("findrecord.jsp.notfound"));
servletRequest.getSession().setAttribute("pageinfo",
newPageInfo(0,1));
}
returnactionMapping.findForward("browser");
}
}
/*GenericAction.java*/
packagecom.bookshop.action;
importorg.apache.struts.action.ActionMapping;
importorg.apache.struts.action.ActionForm;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.apache.struts.action.ActionForward;
importorg.apache.struts.actions.DispatchAction;
importorg.apache.struts.action.ActionErrors;
importorg.apache.struts.action.ActionError;
publicclassGenericAction
extendsDispatchAction{
/*
publicActionForwardexecute(ActionMappingactionMapping,
ActionFormactionForm,
HttpServletRequestservletRequest,
HttpServletResponseservletResponse){
thrownewjava.lang.UnsupportedOperationException(
"Method$execute()notyetimplemented.");
}
*/
publicvoidsaveGlobalErrors(HttpServletRequesthttpServletRequest,
StringerrorKey){
ActionErrorserrors=newActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR,newActionError(errorKey));
if(errors!=null){
saveErrors(httpServletRequest,errors);
}
}
publicActionForwardgetIndexForward(ActionMappingactionMapping){
returnactionMapping.findForward("index");
}
publicActionForwardgetBrowserForward(ActionMappingactionMapping){
returnactionMapping.findForward("browser");
}
publicActionForwardshowDeleteForward(ActionMappingactionMapping){
returnactionMapping.findForward("showDelete");
}
publicActionForwardgetOperatorOkForward(ActionMappingactionMapping){
returnactionMapping.findForward("operatorok");
}
publicActionForwardgetErrorForward(ActionMappingactionMapping){
returnactionMapping.findForward("global_error");
}
publicActionForwardgetShowAddForward(ActionMappingactionMapping){
returnactionMapping.findForward("showAddRecord");
}
publicActionForwardgetShowModifyForward(ActionMappingactionMapping){
returnactionMapping.findForward("showModifyRecord");
}
publicActionForwardgetShowDeleteForward(ActionMappingactionMapping){
returnactionMapping.findForward("showDeleteRecord");
}
publicActionForwardgetShowFindForward(ActionMappingactionMapping){
returnactionMapping.findForward("showFindRecord");
}
}
/*OperatorAction.java*/
packagecom.bookshop.action;
importorg.apache.struts.action.ActionMapping;
importorg.apache.struts.action.ActionForm;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.apache.struts.action.ActionForward;
importcom.bookshop.form.OperatorForm;
importorg.apache.struts.action.Action;
importjava.util.List;
importorg.apache.struts.Globals;
importcom.bookshop.util.DBUtil;
importcom.bookshop.util.ApplicationUtil;
importcom.bookshop.model.Operator;
importjava.util.ArrayList;
importcom.bookshop.util.PageInfo;
importorg.apache.struts.actions.DispatchAction;
importjava.util.Map;
importjava.util.HashMap;
importcom.bookshop.form.BookForm;
importjava.util.Locale;
importorg.apache.struts.action.ActionErrors;
importorg.apache.struts.action.ActionError;
importcom.bookshop.util.BookBean;
publicclassOperatorAction
extendsGenericAction{
/*
publicActionForwardexecute(ActionMappingactionMapping,
ActionFormactionForm,
HttpServletRequestservletRequest,
HttpServletResponseservletResponse){
thrownewjava.lang.UnsupportedOperationException(
"Method$execute()notyetimplemented.");
}
*/
//转换为中文页面
publicActionForwardChangeCH(ActionMappingactionMapping,
ActionFormactionForm,
HttpServletRequestservletRequest,
HttpServletResponseservletResponse){
servletRequest.getSession().setAttribute(Globals.LOCALE_KEY,Locale.CHINA);
returnthis.getIndexForward(actionMapping);
}
//转换为英文页面
publicActionForwardChangeEN(ActionMappingactionMapping,
ActionFormactionForm,
HttpServletRequestservletRequest,
HttpServletResponseservletResponse){
servletRequest.getSession().setAttribute(Globals.LOCALE_KEY,Locale.ENGLISH);
returnthis.getIndexForward(actionMapping);
}
//链接到首页纪录
publicActionForwardshowFirstPage(ActionMappingactionMapping,
ActionFormactionForm,
HttpServletRequesthttpServletRequest,
HttpServletResponsehttpServletResponse){
Listlist=newArrayList();
list=Operator.getRecords(0);
httpServletRequest.getSession().setAttribute("books",list);
httpServletRequest.getSession().setAttribute("pageinfo",
newPageInfo(Operator.
getRecordsNumber(),1));
returnthis.getBrowserForward(actionMapping);
}
//链接到上一页纪录
publicActionForwardshowPreviousPage(ActionMappingactionMapping,
ActionFormactionForm,
HttpServletRequesthttpServletRequest,
HttpServletResponsehttpServletResponse){
Listlist=newArrayList();
PageInfopageInfo=(PageInfo)httpServletRequest.getSession().getAttribute(
"pageinfo");
list=Operator.getRecords((pageInfo.getPreviousPageNumber()-1)*
ApplicationUtil.recordPerPage);
httpServletRequest.getSession().setAttribute("books",list);
httpServletRequest.getSession().setAttribute("pageinfo",
newPageInfo(Operator.
getRecordsNumber(),pageInfo.getPreviousPageNumber()));
returnthis.getBrowserForward(actionMapping);
}
//链接到下一页纪录
publicActionForwardshowNextPage(ActionMappingactionMapping,
ActionFormactionForm,
HttpServletRequesthttpServletRequest,
HttpServletResponsehttpServletResponse){
Listlist=newArrayList();
PageInfopageInfo=(PageInfo)httpServletRequest.getSession().getAttribute(
"pageinfo");
list=Operator.getRecords(pageInfo.getCurrentlyPage()*
ApplicationUtil.recordPerPage);
httpServletRequest.getSession().setAttribute("books",list);
httpServletRequest.getSession().setAttribute("pageinfo",
newPageInfo(Operator.
getRecordsNumber(),pageInfo.getNextPageNumber()));
returnthis.getBrowserForward(actionMapping);
}
//链接到末页纪录
publicActionForwardshowLastPage(ActionMappingactionMapping,
ActionFormactionForm,
HttpServletRequesthttpServletRequest,
HttpServletResponsehttpServletResponse){
Listlist=newArrayList();
PageInfopageInfo=(PageInfo)httpServletRequest.getSession().getAttribute(
"pageinfo");
list=Operator.getRecords((pageInfo.getPageCountNumber()-1)*
ApplicationUtil.recordPerPage);
httpServletRequest.getSession().setAttribute("books",list);
httpServletRequest.getSession().setAttribute("pageinfo",
newPageInfo(Operator.
getRecordsNumber(),pageInfo.getLastPageNumber()));
returnthis.getBrowserForward(actionMapping);
}
//作废操纵的转向
publicActionForwardcancel(ActionMappingactionMapping,
ActionFormactionForm,
HttpServletRequesthttpServletRequest,
HttpServletResponsehttpServletResponse){
if(isCancelled(httpServletRequest)){
returnthis.getOperatorOkForward(actionMapping);
}
returnnull;
}
//检察一切纪录
publicActionForwardbrowser(ActionMappingactionMapping,
ActionFormactionForm,
HttpServletRequesthttpServletRequest,
HttpServletResponsehttpServletResponse){
returnthis.showFirstPage(actionMapping,actionForm,httpServletRequest,
httpServletResponse);
}
//实行增加纪录
publicActionForwardaddRecord(ActionMappingactionMapping,
ActionFormactionForm,
HttpServletRequesthttpServletRequest,
HttpServletResponsehttpServletResponse){
BookFormbookForm=(BookForm)actionForm;
if(Operator.addRecord(bookForm.loadBook())>=1){
returnthis.getOperatorOkForward(actionMapping);
}
else{
this.saveGlobalErrors(httpServletRequest,"editrecord.jsp.adderror");
returnthis.getErrorForward(actionMapping);
}
}
//提交更新操纵
publicActionForwardSubmitRecord(ActionMappingactionMapping,
ActionFormactionForm,
HttpServletRequesthttpServletRequest,
HttpServletResponsehttpServletResponse){
Stringstr=(String)httpServletRequest.getSession().getAttribute("method");
if(str.equals("addRecord")){
returnaddRecord(actionMapping,actionForm,httpServletRequest,
httpServletResponse);
}
if(str.equals("modifyRecord")){
returnmodifyRecord(actionMapping,actionForm,httpServletRequest,
httpServletResponse);
}
else{
this.saveGlobalErrors(httpServletRequest,"edit.body.error");
returnthis.getErrorForward(actionMapping);
}
}
//实行修正操纵
publicActionForwardmodifyRecord(ActionMappingactionMapping,
ActionFormactionForm,
HttpServletRequesthttpServletRequest,
HttpServletResponsehttpServletResponse){
BookFormbookForm=(BookForm)actionForm;
if(Operator.modifyRecord(bookForm.loadBook())!=-1){
returnthis.getOperatorOkForward(actionMapping);
}
else{
this.saveGlobalErrors(httpServletRequest,"editrecord.jsp.modifyerror");
returnthis.getErrorForward(actionMapping);
}
}
//跳转到增加纪录编纂页面
publicActionForwardshowAdd(ActionMappingactionMapping,
ActionFormactionForm,
HttpServletRequesthttpServletRequest,
HttpServletResponsehttpServletResponse){
httpServletRequest.getSession().setAttribute("bookBean",newBookForm());
httpServletRequest.getSession().setAttribute("method",
newString("addRecord"));
returnthis.getShowAddForward(actionMapping);
}
//跳转到修正纪录编纂页面
publicActionForwardshowModify(ActionMappingactionMapping,
ActionFormactionForm,
HttpServletRequesthttpServletRequest,
HttpServletResponsehttpServletResponse){
BookBeanbook=newBookBean();
Stringstr=httpServletRequest.getParameter("bookid").toString();
book=Operator.getRecord(str);
httpServletRequest.getSession().setAttribute("bookBean",
newBookForm(book.getBookId(),
book.getBookName(),book.getAuthor(),book.getPublish(),book.getPrice()));
httpServletRequest.getSession().setAttribute("method",
newString("modifyRecord"));
returnthis.getShowModifyForward(actionMapping);
}
//删除纪录
publicActionForwardshowDelete(ActionMappingactionMapping,
ActionFormactionForm,
HttpServletRequesthttpServletRequest,
HttpServletResponsehttpServletResponse){
Stringstr=httpServletRequest.getParameter("bookid").toString();
if(Operator.deleteRecord(str)!=-1){
returnthis.getOperatorOkForward(actionMapping);
}
else{
this.saveGlobalErrors(httpServletRequest,"edit.body.error");
returnthis.getErrorForward(actionMapping);
}
}
publicActionForwardshowFind(ActionMappingactionMapping,
ActionFormactionForm,
HttpServletRequesthttpServletRequest,
HttpServletResponsehttpServletResponse){
//传送参数
httpServletRequest.getSession().setAttribute("bookBean",newBookForm());
httpServletRequest.getSession().setAttribute("method",
newString("findRecord"));
returnthis.getShowFindForward(actionMapping);
}
}
以下是三个ActionForm文件:
packagecom.bookshop.form;
importorg.apache.struts.action.ActionForm;
importorg.apache.struts.action.ActionErrors;
importorg.apache.struts.action.ActionError;
importorg.apache.struts.action.ActionMapping;
importjavax.servlet.http.HttpServletRequest;
importjava.util.Map;
importjava.util.HashMap;
publicclassBookForm
extendsActionForm{
privateStringauthor;
privateStringbookId;
privateStringbookName;
privateStringprice;
privateStringpublish;
privateStringbeanId;
publicBookForm(){
this.bookId="";
this.bookName="";
this.author="";
this.publish="";
this.price="";
this.beanId="";
}
publicBookForm(Stringid,Stringname,Stringauthor,Stringpublish,
Stringprice){
this.bookId=id;
this.bookName=name;
this.author=author;
this.publish=publish;
this.price=price;
this.beanId=id;
}
publicStringgetAuthor(){
returnauthor;
}
publicvoidsetAuthor(Stringauthor){
this.author=author;
}
publicvoidsetPublish(Stringpublish){
this.publish=publish;
}
publicvoidsetPrice(Stringprice){
this.price=price;
}
publicvoidsetBookName(StringbookName){
this.bookName=bookName;
}
publicvoidsetBookId(StringbookId){
this.bookId=bookId;
}
publicStringgetBookId(){
returnbookId;
}
publicStringgetBookName(){
returnbookName;
}
publicStringgetPrice(){
returnprice;
}
publicStringgetPublish(){
returnpublish;
}
publicStringgetBeanId(){
returnthis.beanId;
}
publicvoidsetBeanId(StringbeanId){
this.beanId=beanId;
}
publicMaploadBook(){
Maprecord=newHashMap();
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());
returnrecord;
}
publicActionErrorsvalidate(ActionMappingactionMapping,
HttpServletRequesthttpServletRequest){
ActionErrorserrors=newActionErrors();
if(this.bookId==null||this.bookId.equals("")||
this.bookId.length()<1){
errors.add(ActionErrors.GLOBAL_ERROR,newActionError("book.bookid.error"));
}
if(this.bookName==null||this.bookName.equals("")||
this.bookName.length()<1){
errors.add(ActionErrors.GLOBAL_ERROR,
newActionError("book.bookname.error"));
}
if(this.author==null||this.author.equals("")||
this.author.length()<1){
errors.add(ActionErrors.GLOBAL_ERROR,newActionError("book.author.error"));
}
if(this.publish==null||this.publish.equals("")||
this.publish.length()<1){
errors.add(ActionErrors.GLOBAL_ERROR,
newActionError("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,newActionError("book.price.error"));
}
returnerrors;
}
}
/**/
packagecom.bookshop.form;
importorg.apache.struts.action.ActionForm;
importorg.apache.struts.action.ActionErrors;
importorg.apache.struts.action.ActionError;
importorg.apache.struts.action.ActionMapping;
importjavax.servlet.http.HttpServletRequest;
publicclassFindRecordForm
extendsActionForm{
privateStringfindByKey;
privateStringfindByValue;
publicStringgetFindByKey(){
returnfindByKey;
}
publicvoidsetFindByKey(StringfindByKey){
this.findByKey=findByKey;
}
publicvoidsetFindByValue(StringfindByValue){
this.findByValue=findByValue;
}
publicStringgetFindByValue(){
returnfindByValue;
}
publicActionErrorsvalidate(ActionMappingactionMapping,
HttpServletRequesthttpServletRequest){
/**@todo:finishthismethod,thisisjusttheskeleton.*/
ActionErrorserrors=null;
if(this.findByKey.equals("")||this.findByValue.equals("")){
errors=newActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR,
newActionError("find.jsp.error"));
}
returnerrors;
}
publicvoidreset(ActionMappingactionMapping,
HttpServletRequestservletRequest){
}
}
/**/
packagecom.bookshop.form;
importorg.apache.struts.action.ActionForm;
importorg.apache.struts.action.ActionErrors;
importorg.apache.struts.action.ActionMapping;
importjavax.servlet.http.HttpServletRequest;
importorg.apache.struts.action.ActionError;
publicclassOperatorForm
extendsActionForm{
privateStringoperator;
publicStringgetOperator(){
returnoperator;
}
publicvoidsetOperator(Stringoperator){
this.operator=operator;
}
publicActionErrorsvalidate(ActionMappingactionMapping,
HttpServletRequesthttpServletRequest){
ActionErrorserrors=newActionErrors();
if(httpServletRequest.getParameter("operator")!=null){
Stringlang=httpServletRequest.getParameter("operator");
/*if((lang.length()<6)||(lang.length()>6)){
errors.add(ActionErrors.GLOBAL_ERROR,newActionError("index.jsp.operator.error"));
}
*/
}
else{
errors.add(ActionErrors.GLOBAL_ERROR,
newActionError("index.jsp.operator.null"));
}
returnerrors;
}
publicvoidreset(ActionMappingactionMapping,
HttpServletRequestservletRequest){
}
}
以下是营业类和数据库会见类:
packagecom.bookshop.model;
importjava.util.Map;
importjava.util.List;
importcom.bookshop.util.*;
publicclassOperator{
privatestaticintrecordPerPage=ApplicationUtil.recordPerPage;
//静态SQL
privatestaticStringsqlNumber="";
//留出接口设置每页显现几笔记录
publicstaticvoidsetRecordPerPage(intnumber){
recordPerPage=number;
}
publicOperator(){
}
//取得一切纪录集(只查询一页纪录)
publicstaticListgetRecords(intstartIndex){
Stringsql="select*frombooktablimit?,?";
sqlNumber="selectcount(*)frombooktab";
returnDBUtil.executeQuery(sql,startIndex,recordPerPage);
}
//按前提查找纪录集(只查询一页纪录)
publicstaticListgetRecords(Stringkey,Stringvalue,intstartIndex){
Stringsql="select*frombooktabwhere"+key+"="+value+
"limit?,?";
sqlNumber="selectcount(*)frombooktabwhere"+key+"="+value+
"";
returnDBUtil.executeQuery(sql,startIndex,recordPerPage);
}
//查询单笔记录用于修正
publicstaticBookBeangetRecord(Stringvalue){
Stringsql="select*frombooktabwherebookid="+value+"";
BookBeanbook=newBookBean();
book=DBUtil.execQuery(sql);
returnbook;
}
//增加一条新纪录
publicstaticintaddRecord(MapnewRecord){
Stringsql=
"insertintobooktab(bookname,author,publish,price,bookid)values(?,?,?,?,?)";
returnDBUtil.execUpdate(sql,newRecord);
}
//修正指定的纪录
publicstaticintmodifyRecord(MapnewRecord){
Stringsql=
"updatebooktabsetbookname=?,author=?,publish=?,price=?wherebookid=?";
returnDBUtil.execUpdate(sql,newRecord);
}
//删除指定的纪录
publicstaticintdeleteRecord(Stringvalue){
Stringsql=
"deletefrombooktabwherebookid="+value+"";
returnDBUtil.execUpdate(sql);
}
//取得表中一切纪录的条数
publicstaticintgetRecordsNumber(){
returnDBUtil.executeQuery(sqlNumber);
}
/*
publicstaticvoidmain(String[]args){
Operatoroperator=newOperator();
}
*/
}
/**/
packagecom.bookshop.util;
importjavax.servlet.http.HttpServletRequest;
publicclassApplicationUtil{
publicApplicationUtil(){
}
publicstaticfinalStringdriver="org.gjt.mm.mysql.Driver";
publicstaticfinalStringurl="jdbc:mysql://localhost/bookshop";
publicstaticfinalStringuser="root";
publicstaticfinalStringpassword="";
publicstaticfinalintrecordPerPage=5;
publicstaticStringtoGBK(Strings){
try{
returnnewString(s.getBytes("ISO-8859-1"),"gb2312");
}
catch(Exceptionex){
return"";
}
}
publicstaticStringgetSelfURL(HttpServletRequestreq){
Strings1=req.getRequestURI();
Strings2=req.getQueryString();
if(s2!=null){
s1=s1+"?"+s2;
}
returns1;
}
publicstaticvoidsetParam(Stringparam,Stringvalue,java.util.HashMapmap){
String[]p=param.split(";");
String[]v=value.split(";");
for(inti=0;i<p.length;i++){
map.put(p,v);
}
}
}
/**/
packagecom.bookshop.util;
importjavax.servlet.jsp.tagext.*;
importjavax.servlet.jsp.JspException;
importjavax.servlet.jsp.PageContext;
publicclassIsLastTag
extendsTagSupport{
privateStringpage="";
publicIsLastTag(){
}
publicvoidsetPage(Stringpage){
this.page=page;
}
publicStringgetPage(){
returnthis.page;
}
publicintdoStartTag()throwsJspException{
if(this.page!=null){
//从session内里掏出来的是PageInfo对象的援用
PageInfopageBean=newPageInfo();
pageBean=(PageInfo)(pageContext.getSession().getAttribute(this.
page));
//只需该PageInfo对象的总页数即是以后页数就不处置主体部分
if(pageBean.getPageCountNumber()<=pageBean.getCurrentlyPage()){
returnthis.SKIP_BODY;
}
else{
returnthis.EVAL_PAGE;//不然持续处置主体部分
}
}
else{
returnthis.SKIP_BODY;
}
}
}
/**/
packagecom.bookshop.util;
importjava.util.List;
importjava.util.ArrayList;
importjava.sql.DriverManager;
importjava.sql.Connection;
importjava.sql.Statement;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.util.HashMap;
importjava.util.Map;
publicclassDBUtil{
publicDBUtil(){
}
privatestaticStringdriver=ApplicationUtil.driver;
privatestaticStringurl=ApplicationUtil.url;
privatestaticStringuser=ApplicationUtil.user;
privatestaticStringpassword=ApplicationUtil.password;
privatestaticListlist=null;
privatestaticConnectioncon=null;
privatestaticStatementsta=null;
privatestaticPreparedStatementpsta=null;
privatestaticResultSetres=null;
//取得满意查询前提的纪录行数
publicstaticintexecuteQuery(Stringsql){
intcountNum=0;
try{
execute(sql);
while(res.next()){
countNum=res.getInt(1);
}
}
catch(Exceptione){
e.printStackTrace();
}
finally{
close();
returncountNum;
}
}
//删除纪录
publicstaticintexecUpdate(Stringsql){
inti=-1;
try{
getStatement();
i=sta.executeUpdate(sql);
}
catch(Exceptione){
e.printStackTrace();
}
finally{
close();
returni;
}
}
//增加新纪录
publicstaticintexecUpdate(Stringsql,MapnewRecord){
inti=-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(Exceptione){
e.printStackTrace();
}
finally{
close();
returni;
}
}
//查询单个纪录(用于修正)
publicstaticBookBeanexecQuery(Stringsql){
BookBeanbook=null;
try{
execute(sql);
while(res.next()){
book=newBookBean(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(Exceptione){
e.printStackTrace();
}
finally{
close();
returnbook;
}
}
//查询纪录(只查询指定页要显现的纪录)
publicstaticListexecuteQuery(Stringsql,intstartIndex,intcount){
try{
list=newArrayList();
getResultSet(sql,startIndex,count);
while(res.next()){
list.add(newBookBean(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(Exceptione){
e.printStackTrace();
}
finally{
close();
returnlist;
}
}
privatestaticvoidgetConnection()throwsException{
Class.forName(driver);
con=DriverManager.getConnection(url,user,password);
//con.setAutoCommit(false);
}
privatestaticvoidgetPreparedStatement(Stringsql)throwsException{
getConnection();
psta=con.prepareStatement(sql);
}
privatestaticvoidexecute(Stringsql)throwsException{
getStatement();
res=sta.executeQuery(sql);
}
privatestaticvoidgetStatement()throwsException{
getConnection();
sta=con.createStatement();
}
privatestaticvoidgetResultSet(Stringsql,intstartIndex,intcount)throws
Exception{
getPreparedStatement(sql);
psta.setInt(1,startIndex);
psta.setInt(2,count);
res=psta.executeQuery();
}
<P>//开释资本
privatestaticvoidclose(){
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(SQLExceptione){
e.printStackTrace(); |
|