目錄
簡介
- Struts2框架應用於JavaEE三層結構中Web層的框架;
- Struts2框架在Structs1和WebWork基礎之上發展的全新的框架;
- Web層常見框架:(1)struts2;(2)Spring MVC。
Struts2中的Action
Action類的配置
配置action類訪問路徑的步驟如下:
(1)創建struts2核心配置文件
- 核心配置文件的名稱和位置是固定的,位置必須在src下面,名稱爲struts.xml
(2)爲struts.xml引入dtd約束
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
(3)在struts.xml中進行action配置
<struts>
<package name="hellodemo" extends="struts-default" namespace="/">
<!--name: 訪問名稱-->
<action name="hello" class="com.wm103.action.HelloAction">
<!--配置方法返回值跳轉到的頁面-->
<result name="ok">/hello.jsp</result>
</action>
</package>
</struts>
(4)在web.xml中配置struts2的過濾器
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
(5)訪問地址:http://localhost:8080/Web應用的名稱/hello.action
或者 http://localhost:8080/Web應用的名稱/hello
web.xml中Struts2的過濾器
在web.xml中配置的Struts2過濾器在服務器啓動時候創建,創建過濾器時執行init方法。在init方法中主要加載配置文件,包含自己創建的配置文件和Struts2自帶的配置文件。
注:過濾器在我們啓動服務器時創建,而Servlet默認在第一次訪問時創建。
Struts2的核心配置文件
Struts2的核心配置文件固定放置在Web應用src目錄下,名稱爲struts.xml。在配置文件中主要有3個標籤以及其屬性,分別爲 package、action、result標籤。
標籤package
類似於代碼中的包,用於區分不同的action。要配置action,必須首先寫package標籤,在package標籤內才能配置action標籤。
標籤package的屬性
(1)name屬性
name屬性值跟功能本身並沒有什麼關係。同時,在一個配置文件中,我們可以寫多個package標籤,但是package標籤的屬性name值不能相同。
(2)extends屬性
extends屬性的屬性值是固定的,如:extends=”struts-default”。
配置了extends屬性後,在package裏面配置的類就具有了action的功能。
(3)namespace屬性
namespace屬性的默認值爲“/”。namespace屬性值同action標籤中的name屬性值構成訪問路徑。如:
<package name="hellodemo" extends="struts-default" namespace="/web">
<!--name: 訪問名稱-->
<action name="hello" class="com.wm103.action.HelloAction">
<!--配置方法返回值跳轉到的頁面-->
<result name="ok">/hello.jsp</result>
</action>
</package>
則訪問:http://localhost:8080/Web應用名稱/web/hello
。
標籤action
action標籤配置action的訪問路徑。
標籤action的屬性
(1)name屬性
namespace屬性的默認值爲“/”。namespace屬性值同action標籤中的name屬性值構成訪問路徑。
在package標籤裏面可以寫多個action標籤,但是action的name屬性值不能相同。
(2)class屬性
配置action的全路徑。
(3)method屬性
在action裏面默認執行execute方法,可以通過method屬性配置其他方法執行。
標籤result
根據action的方法(默認爲execute方法)的返回值,配置到不同的路徑裏面。
標籤result的屬性
(1)name屬性
和方法返回值一樣,如:
<!--配置方法返回值跳轉到的頁面-->
<result name="ok">/hello.jsp</result>
(2)type屬性
配置如何到路徑中去(轉發或重定向),type屬性默認值是轉發的操作。詳情請看:result標籤的type屬性。
分模塊開發
單獨寫配置文件,並把配置文件引入到核心配置文件struts.xml,如:
<!-- 引入hello.xml文件 -->
<include file="com/wm103/action/hello.xml"></include>
如果直接在src目錄下,則
<include file="hello.xml"></include>
Struts2常量配置
Struts2默認的常量存放位置在項目struts2 jar包中的org.apache.struts2
下的default.properties
中。
修改Struts2默認常量值
(1)常用的方式
- 在struts.xml中進行配置,如:
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
(2)在src下面創建struts.properties
,進行修改。
(3)在web.xml中通過<init-param>
元素配置常量。
常用常量
struts.i18n.encoding=UTF-8
(1)表單提交數據到action中,在action中可以獲取表單提交的數據。
(2)表單提交的數據中有中文,亂碼問題的解決:
- post提交,直接設置編碼;
- get提交,做編碼轉換。
參考:http://blog.csdn.net/qq_15096707/article/details/70953444#request接收中文數據亂碼問題
(3)如果在action獲取表單通過post方式提交的中文數據,那麼中文亂碼問題就已經被解決,不需要我們自己處理。
Action的編寫方式
Action的編寫有3中方式:
1. 創建簡單類,不實現任何接口,也不繼承任何類;
public class HelloAction {
public String execute() {
return "ok";
}
}
2. 創建一個實現Action接口的類;
import com.opensymphony.xwork2.Action;
/**
* Created by DreamBoy on 2017/5/16.
* 實現接口
*/
public class UserAction implements Action {
@Override
public String execute() throws Exception {
//return "success";
return SUCCESS;
}
}
3. 創建一個繼承ActionSupport類的類。(最常用的方式)
import com.opensymphony.xwork2.ActionSupport;
/**
* Created by DreamBoy on 2017/5/16.
* 繼承類
*/
public class PersonAction extends ActionSupport {
@Override
public String execute() throws Exception {
return SUCCESS;
}
}
修改訪問Action時執行的方法
有3種實現的方式:
1. 修改struts.xml配置文件中action標籤的method屬性,該屬性的值設置爲訪問Action時執行的方法名。
2. 使用通配符實現(重點)
設置action標籤name屬性的值包含符號“*
”。*
表示匹配任意內容。如:
struts.xml中配置:
<package name="demo" extends="struts-default" namespace="/">
<action name="book_*" class="com.wm103.action.BookActon" method="{1}"></action>
</package>
這樣配置後,表示訪問URL路徑 /book_add 時,將會調用BookAction中的add方法執行;訪問URL路徑 /book_update 時,則會調用BookAction中update方法執行,以此類推。因爲 *
號表示匹配任意字符。其中 {1}
則表示第一個匹配到*
號的內容,如 book_add
則匹配 book_*
,那麼{1}
的值爲add。
注:book_
並不匹配 book_*
。
3. 動態訪問實現(一般不使用)
注:1. 訪問Action時執行的方法(默認爲execute方法),有返回值,那麼返回值類型必須爲String類型。2. 該方法也可以沒有返回值,在沒有返回值的時候,在struts.xml中result標籤不需要配置。並將方法的返回值設置爲void或者讓返回值,返回“none”字符串(一般我們返回“none”)。
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport {
public String execute() {
return NONE;
}
}