1. 下載Eclipse
登入Eclipse官網下載http://www.eclipse.org/downloads/
我選擇的是Eclipse Classic 3.6.2, 32位
2. 添加Web and Java EE開發工具
Help -> Install new software,在"Work with:"下拉框中選中"Helios - http://download.eclipse.org/releases/helios"(如果沒有,可以自行添加),然後在"Web, XML, and Java EE Development"中選擇
- Eclipse Java EE Developer Tools
- Eclipse Web Developer Tools
- JST Server Adapters (建立Apache Tomcat Server需要)
3. 下載Tomcat
登入Apache Tomcat官網下載http://tomcat.apache.org/
我選擇的是Tomcat 7.0.14
4. 添加Tomcat外掛程式
http://www.eclipsetotale.com/tomcatPlugin.html
這個好像沒有官網,也沒有update site
下載後解壓,複製到plugin目錄下,然後重啟eclips即可
5. 建立Web工程
File -> New -> Project -> Dynamic Web Project
6. 添加Struts 2類庫
登入Struts官網下載http://struts.apache.org/downloads.html
我選擇的是struts-2.2.3-lib.zip
下載後解壓,把Struts 2必須的核心類庫複製到到Web工程的WebContent/WEB-INF/lib下
- commons-fileupload-1.2.2.jar
- commons-io-2.0.1.jar
- commons-lang-2.5.jar
- commons-logging-1.1.1.jar
- freemarker-2.3.16.jar
- javassist-3.11.0.GA.jar
- ognl-3.0.1.jar
- struts-core-1.3.10.jar
- xwork-core-2.2.3.jar
7. 建立web.xml
不知道為什麼,工程建立的時候沒有web.xml,不過沒有關係,我們可以自己建
檔案位置:/WebContent/WEB-INF/web.xml
內容 Xml代碼
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app id="WebApp_ID" version="2.4"
- xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
-
- <display-name>Project Name</display-name>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- </welcome-file-list>
-
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
- </filter>
-
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
-
- </web-app>
<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_ID" version="2.4"xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><display-name>Project Name</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>
8. 建立struts.xml
檔案位置:/WebContent/WEB-INF/classes/struts.xml
內容 Xml代碼
- <?xml version="1.0" encoding="UTF-8" ?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
-
- <struts>
-
- <package name="default" extends="struts-default">
- <action name="action_name" class="package_name.class_name">
- <result name="Success">./WEB-INF/jsp/success.jsp</result>
- <result name="Failure">./WEB-INF/jsp/failure.jsp</result>
- </action>
- </package>
-
- </struts>
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"><struts> <package name="default" extends="struts-default"> <action name="action_name" class="package_name.class_name"> <result name="Success">./WEB-INF/jsp/success.jsp</result> <result name="Failure">./WEB-INF/jsp/failure.jsp</result> </action> </package></struts>
9. 建立Server
File -> New -> Server
"Server Type"我選擇了"Tomcat v7.0 Server",然後把建立的Web工程移動到"Configured"中就可以了
這樣基本就搭建好Struts 2的結構了