這裏顯示二個版本的差異處。
| — |
eclipse_struts1 [2011/03/21 13:21] (目前版本) bestlong 建立 |
||
|---|---|---|---|
| 行 1: | 行 1: | ||
| + | ====== 在 Eclipse 使用 Struts 1.3.x ====== | ||
| + | ===== 建立支援 Struts 的專案環境 ===== | ||
| + | 1. Eclipse 中建立一個 Dynamic Web Project 然後 | ||
| + | |||
| + | 2. 將 Struts 的 lib 下的檔案都複製到 WebContent/WEB-INF/lib 目錄下 | ||
| + | |||
| + | 3. 將 Struts 的 src\taglib\src\main\resources\META-INF\tld 下的 *.tld 複製到 WebContent/WEB-INF/lib | ||
| + | |||
| + | 4. 在 WebContent/WEB-INF 目錄下建立 struts-config.xml 內容如下: | ||
| + | |||
| + | <?xml version="1.0" encoding="ISO-8859-1" ?> | ||
| + | <!DOCTYPE struts-config PUBLIC | ||
| + | "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" | ||
| + | "http://struts.apache.org/dtds/struts-config_1_3.dtd"> | ||
| + | <struts-config> | ||
| + | <form-beans /> | ||
| + | <global-exceptions /> | ||
| + | <global-forwards /> | ||
| + | <action-mappings /> | ||
| + | <message-resources parameter="struts.ApplicationResources" /> | ||
| + | </struts-config> | ||
| + | |||
| + | 5. 修改 WebContent/WEB-INF/web.xml 增加處理 Struts 動作的 ActionServlet 的設定,內容下: | ||
| + | |||
| + | <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>3</param-value> | ||
| + | </init-param> | ||
| + | <init-param> | ||
| + | <param-name>detail</param-name> | ||
| + | <param-value>3</param-value> | ||
| + | </init-param> | ||
| + | <load-on-startup>0</load-on-startup> | ||
| + | </servlet> | ||
| + | <servlet-mapping> | ||
| + | <servlet-name>action</servlet-name> | ||
| + | <url-pattern>*.do</url-pattern> | ||
| + | </servlet-mapping> | ||
| + | |||
| + | ===== 建立首頁(index.jsp) ===== | ||
| + | 首先在 WebContent/ 下建立 index.jsp 檔案,修改內容如下: | ||
| + | |||
| + | <%@ page language="java" contentType="text/html; charset=BIG5" | ||
| + | pageEncoding="BIG5"%> | ||
| + | <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> | ||
| + | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | ||
| + | <html> | ||
| + | <head> | ||
| + | <meta http-equiv="Content-Type" content="text/html; charset=BIG5"> | ||
| + | <title>首頁</title> | ||
| + | </head> | ||
| + | <body> | ||
| + | <table align="center" cellpadding="10" width="100%"> | ||
| + | <tr> | ||
| + | <td align="right" width="50%"> | ||
| + | <%-- 使用Struts tag--%> | ||
| + | <html:link forward="newProduct">新增產品</html:link> | ||
| + | </td> | ||
| + | <td> | ||
| + | <html:link forward="searchProduct">查詢產品資料</html:link> | ||
| + | </td> | ||
| + | </tr> | ||
| + | </table> | ||
| + | </body> | ||
| + | </html> | ||
| + | |||
| + | 編輯 struts-config.xml 中的 <struts-config> 的 <global-forwards> 節點,修改內容如下: | ||
| + | |||
| + | <global-forwards> | ||
| + | <forward name="newProduct" path="/pages/newProduct.jsp" /> | ||
| + | <forward name="searchProduct" path="/pages/searchProduct.jsp" /> | ||
| + | </global-forwards> | ||
| + | |||
| + | 在 WebContent/ 目錄下建立 pages 目錄 | ||
| + | |||
| + | 在 WebContent/pages/ 目錄下建立 searchProduct.jsp 檔案後修改內容如下: | ||
| + | |||
| + | <%@ page language="java" contentType="text/html; charset=BIG5" | ||
| + | pageEncoding="BIG5"%> | ||
| + | <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> | ||
| + | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> | ||
| + | <html> | ||
| + | <head> | ||
| + | <meta http-equiv="Content-Type" content="text/html; charset=BIG5"> | ||
| + | <title>查詢產品資料</title> | ||
| + | </head> | ||
| + | <body> | ||
| + | <html:form action="searchProduct"> | ||
| + | <table width="100%"> | ||
| + | <tr> | ||
| + | <td align="center"> | ||
| + | 產品名稱: | ||
| + | <html:text property="productName" /> | ||
| + | </td> | ||
| + | </tr> | ||
| + | <tr> | ||
| + | <td align="center"> | ||
| + | <br> | ||
| + | <html:submit value=" 查詢 " /> | ||
| + | </td> | ||
| + | </tr> | ||
| + | </table> | ||
| + | </html:form> | ||
| + | </body> | ||
| + | </html> | ||
| + | |||