Struts2學習第三天——攔截器與文件傳輸

文檔版本 開發工具 測試平臺 工程名字 日期 作者 備註
V1.0 2016.06.15 lutianfei none


  • 內容摘要:
    1.國際化(瞭解)
    2.攔截器(Interceptor)—重點
    3.struts2文件上傳與下載(次重點)
    4.ognl與valuestack

國際化

  • 國際化原理,什麼是國際化 ?

    • 同一款軟件 可以爲不同用戶,提供不同語言界面 —- 國際化軟件
    • 需要一個語言資源包(很多properties文件,每個properties文件 針對一個國家或者語言 ,通過java程序根據來訪者國家語言,自動讀取不同properties文件 )
  • Java原生國家化操作

  • 資源包編寫
    • properties文件命名 : 基本名稱_語言(小寫)_國家(大寫).properties
    • 例如 :
      • messages_zh_CN.properties 中國中文
      • messages_en_US.properties 美國英文
  • ResourceBundle 根據不同Locale(地域信息),讀取不同國家 properties文件
    ResourceBundle bundle = ResourceBundle.getBundle("messages", Locale.US);


struts2中國際化

  • struts2中對國際化進行了封裝,我們只需要根據其提供的API進行訪問就可以。
問題1:在struts2中國際化時properties文件的定義
  • 1.全局 : 需要通過一個常量來聲明
    • 默認全局路徑:
    • 第166行:struts.custom.i18n.resources=testmessages,testmessages2
    • 對於properties配置文件可以放置在任意位置
    • struts.xml配置如下:
      • 當message.properties在src下:<constant name="struts.custom.i18n.resources" value="message">
      • 當message.properties在cn.itcast.i18n.resource包下: <constant name="struts.custom.i18n.resources" value="cn.itcast.i18n.resource.message">


  • 2.局部
    • 1.針對於action類
      • 位置:與action類在同一個包下.
      • 名稱: ActionClassName.properties
      • 這個配置文件只對當前action有效。
    • 2.針對於package下所有action
      • 位置:在指定的包下
      • 名稱:package.properties
    • 3.jsp頁面臨時使用某一個properties文件.
      • 在某個jsp頁面中加入 : <s:i18n name="cn.itcast.action.package"></s:i18n>

問題2:Struts2中國際化操作在哪些位置使用

  • 1.action類中使用
  • 2.配置文件中使用<validation.xml>
  • 3.在jsp頁面上使用


問題3:Struts2中操作國際化

  • 1.在action類中使用

    • 前提:action類要繼承ActionSupport類。
    • getText(String name)就可以獲取配置文件中對應名稱的值。
  • eg :

// I18nDemo1Action.properties 中 msg=hello world

    public String execute() throws Exception {

        // 得到properties文件中信息.

        System.out.println(this.getText("msg"));
        return NONE;
    }

    //結果就是 hello world


  • 2.在validation.xml文件中使用
    • <message key="名稱"/>
    • eg : I18nDemo2Action-validation.xml & xxx.properites
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
        "-//Apache Struts//XWork Validator 1.0.3//EN"
        "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">

<validators>
    <field name="name">
        <field-validator type="requiredstring">
            <message key="nameerror"></message>
        </field-validator>

    </field>

</validators>


//properties

//package_en_US.properties
nameerror=name required


//package_zh_CN.properties
nameerror=\u540D\u5B57\u5FC5\u987B\u5730

//package.properties
nameerror=name required
  • 3.在jsp頁面上使用

    • <s:text name="名稱"> :這裏名稱爲對應的Action名稱
    • 如果沒有使用<s:i18n name="">來指定,會從全局配置文件中獲取。
    • 如果要從某一個配置文件中獲取,通過name屬性來指定 包名.配置文件名稱
  • eg : i18n.jsp


      <s:i18n name="cn.itcast.action.package">
          <s:text name="nameerror"/>
      </s:i18n>


    <s:text name="name" /> //全局包:cn.itcast.i18n.resource
    //由struts.xml中常量配置決定:
    //<constant name="struts.custom.i18n.resources" value="cn.itcast.i18n.resource.message"></constant>


* Struts2中國際化配置文件中使用動態文本*

  • 1.action中動態文本使用

    • I18nDemo1Action.properties 中 : msg=hello world {0}
    • I18nDemo1Action 輸出: this.getText(“msg”,new String[]{“tom”})
  • eg :

    public String execute() throws Exception {

        // 得到properties文件中信息.

        //System.out.println(this.getText("msg"));

        //動態文本
        System.out.println(this.getText("msg", new String[]{"tom"}));

        return NONE;
    }

    //結果就是 hello world tom


  • 2.jsp頁面上怎樣使用
    • 在i18n.jsp 中加入 :
        <s:i18n name="cn.itcast.action.I18nDemo1Action">
        <s:text name="msg">
        <s:param>張三</s:param>
        </s:text>
        </s:i18n>
* 結果就是 hello world  張三.


攔截器(interceptor)

  • struts2攔截器使用的是AOP思想
  • AOP的底層實現就是動態代理

  • 攔截器採用 責任鏈 模式

    • 在責任鏈模式裏,很多對象由每一個對象對其下家的引用而連接起來形成一條鏈。
    • 責任鏈每一個節點,都可以繼續調用下一個節點,也可以阻止流程繼續執行
  • struts2中在struts-default.xml文件中聲明瞭所有的攔截器。

  • 而struts2框架默認使用的是defaultStack這個攔截器棧。
  • 在這個攔截器棧中使用了18個攔截器
  • 簡單說,struts2框架在默認情況下,加載了18個攔截器

Struts2中使用攔截器

  • 使用攔截器可以做什麼?
    • 可以通過使用攔截器進行控制action的訪問。例如,權限操作。
攔截器使用方法
  • 1.創建一個Interceptor 可以自定義一個類實現com.opensymphony.xwork2.interceptor.Interceptor在這個接口中有三個方法
    • init
    • destory
    • intercept 真正攔截的方法。
    • 在intercept方法中如果要向下繼續執行,通過其參數ActionInvocation調用它的invoke()方法就可以。
public class _MyInterceptor implements Interceptor {
    public void destroy() {
    }

    public void init() {
        System.out.println("my interceptor init");
    }

    public String doIntercept(ActionInvocation ai) throws Exception {

        System.out.println("my interceptor 攔截。。。。。");
        //return ai.invoke(); // 放行
        return Action.LOGIN;  //"login"
    }

}


  • 2.聲明一個Interceptor 在struts-default.xml文件中
    • eg:
    <interceptor name="my" class="cn.itcast.intercept.MyInterceptor">
    </interceptor>


  • 3.在action中指定使用哪些攔截器
<interceptors>
    <interceptor name="my" class="cn.itcast.intercept.MyInterceptor">
    </interceptor>
</interceptors>
  • 注意 : 只要顯示聲明使用了一個攔截器。那麼默認的攔截器就不在加載。
    • 如果需要繼續使用默認攔截器只需要在struts.xml中設計自己的攔截器棧。
<interceptors>

    <interceptor name="my" class="cn.itcast.intercept.MyInterceptor">
    </interceptor>

    <interceptor name="bookInterceptor" class="cn.itcast.intercept.BookInterceptor">
        <param name="includeMethods">add,update,delete</param>
    </interceptor>

    <interceptor-stack name="myStack">
        <interceptor-ref name="bookInterceptor"></interceptor-ref>
        <interceptor-ref name="defaultStack" />
    </interceptor-stack>
</interceptors>


分析攔截器原理

  • 源代碼執行流程:

  • 1.在StrutsPrepareAndExecuteFilter中查找

    • doFilter方法內有一句話 execute.executeAction (request, response, mapping) 執行Action操作.
  • 2.在executeAction執行過程中會訪問Dispatcher類中的serviceAction,在這個方法中會創建一個

    • ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(namespace, name, method, extraContext, true, false); 這就是我們的Action的代理對象
  • 3.查看ActionInvocation,查看其實現類 DefaultActionInvocation.

    • 在其invoke方法中
    if (interceptors.hasNext()) {//判斷是否有下一個攔截器.
    final InterceptorMapping interceptor = interceptors.next(); //得到一個攔截器
    String interceptorMsg = "interceptor: " + interceptor.getName();
    UtilTimerStack.push(interceptorMsg);
    try {
    resultCode = interceptor.getInterceptor().intercept(DefaultActionInvocation.this); 
    //調用得到的攔截器的攔截方法.將本類對象傳遞到了攔截器中。
    }
    finally {
    UtilTimerStack.pop(interceptorMsg);
    }
    } 
  • 通過源代碼分析,發現在DefaultActionInvocation中就是通過遞歸完成所有的攔截調用操作.


關於interceptor與Filter區別
  • 1、攔截器是基於java反射機制的,而過濾器是基於函數回調的。
  • 2、過濾器依賴於servlet容器,而攔截器不依賴於servlet容器。
  • 3、攔截器只能對Action請求起作用,而過濾器則可以對幾乎所有請求起作用。
  • 4、攔截器可以訪問Action上下文、值棧裏的對象,而過濾器不能。
  • 5、在Action的生命週期中,攔截器可以多次調用,而過濾器只能在容器初始化時被調用一次。
  • interceptor 可以在其他環境中使用,而Filter只能在Web環境中使用。


攔截器案例

  • 1.login.jsp –> LoginAction –> book.jsp
  • 登錄成功,將用戶存儲到session。

  • login.jsp

  <body>
    <s:fielderror/>
    <s:actionerror/>

    <form action="${pageContext.request.contextPath }/login" method="post">
        username:<input type="text" name="username"><br>
        password:<input type="password" name="password"><br>

        <input type="submit" value="登錄">
    </form>
  </body>


  • LoginAction.java
public class LoginAction extends ActionSupport implements ModelDriven<User>{

    private User user = new User();

    public User getModel(){
        return user;
    }
     @Override
    public String execute() throws Exception {

         System.out.println("name: "+user.getUsername());
         System.out.println("password: "+user.getPassword());

        if("miaolu".equals(user.getUsername()) && "123".equals(user.getPassword())){
            ServletActionContext.getRequest().getSession().setAttribute("user", user);
            return SUCCESS;
        }
        else{
            this.addActionError("用戶名或密碼錯誤");
            return INPUT;
        }
    }
}


  • book.jsp
  <body>
      <a href="${pageContext.request.contextPath}/book_add">book add</a><br>
      <a href="${pageContext.request.contextPath}/book_update">book update</a><br>
      <a href="${pageContext.request.contextPath}/book_delete">book delete</a><br>
      <a href="${pageContext.request.contextPath}/book_search">book search</a><br>
  </body>


  • 2.在book.jsp中提供crud鏈接。
  • 每一個連接訪問一個BookAction中一個方法。
  • 要求:對於BookAction中的add,update,delete方法要求用戶必須登錄後纔可以訪問。search無要求。

  • BookAction.java

public class BookAction extends ActionSupport {


    public String add() throws Exception{
        System.out.println("book action add");

        return null;
    }

    public String update() throws Exception{
        System.out.println("book action update");

        return null;
    }

    public String delete() throws Exception {

        System.out.println("book action delete");
        return null;
    }

    public String search() throws Exception {

        System.out.println("book action search");
        return null;
    }

}


  • 怎樣解決只控制action中某些方法的攔截?

    • 1.創建類不在實現Interceptor接口,而是繼承其下的一個類.MethodFilterInterceptor
    • 不用在重寫intercept方法,而是重寫 doIntercept方法。
  • BookInterceptor.java

public class BookInterceptor extends MethodFilterInterceptor {

    @Override
    protected String doIntercept(ActionInvocation invocation) throws Exception {

        User user = (User) ServletActionContext.getRequest().getSession().getAttribute("user");


        if(user == null){
            BookAction action = (BookAction) invocation.getAction();

            action.addActionError("權限不足,請先登錄");

            return Action.LOGIN;
        }

        return invocation.invoke();
    }
}


  • 2.在struts.xml文件中聲明
<struts>
    <package name="default" namespace="/" extends="struts-default">

        <interceptors>

            <interceptor name="bookInterceptor" class="test.intercept.BookInterceptor">
                <param name="includeMethods">add,update,delete</param>
                <param name="excludeMethods">search</param>
            </interceptor>

            <interceptor-stack name="myStack">
                <interceptor-ref name="bookInterceptor"></interceptor-ref>
                <interceptor-ref name="defaultStack"/>
            </interceptor-stack>

        </interceptors>

        <global-results>
            <result name = "login">/login.jsp</result>
        </global-results>

        <action name ="login" class="test.action.LoginAction">
            <result name="input">/login.jsp</result>
            <result>/book.jsp</result>
        </action>


        <action name="book_*" class="test.action.BookAction" method="{1}">
            <interceptor-ref name="myStack"/>
        </action>
    </package>




Struts2中文件上傳與下載

文件上傳

  • 瀏覽器端:

    • 1.method=post
    • 2.<input type="file" name="xx">
    • 3.encType=”multipart/form-data”;
  • 服務器端:

    • commons-fileupload組件
      • 1.DiskFileItemFactory
      • 2.ServletFileUpload
      • 3.FileItem
  • struts2中文件上傳

    • 默認情況下struts2框架使用的就是commons-fileupload組件.
    • struts2它使用了一個interceptor幫助我們完成文件上傳操作。
    • <interceptor name="fileUpload" class="org.apache.struts2.interceptor.FileUploadInterceptor"/>
  • 在action中怎樣處理文件上傳?

    • 頁面上組件:<input type="file" name="upload">
  • 在action中要有三個屬性:

    • private File upload; 如果是上傳單個文件, upload屬性的類型就是 java.io.File, 它代表被上傳的文件,要與jsp 頁面上的 file 標籤的名字相同。
    • private String uploadContentType;表示上傳文件的類型,格式:file組件的名稱+ContentType
    • private String uploadFileName; 表示上傳文件的文件名,格式:file組件的名稱+FileName
  • 在execute方法中使用commons-io包下的FileUtils完成文件複製.

    • FileUtils.copyFile(upload, new File("d:/upload",uploadFileName));
@Override
    public String execute() throws Exception {
            System.out.println("上傳文件的類型:" + uploadContentType);
            System.out.println("上傳文件的名稱:" + uploadFileName);

            // 完成文件上傳.
            FileUtils.copyFile(upload, new File("d:/upload", uploadFileName);
        return null;
    }


  • struts.xml 中的配置
<action name="upload" class="cn.itcast.action.UploadAction">
</action>


關於struts2中文件上傳細節
  • 1.關於控制文件上傳大小
  • 在default.properties文件中定義了文件上傳大小
    • struts.multipart.maxSize=2097152 上傳文件默認的總大小 2MB
  • 在struts.xml`中通過修改如下參數,進行修改

    • <constant name="struts.multipart.maxSize" value="20971520"></constant>
  • 2.在struts2中默認使用的是commons-fileupload進行文件上傳。

    • struts.multipart.parser=cos : 上傳組件(備用)
    • struts.multipart.parser=pell : 上傳組件(備用)
    • struts.multipart.parser=jakarta : 默認上傳方式
    • 如果使用pell,cos進行文件上傳,必須導入其jar包.
  • 3.如果出現問題,需要配置input視圖,在頁面上可以通過<s:actionerror>展示錯誤信息.

  • 在頁面上展示的英文信息的國際化操作

  • struts-messages.properties 文件裏預定義 上傳錯誤信息,通過覆蓋對應key 顯示中文信息
struts.messages.error.uploading=Error uploading: {0}
struts.messages.error.file.too.large=The file is to large to be uploaded: {0} "{1}" "{2}" {3}
struts.messages.error.content.type.not.allowed=Content-Type not allowed: {0} "{1}" "{2}" {3}
struts.messages.error.file.extension.not.allowed=File extension not allowed: {0} "{1}" "{2}" {3}
  • 上述信息在自己新建的properties文件修改爲
struts.messages.error.uploading=上傳錯誤: {0}
struts.messages.error.file.too.large=上傳文件太大: {0} "{1}" "{2}" {3}
struts.messages.error.content.type.not.allowed=上傳文件的類型不允許: {0} "{1}" "{2}" {3}
struts.messages.error.file.extension.not.allowed=上傳文件的後綴名不允許: {0} "{1}" "{2}" {3}
  • {0}:<input type=“file” name=“uploadImage”>中name屬性的值
  • {1}:上傳文件的真實名稱
  • {2}:上傳文件保存到臨時目錄的名稱
  • {3}:上傳文件的類型(對struts.messages.error.file.too.large是上傳文件的大小)


  • 4.關於多文件上傳時的每個上傳文件大小控制以及上傳文件類型控制
    • 1.多文件上傳
      • 服務器端 : 只需要將action屬性聲明成List集合或數組就可以。
public class UploadAction extends ActionSupport {

    // 在action類中需要聲明三個屬性
    private List<File> upload;
    private List<String> uploadContentType;
    private List<String> uploadFileName;

    @Override
    public String execute() throws Exception {

        for (int i = 0; i < upload.size(); i++) {


            System.out.println("上傳文件的類型:" + uploadContentType.get(i));
            System.out.println("上傳文件的名稱:" + uploadFileName.get(i));

            // 完成文件上傳.
            FileUtils.copyFile(upload.get(i), new File("d:/upload", uploadFileName.get(i)));
        }
        return null;
    }

}


  • 2.怎樣控制每一個上傳文件的大小以及上傳文件的類型?
    • 在fileupload攔截器中,通過其屬性進行控制.
      • maximumSize—每一個上傳文件大小
      • allowedTypes–允許上傳文件的mimeType類型.
      • allowedExtensions–允許上傳文件的後綴名.
    • 在自定義攔截器配置中加入下代碼:
        <interceptor-ref name="defaultStack">
        <param name="fileUpload.allowedExtensions">txt,mp3,doc</param>
        </interceptor-ref>
  • eg:
        <action name="upload" class="cn.itcast.action.UploadAction">
            <result name="input">/upload.jsp</result>
            <interceptor-ref name="defaultStack">
                <param name="maximumSize">2097152</param>
                <param name="fileUpload.allowedExtensions">txt,mp3,doc</param>
            </interceptor-ref>
        </action>


文件下載

  • 文件下載方式:
    • 1.超連接
    • 2.服務器編碼,通過流向客戶端寫回。
  • 過程:

    • 1.通過response設置 response.setContentType(String mimetype);
    • 2.通過response設置 response.setHeader(“Content-disposition;filename=xxx”);
    • 3.通過response獲取流,將要下載的信息寫出。
  • struts2中文件下載

    • 通過<result type="stream">完成。
    • <result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>在StreamResult類中有三個屬性:
      • protected String contentType = "text/plain"; : 用於設置下載文件的mimeType類型
        • protected String contentDisposition = "inline"; : 用於設置進行下載操作以及下載文件的名稱
        • protected InputStream inputStream; : 用於讀取要下載的文件。
  • eg:

<action name="download" class="cn.itcast.action.DownloadAction">
    <result type="stream">
        <param name="contentType">${contentType}</param> <!-- 調用當前action中的getContentType()方法 -->
        <param name="contentDisposition">attachment;filename=${downloadFileName}</param>
        <param name="inputStream">${inputStream}</param><!-- 調用當前action中的getInputStream()方法 -->
    </result>
</action>
  • 在action類中定義一個方法
public InputStream getInputStream() throws FileNotFoundException {
FileInputStream fis = new FileInputStream("d:/upload/" + filename);
return fis;
}


  • 問題1: <a href="${pageContext.request.contextPath}/download?filename=捕獲.png">捕獲.png</a>下載報錯
    • 原因:超連接是get請求,並且下載的文件是中文名稱,亂碼。
    public InputStream getInputStream() throws FileNotFoundException,
            UnsupportedEncodingException {

        filename = new String(filename.getBytes("iso8859-1"), "utf-8"); // 解決中文名稱亂碼.

        FileInputStream fis = new FileInputStream("d:/upload/" + filename);
        return fis;
    }


  • 問題2:下載捕獲文件時,文件名稱是a.txt

    • 下載文件後綴名應該是png,而我們在配置文件中規定卻是txt?
  • struts.xml 修訂:

<result type="stream">
<param name="contentType">${contentType}</param> <!-- 調用當前action中的getContentType()方法 -->
<param name="contentDisposition">attachment;filename=${downloadFileName}</param>
<param name="inputStream">${inputStream}</param><!-- 調用當前action中的getInputStream()方法 -->
</result>
// 設置下載文件mimeType類型
    public String getContentType() {

        String mimeType = ServletActionContext.getServletContext().getMimeType(
                filename);
        return mimeType;
    }



// 獲取下載文件名稱
public String getDownloadFileName() throws UnsupportedEncodingException {

    return DownloadUtils.getDownloadFileName(ServletActionContext
            .getRequest().getHeader("user-agent"), filename);

}
  • 在struts2中進行下載時,如果使用<result type="stream">它有缺陷
    • 例如:下載點擊後,取消下載,服務器端會產生異常。
    • 在開發中,解決方案:可以下載一個struts2下載操作的插件,它解決了stream問題。




OGNL與ValueStack

OGNL表示式使用

  • OGNL是Object Graphic Navigation Language(對象圖導航語言)的縮寫,它是一個開源項目。 Struts2框架使用OGNL作爲默認的表達式語言。
    • xwork 提供 OGNL表達式
    • ognl-3.0.5.jar
  • OGNL 是一種比EL 強大很多倍的語言
OGNL 提供五大類功能
  • 1、支持對象方法調用,如xxx.doSomeSpecial();
  • 2、支持類靜態的方法調用和值訪問
  • 3、訪問OGNL上下文(OGNL context)和ActionContext; (重點 操作ValueStack值棧 )
  • 4、支持賦值操作和表達式串聯
  • 5、操作集合對象。

  • 使用OGNL訪問 對象方法 和 靜態方法

    • OGNL 在jsp 結合 struts2 標籤庫 使用 , <s:property value="ognl表達式" /> 執行 ognl表達式
    • 調用 實例方法 : 對象.方法() —-> <s:property value="'hello,world'.length()"/>
    • 調用 靜態方法 : @[類全名(包括包路徑)]@[方法名] —> <s:property value="@java.lang.String@format('您好,%s','小明')"/>
    • 使用 靜態方法調用 必須 設置 struts.ognl.allowStaticMethodAccess=true
  • eg : 對象調方法

    public static void main(String[] args) throws OgnlException {
        // ognl可以通過對象調用方法.

        // System.out.println("aaa".length());

        // 使用ognl來完成上面操作.

        // 1.創建一個ognl上下文。
        OgnlContext context = new OgnlContext();

        Object obj1 = Ognl.getValue("'aaa'.length()", context.getRoot());

        System.out.println(obj1);

    }


  • 靜態方法
    public static void main(String[] args) throws OgnlException {
        // ognl可以通過對象調用方法.

        System.out.println(Math.max(10, 20));
        System.out.println(Math.PI);
        // 使用ognl來完成上面操作.

        // 1.創建一個ognl上下文。
        OgnlContext context = new OgnlContext();

        Object obj1 = Ognl.getValue("@java.lang.Math@max(10,20)", context.getRoot());

        System.out.println(obj1);

        Object obj2= Ognl.getValue("@java.lang.Math@PI", context.getRoot());

        System.out.println(obj2);

    }


  • 訪問OGNL上下文(OGNL context)和ActionContext
    public static void main(String[] args) throws OgnlException {

        // 創建一個ognl上下文
        OgnlContext context = new OgnlContext(); // 本質上就是一個Map集合.

        Person p = new Person();
        p.setName("張三");

        Dog dog = new Dog();
        dog.setName("lucy");

        p.setDog(dog); //張三有條狗叫lucy

        context.setRoot(p);

        Dog dog1 = new Dog();
        dog1.setName("豆豆");

        Person pp=new Person();
        pp.setName("james");
        dog1.setP(pp);

        context.put("dog", dog1);

        context.put("name", "tom");

        // 使用 ognl來獲取根中數據 獲取根中數據,不需要加#
        Object name1 = Ognl.getValue("name", context, context.getRoot());

        System.out.println(name1);

        // 使用ognl來獲取非根中的數據 獲取非根中數據,需要使用#

        Object name2 = Ognl.getValue("#name", context, context.getRoot());

        System.out.println(name2);

        //獲取出張三的的狗的名稱
        //張三是root中故直接指向根張三
        Object name3 = Ognl.getValue("dog.name", context, context.getRoot());

        System.out.println(name3);

        //豆豆的主人名稱
        Object name4=Ognl.getValue("#dog.p.name", context, context.getRoot());

        System.out.println(name4);


    }
}


//運行結果:
    //張三
    //tom
    //lucy
    //james


* OGNL上下文(OGNL context)對象 值棧 ValueStack *

  • 什麼是值棧 ValueStack ?

    • ValueStack 是 struts2 提供一個接口,實現類 OgnlValueStack —- 值棧對象 (OGNL是從值棧中獲取數據的 )
    • 每個Action實例都有一個ValueStack對象 (一個請求 對應 一個ValueStack對象 )
    • 在其中保存當前Action 對象和其他相關對象 (值棧中 是有Action 引用的 )
    • Struts 框架把 ValueStack 對象保存在名爲 “struts.valueStack” 的請求屬性中,request中 (值棧對象 是 request一個屬性)
  • 值棧的內部結構 ?

    • 值棧由兩部分組成
      • ObjectStack: Struts 把動作和相關對象壓入 ObjectStack 中–List
      • ContextMap: Struts 把各種各樣的映射關係(一些 Map 類型的對象) 壓入 ContextMap 中,Struts 會把下面這些映射壓入 ContextMap 中
        • parameters: 該 Map 中包含當前請求的請求參數
        • request: 該 Map 中包含當前 request 對象中的所有屬性
        • session: 該 Map 中包含當前 session 對象中的所有屬性
        • application:該 Map 中包含當前 application 對象中的所有屬性
        • attr: 該 Map 按如下順序來檢索某個屬性: request, session, application
  • ValueStack中 存在root屬性 (CompoundRoot) 、 context 屬性OgnlContext

    • CompoundRoot 就是ArrayList
    • OgnlContext 就是 Map
  • context 對應Map 引入 root對象

    • context中還存在 request、 session、application、 attr、 parameters 對象引用
    • OGNL表達式,訪問root中數據時 不需要 #, 訪問 request、 session、application、 attr、 parameters 對象數據 必須寫 #
    • 操作值棧 默認指 操作 root 元素
  • 值棧對象的創建 ,ValueStack 和 ActionContext 是什麼關係 ?

    • 值棧對象 是請求時 創建的
    • doFilter中 prepare.createActionContext(request, response);
      • 創建ActionContext 對象過程中,創建 值棧對象ValueStack
      • ActionContext對象 對 ValueStack對象 有引用的 (在程序中 通過 ActionContext 獲得 值棧對象 )
    • Dispatcher類 serviceAction 方法中 將值棧對象保存到 request範圍
      • request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());
  • 獲得值棧對象的兩種方法

    • ValueStack valueStack = (ValueStack) ServletActionContext.getRequest().getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);
    • ValueStack valueStack2 = ActionContext.getContext().getValueStack();
  • 向值棧保存數據 (主要針對 root)的兩種方式

    • 將數據保存root的索引0位置,放置到第一個元素
      • ArrayList add(0,element);
      • valueStack.push(“itcast”);
    • 在值棧創建參數map, 將數據保存到map中
      • valueStack.set(“company”, “傳智播客”);
    • 在jsp中 通過 <s:debug /> 查看值棧的內容
  • 在JSP中獲取值棧的數據

    • 訪問root中數據 不需要#
    • 訪問 其它對象數據 加 #
  • 通過下標獲取root中對象

    • <s:property value="[0].top"/> //取值棧頂對象
  • 直接在root中查找對象屬性 (自上而下自動查找)

    • valueStack:<s:property value="username"/>
  • 在OgnlContext中獲取數據

request:<s:property value="#request.username"/>
session:<s:property value="#session.username"/>
application:<s:property value="#application.username"/>
attr:<s:property value="#attr.username"/>
parameters:<s:property value="#parameters.cid[0]"/>
發佈了91 篇原創文章 · 獲贊 177 · 訪問量 43萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章