这个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"