Struts官網: http://struts.apache.org/
Struts2框架預先實現了一些功能
1:請求數據自動封裝
2:文件上傳的功能
3:對國際化功能的簡化
4:數據校驗的功能
第一:首先需要說明的是Struts就是基於MVC模式的框架!(struts其實也是servlet封裝,提高開發效率!)
第二:Struts開發步驟:
1. web項目,引入struts - jar包
2. web.xml中,引入struts的核心功能,配置過濾器
3. 開發action
4. 配置action --->src/struts.xml
下面詳細介紹一下第一個struts2的開發流程:
1. 創建動態web項目,引入struts - jar包(這里引入8個jar包,如下所示。很容易就可以得到就不分享了哦)

commons-fileupload-1.3.1.jar 【文件上傳相關包】
commons-io-2.2.jar
commons-lang3-3.1.jar 【struts對java.lang包的擴展】
freemarker-2.3.19.jar 【struts對標簽模板庫jar文件】
javassist-3.11.0.GA.jar 【struts對字節碼的處理相關jar】
ognl-3.0.6.jar 【Ognl表達式功能支持表】
struts2-core-2.3.16.3.jar 【struts2核心功能包】
xwork-core-2.3.16.3.jar 【xwork核心包】
2. 在web.xml中,引入struts的核心功能,配置過濾器(已經加了注釋,不作多敘述)
Tomcat啟動---》加載自身web.xml---》加載所有項目的web.xml
通過在項目的web.xml中引入過濾器
struts2的核心功能的初始化,是通過過濾器完成的。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 3 <display-name>struts2_20170219</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 13 14 <!-- 其他過濾器 --> 15 16 <!-- 引入struts2的核心過濾器 --> 17 <filter> 18 <!-- 過濾器的名稱 --> 19 <filter-name>struts2</filter-name> 20 <!-- 過濾器類 --> 21 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 22 </filter> 23 <filter-mapping> 24 <!-- 過濾器名稱 --> 25 <filter-name>struts2</filter-name> 26 <!-- 過濾器映射 --> 27 <url-pattern>/*</url-pattern> 28 </filter-mapping> 29 </web-app>
此處插一句,配置過濾器的時候,filter-class里面的內容我是這樣獲取的,因為寫法是死的,如圖,僅供參考。
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

雙擊點開之后,復制此句即可。

3. 開發action,此處習慣繼承了ActionSupport.
package com.bie;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author BieHongLi
* @version 創建時間:2017年2月19日 下午3:08:53
* 開發action,處理請求
*/
public class HelloAction extends ActionSupport{
private static final long serialVersionUID = 1L;
/**
* 重寫execute,處理請求的方法
*/
@Override
public String execute() throws Exception {
System.out.println("訪問到了action,正在 處理請求");
System.out.println("hello world!!! struts2");
return SUCCESS;
}
}
4. 配置action --->src/struts.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="helloWorld" extends="struts-default">
<!-- 定義action -->
<action name="hello" class="com.bie.HelloAction" method="execute">
<!-- 顯示成功的jsp頁面 -->
<result name="success">success.jsp</result>
</action>
</package>
</struts>
配置struts.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>
</struts>
最后了,當然直接運行就行了,結果如下所示:

這個是在瀏覽器運行的結果:

