struts.xml配置文件2_上

1.深入Struts2的配置文件
本部分主要介紹struts.xml的常用配置。
1.1.包配置:
Struts2框架中核心組件就是Action、攔截器等,Struts2框架使用包來管理Action和攔截器等。每個包就是多個Action、多個攔截器、多個攔截器引用的集合。
在struts.xml文件中package元素用於定義包配置,每個package元素定義了一個包配置。它的常用屬性有:
l name:必填屬性,用來指定包的名字。
l extends:可選屬性,用來指定該包繼承其他包。繼承其它包,可以繼承其它包中的Action定義、攔截器定義等。
l namespace:可選屬性,用來指定該包的命名空間。
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- struts2action必須放在一個指定的包空間下定義 -->
<package name="default" extends="struts-default">
<!-- 定義處理請求URLlogin.actionAction -->
<action name="login" class="org.qiujy.web.struts.action.LoginAction">
<!-- 定義處理結果字符串和資源之間的映射關係 -->
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
如上示例的配置,配置了一個名爲default的包,該包下定義了一個Action。
1.2.命名空間配置:
考慮到同一個Web應用中需要同名的Action,Struts2以命名空間的方式來管理Action,同一個命名空間不能有同名的Action。
Struts2通過爲包指定namespace屬性來爲包下面的所有Action指定共同的命名空間。
把上示例的配置改爲如下形式:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- struts2action必須放在一個指定的包空間下定義 -->
<package name="qiujy" extends="struts-default">
<!-- 定義處理請求URLlogin.actionAction -->
<action name="login" class="org.qiujy.web.struts2.action.LoginAction">
<!-- 定義處理結果字符串和資源之間的映射關係 -->
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
<package name="my" extends="struts-default" namespace="/manage">
<!-- 定義處理請求URLlogin.actionAction -->
<action name="backLogin" class="org.qiujy.web.struts2.action.LoginAction">
<!-- 定義處理結果字符串和資源之間的映射關係 -->
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package></struts>
如上配置了兩個包:default和my,配置my包時指定了該包的命名空間爲/manage。
對於包default:沒有指定namespace屬性。如果某個包沒有指定namespace屬性,即該包使用默認的命名空間,默認的命名空間總是""。
對於包my:指定了命名空間/manage,則該包下所有的Action處理的URL應該是“命名空間/Action名”。如上名爲backLogin的Action,它處理的URL爲:
http://localhost:8080/userlogin_struts2/manage/backLogin.action
Struts2的命名空間的作用等同於struts1裏模塊的作用。
1.3.包含配置:
在Struts2中可以將一個配置文件分解成多個配置文件,那麼我們必須在struts.xml中包含其他配置文件。
<struts>
<include file="struts-default.xml"/>
<include file="struts-user.xml"/>
<include file="struts-book.xml"/>
<include file="struts-shoppingCart.xml"/>
   ......
</struts>
1.4.攔截器配置:
見後面章節介紹。
1.5.常量配置:
Struts2框架有兩個核心配置文件,其中struts.xml文件主要負責管理應用中的Action映射, 及Action處理結果和物理資源之間的映射關係。除此之外,Struts2框架還包含了一個struts.properties文件,該文件主義了Struts2框架的大量常量屬性。但通常推薦也是在struts.xml文件中來配置這些常量屬性。
如:後面會講到Struts2的國際化,它的資源文件位置就用常量屬性來指定:
<struts>
   ......
<constant name="struts.custom.i18n.resources" value="messages"/>
</struts>


表示指定了資源文件的放置在classes目錄下,基本名是messages,則在classes目錄下您就應該放置類似messages_zh_CN.properties,message_en.properties名的文件。
2.Struts2的Action
2.1.實現Action類:
Struts2中Action是核心內容,它包含了對用戶請求的處理邏輯,我們也稱Action爲業務控制器。
Struts2中的Action採用了低侵入式的設計,Struts2不要求Action類繼承任何的Struts2的基類或實現Struts2接口。(但是,我們爲了方便實現Action,大多數情況下都會繼承com.opensymphony.xwork2.ActionSupport類,並重寫此類裏的public String execute() throws Exception方法。因爲此類中實現了很多的實用接口,提供了很多默認方法,這些默認方法包括獲取國際化信息的方法、數據校驗的方法、默認的處理用戶請求的方法等,這樣可以大大的簡化Action的開發。)
Struts2中通常直接使用Action來封裝HTTP請求參數,因此,Action類裏還應該包含與請求參數對應的屬性,並且爲屬性提供對應的getter和setter方法。(當然,Action類中還可以封裝處理結果,把處理結果信息當作一屬性,提供對應的getter和setter方法)
修改第一部分的用戶登錄示例:把Action改成如下:
package org.qiujy.web.struts2.action;
import com.opensymphony.xwork2.ActionSupport;
/**
*@authorqiujy
*@version1.0
*/
publicclass LoginAction extends ActionSupport{
private String userName;
private String password;
private String msg; //結果信息屬性
/**
*@returnthemsg
*/
public String getMsg() {
returnmsg;
   }
/**
*@parammsgthemsgtoset
*/
publicvoid setMsg(String msg) {
this.msg = msg;
   }
/**
*@returntheuserName
*/
public String getUserName() {
returnuserName;
   }
/**
*@paramuserNametheuserNametoset
*/
publicvoid setUserName(String userName) {
this.userName = userName;
   }
/**
*@returnthepassword
*/
public String getPassword() {
returnpassword;
   }
/**
*@parampasswordthepasswordtoset
*/
publicvoid setPassword(String password) {
this.password = password;
   }
/**
*處理用戶請求的excute()方法
*@return結果導航字符串
*@throwsException
*/
public String execute() throws Exception{
if("test".equals(this.userName) &&
"test".equals(this.password)){
msg = "登錄成功,歡迎" + this.userName;
returnthis.SUCCESS;
      }else{
msg = "登錄失敗,用戶名或密碼錯";
returnthis.ERROR;
      }
   }
}
往success.jsp和error.jsp頁面中添加 ${msg} EL表達式來顯示結果信息。則最終效果跟以前一樣。
2.2.Action訪問Servlet API
Struts2中的Action並沒有和任何Servlet API耦合,這樣框架更具靈活性,更易測試。
但是,對於web應用的控制器而言,不訪問Servlet API幾乎是不可能的,例如跟蹤HTTP Session狀態等。Struts2框架提供了一種更輕鬆的方式來訪問Servlet API。Struts2中提供了一個ActionContext類(當前Action的上下文對象),通過這個類可以訪問Servlet API。下面是該類中提供的幾個常用方法:
l public static ActionContext getContext() :獲得當前Action的ActionContext實例。
l public Object get(Object key) :此方法類似於調用HttpServletRequest的getAttribute(String name)方法。
l public void put(Object key, Object value) :此方法類似於調用HttpServletRequest 的setAttribute(String name, Object o)。
l public Map getParameters() :獲取所有的請求參數。類似於調用HttpServletRequest對象的getParameterMap() 方法。
l public Map getSession() :返回一個Map對象,該Map對象模擬了HttpSession實例。
l public void setSession(Map session) : 直接傳入一個Map實例,將該Map實例裏的key-value對轉換成session的屬性名-屬性值對。
l public Map getApplication() :返回一個Map對象,該對象模擬了該應用的ServletContext實例。
l public void setApplication(Map application) :直接傳入一個Map實例,將該Map實例裏的key-value對轉換成application的屬性名-屬性值對。
修改以上用戶登錄驗證示例的Action類中的execute方法:
public String execute() throws Exception{
if("test".equals(this.userName) && "test".equals(this.password)){
msg = "登錄成功,歡迎" + this.userName;
//獲取ActionContext實例,通過它來訪問Servlet API
           ActionContext context = ActionContext.getContext();
//session中是否已經存放了用戶名,如果存放了:說明已經登錄了;
//否則說明是第一次登錄成功
if(null != context.getSession().get("uName")){
msg = this.userName + ":你已經登錄過了!!!";
           }else{
               context.getSession().put("uName", this.userName);
           }
returnthis.SUCCESS;
       }else{
msg = "登錄失敗,用戶名或密碼錯";
returnthis.ERROR;
       }
   }
      Struts2中通過ActionContext來訪問Servlet API,讓Action徹底從Servlet API 中分離出來,最大的好處就是可以脫離Web容器測試Action。
      另外,Struts2中還提供了一個ServletActionContext類,Action只要繼承自該類,就可以直接訪問Servlet API。具體方法參看struts2的API文檔。
3.一個Action內包含多個請求處理方法的處理
Struts1提供了DispatchAction,從而允許一個Action內包含多個請求處理方法。Struts2也提供了類似的功能。處理方式主要有以下三種方式:
3.1.動態方法調用:
DMI:Dynamic Method Invocation 動態方法調用。
動態方法調用是指:表單元素的action不直接等於某個Action的名字,而是以如下形式來指定對應的動作名:
<form method="post" action="userOpt!login.action">
則用戶的請求將提交到名爲”userOpt”的Action實例,Action實例將調用名爲”login”方法來處理請求。同時login方法的簽名也是跟execute()一樣,即爲public String login() throws Exception。
注意:要使用動態方法調用,必須設置Struts2允許動態方法調用,通過設置struts.enable.DynamicMethodInvocation常量來完成,該常量屬性的默認值是true。
3.1.1.示例:
修改用戶登錄驗證示例,多增加一個註冊用戶功能。
1.修改Action類:
package org.qiujy.web.struts2.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
*@authorqiujy
*@version1.0
*/
publicclass LoginAction extends ActionSupport{
private String userName;
private String password;
private String msg; //結果信息屬性
/**
*@returnthemsg
*/
public String getMsg() {
returnmsg;
   }
/**
*@parammsgthemsgtoset
*/
publicvoid setMsg(String msg) {
this.msg = msg;
   }
/**
*@returntheuserName
*/
public String getUserName() {
returnuserName;
   }
/**
*@paramuserNametheuserNametoset
*/
publicvoid setUserName(String userName) {
this.userName = userName;
   }
/**
*@returnthepassword
*/
public String getPassword() {
returnpassword;
   }
/**
*@parampasswordthepasswordtoset
*/
publicvoid setPassword(String password) {
this.password = password;
   }
/**
*處理用戶請求的login()方法
*@return結果導航字符串
*@throwsException
*/
public String login() throws Exception{
if("test".equals(this.userName) && "test".equals(this.password)){
msg = "登錄成功,歡迎" + this.userName;
//獲取ActionContext實例,通過它來訪問Servlet API
           ActionContext context = ActionContext.getContext();
//session中是否已經存放了用戶名,如果存放了:說明已經登錄了;
//否則說明是第一次登錄成功
if(null != context.getSession().get("uName")){
msg = this.userName + ":你已經登錄過了!!!";
           }else{
               context.getSession().put("uName", this.userName);
           }
returnthis.SUCCESS;
       }else{
msg = "登錄失敗,用戶名或密碼錯";
returnthis.ERROR;
       }
   }
public String regist() throws Exception{
//將用戶名,密碼添加到數據庫中
//...
msg = "註冊成功。";
returnthis.SUCCESS;
   }
}
2.struts.xml文件:沒有什麼變化,跟以前一樣配置
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="my" extends="struts-default" namespace="/manage">
<!-- 定義處理請求URLlogin.actionAction -->
<action name="userOpt" class="org.qiujy.web.struts2.action.LoginAction">
<!-- 定義處理結果字符串和資源之間的映射關係 -->
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
3.頁面:
index.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<title>用戶登錄頁面</title>
</head>
<body>
<h2>用戶入口</h2>
<hr>
<form action="manage/userOpt!login.action" method="post">
<table border="1">
<tr>
<td>用戶名:</td>
<td><input type="text" name="userName"/></td>
</tr>
<tr>
<td>密碼:</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value=" 確定 "/>
</td>
</tr>
</table>
</form>
</body>
</html>
regist.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<title>用戶註冊頁面</title>
</head>
<body>
<h2>用戶註冊</h2>
<hr>
<form action="manage/userOpt!regist.action" method="post">
<table border="1">
<tr>
<td>用戶名:</td>
<td><input type="text" name="userName"/></td>
</tr>
<tr>
<td>密碼:</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value=" 註冊 "/>
</td>
</tr>
</table>
</form>
</body>
</html>
4.運行結果:
3.2.Action配置method屬性:
將Action類中的每一個處理方法都定義成一個邏輯Action方法。
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="my" extends="struts-default" namespace="/manage">
<action name="userLogin" class="org.qiujy.web.struts2.action.LoginAction" method="login">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
<action name="userRegist" class="org.qiujy.web.struts2.action.LoginAction"method="regist">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
如上,把LoginAction中的login和regist方法都配置成邏輯Action。要調用login方法,則相應的把index.jsp中表單元素的action設置爲"manage/userLogin.action";要調用regist方法,把regist.jsp中表單元素的action設置爲"manage/userRegist.action"。
3.3.使用通配符映射(wildcard mappings)方式:
在struts.xml文件中配置<action…>元素時,它的name、class、method屬性都可支持通配符,這種通配符的方式是另一種形式的動態方法調用。
當我們使用通配符定義Action的name屬性時,相當於用一個元素action定義了多個邏輯Action:
<action name="user_*"
class="org.qiujy.web.struts2.action.UserAction" method="{1}">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
如上,<action name=”user_*”>定義一系列請求URL是user_*.action模式的邏輯Action。同時method屬性值爲一個表達式{1},表示它的值是name屬性值中第一個*的值。例如:用戶請求URL爲user_login.action時,將調用到UserAction類的login方法;用戶請求URL爲user_regist.action時,將調用到UserAction類的regist方法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章