一、配置web.xml文件
在web.xml中配置一個struts的過濾器
2.5版本:
<filter>
<filter-name>struts-prepare</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareFilter</filter-class>
</filter>
<filter>
<filter-name>struts-execute</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-prepare</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts-execute</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.3版本:
<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>
二、配置struts.xml文件
1.文件聲明
2.5版本:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
2.3版本:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
2.正文:
相關知識點:
(1) struts.xml文件中所有的元素的定義都是以“<struts>
”開始,“</struts>
”結束。<struts>
主要的子元素有:
1) 設置常量:
<constant name="常量名" value="常量值"/>
2) package:
struts.xml文件中package元素的各大屬性講解
action等package子元素的配置簡介
3) 導入子文件
<include file="子文件名" />
<struts>
<!--<constant />用於配置常量,屬性name和value分別對應常量的名和值 -->
<!-- 設置匹配時Action默認的後綴,默認值爲“action”,舉例:
若未設置該常量的值,請求URL爲http://localhost:8080/ssh001/one/Index
則默認訪問名爲Index.action的action,
同http://localhost:8080/ssh001/one/Index.action;
Filter接受請求後,採用默認值去匹配時,將以action的name屬性值.action
的形式去匹配URL的Index.action;若設置常量值爲“do”,則請求URL爲
http://localhost:8080/ssh001/one/Index時將報404錯誤,找不到/ssh001/one/Index
因爲此時沒有默認的後綴,必須http://localhost:8080/ssh001/one/Index.do
此時Filter將以action的name屬性值.do的形式去匹配URL的Index.do -->
<constant name="struts.action.extension" value="action"/>
<!-- 使用"!"進行動態方法調用,必須設置該常量 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!--package的name屬性必需設置,若不設置namespace屬性,則該包使用默認
的命名空間--“ ”,查找Action時,系統會先在設置的命名空間裏進行匹配,
若匹配不成功,則再到默認空間裏進行匹配
如下例:進行請求的URL格式應爲 根目錄/one/Action名,若jsp是位於根目錄
下,則<result>應爲../error.jsp -->
<package name="one" extends="struts-default" namespace="/one">
<!-- 全局導航頁面映射定義,這裏定義的<result>被多個Action共用,
如果一個具體Action在<action>裏找不到對應的<result>,則會去
<global-result>裏尋找是否有對應的<result>
-->
<global-results>
<result name="error">error.jsp</result>
</global-results>
<!--全局異常頁面定義,根據其result屬性的值,在<global-result>中進行匹配 -->
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error" />
</global-exception-mappings>
<!--設置默認的action,屬性name的值爲要指定的Action的name值,
當用戶請求的URL在容器中找不到對應的Action時,系統將使用默認的
Action來處理用戶請求-->
<default-action-ref name="Index" />
<!--根據返回結果,決定將要跳轉到的頁面-->
<action name="Index" class="action.IndexAction">
<!--若不設置result的name屬性,則默認爲“success”,可不設置屬性type,
這時將使用默認值dispatcher(用於jsp整合的結果類型) -->
<result>success.jsp</result>
<result name="error">error.jsp</result>
</action>
<!--直接跳轉到指定頁面-->
<action name="Index">
<result>login.jsp</result>
</action>
<!--定義一個通用Action -->
<action name="*">
<result>/{1}.jsp</result>
</action>
<!-- 跳轉至其它action-->
<action name="redirectAction" class="action.RedirectAction">
<result>../WEB-INF/redirect.jsp</result>
</action>
<!--跳轉至redirectAction並給屬性tip賦值,調用其redirect()方法 -->
<action name="redirectTest">
<result type="redirect">redirectAction!redirect?tip=redirect</result>
</action>
</package>
<!-- 從其他地方導入一個xml的子文件-->
<include file="struts2-login.xml"/>
</struts>
注:
1.使用通配符“*”時,可能同時匹配到多個Action,這時將由第一個匹配成功的Action來處理用戶請求
2.通配符可用於<result.../>元素,但當<result name="*">/{1}.jsp</result>時
表達式{1}無法得到result的name屬性的值,舉例:
<action name="*" class="IndexAction" method="{1}">
<result name="*">/{1}.jsp</result>
</action>
兩個表達式{1}獲得的值都是action的name屬性值
3.同一個命名空間裏不能有同名的Action,所有在同一個包裏,不能定義多個name屬性值相同的Action,
即使定義了多個,前面的也會被後面的所覆蓋