一.環境配置:html
1.Struts2包下載,官網下載Strust2的組件。java
官網包分類:徹底分發:struts-2.5.16-all.zip 示例應用:struts-2.5.16-apps.zipweb
僅限基本依賴:struts-2.5.16-min-lib.zip 全部依賴關係:struts-2.5.16-lib.zip apache
文檔:struts-2.5.16-docs.zip 資源:struts-2.5.16-src.zipapp
2.將下載好的組件解壓:框架
commons-fileupload-1.2.2.jar編碼
commons-io-2.2.jarurl
commons-lang3-3.2.jarspa
commons-logging-1.1.1.jar設計
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.5.jar
struts2-core-2.3.4.1.jar
xwork-core-2.3.4.1.jar
將這些組件複製到創建的web工程下的WEB-INF/lib目錄中,這些組件其實就是編譯好的類文件。
3.配置struts.xml文件:
代碼:
<?xml version="1.0" encoding="UTF-8"?>
<!--版本version=1.0 編碼格式UTF-8-->
<!--DOCTYPE 文檔類型-->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
</struts>
(struts標籤暫時爲空,後面用來配置Action, struts.xml是用來配置配置web應用,而web.xml用來配置struts )
4.配置 web.xml 文件:
web.xml文件經過配置加載FilterDispatcher,將會加載Struts2框架。 由於Struts2將核心控制器設計成Filter,而不是一個普通Servlet。
故爲了讓Web應用加載FilterDispatcher,只須要在web.xml文件中配置。不去理解的話,直接把上面的代碼複製後重命名爲web.xml便可
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>main.html</welcome-file> </welcome-file-list></web-app>