?? 5.mvn
字號:
這個DEMO供大家一起探討學習Struts,因為工作太累,沒精力給大家解釋實現(xiàn)原理。如果看不懂,沒關(guān)系。只是說明JSP基礎(chǔ)還沒有到火候,不要心急,回去強化下JSP+Servlet,基礎(chǔ)扎實了,自然能夠看懂我寫的代碼。這個DEMO借鑒了網(wǎng)上很多前人的經(jīng)驗,在此一并謝謝。
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.");
}
*/
//轉(zhuǎn)換為中文頁面
public ActionForward ChangeCH(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest servletRequest,
HttpServletResponse servletResponse) {
servletRequest.getSession().setAttribute(Globals.LOCALE_KEY, Locale.CHINA);
return this.getIndexForward(actionMapping);
}
//轉(zhuǎn)換為英文頁面
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);
}
//取消操作的轉(zhuǎn)向
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);
}
//執(zhí)行添加記錄
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);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -