歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> struts2中頁面取值的原理以及valueStack的應用

struts2中頁面取值的原理以及valueStack的應用

日期:2017/3/1 10:15:33   编辑:Linux編程
  1. //獲取封裝輸出信息的 ValueStack 對象
  2. ValueStack vs = (ValueStack)request.getAttribute("struts.valueStack");
  3. //調用ValueStack的fineValue方法獲取books屬性值
  4. String[] books = (String[])vs.findValue("books");

上面代碼返回一個 ValueStack 對象,該對象封裝了 Action 全部的輸出信息。該對象是 Struts 2 使用的一個 ValueStack對象,可以通過 OGNL 表達式非常方便的訪問該對象封裝的信息。

ValueStack 有點類似於 Map 結構,但它比 Map 結構更加強大(因為它可以根據表達式來查詢值)。Action 所以的屬性都被封裝到了 ValueStack 對象中,Action 中的屬性名可以理解為 ValueStack 中 value 的名字。

web.xml

  1. <?xml version="1.0" encoding="GBK"?>
  2. <!-- 配置Web應用配置文件的根元素,並指定配置文件的Schema信息 -->
  3. <web-app xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  7. <!-- 定義Struts 2的核心控制器:FilterDispatcher -->
  8. <filter>
  9. <!-- 定義核心Filter的名字 -->
  10. <filter-name>struts2</filter-name>
  11. <!-- 定義核心Filter的實現類 -->
  12. <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  13. </filter>
  14. <!-- FilterDispatcher用來初始化Struts 2並且處理所有的HTTP請求 -->
  15. <filter-mapping>
  16. <filter-name>struts2</filter-name>
  17. <url-pattern>/*</url-pattern>
  18. </filter-mapping>
  19. </web-app>

Struts.xml

  1. <?xml version="1.0" encoding="GBK"?>
  2. <!-- 指定Struts 2配置文件的DTD信息 -->
  3. <!DOCTYPE struts PUBLIC
  4. "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
  5. "http://struts.apache.org/dtds/struts-2.1.dtd">
  6. <!-- struts是Struts 2配置文件的根元素 -->
  7. <struts>
  8. <!-- Struts 2的Action必須放在指定的包空間下定義 -->
  9. <package name="strutsqs" extends="struts-default">
  10. <!-- 定義login的Action,該Action的實現類為lee.LoginAction類 -->
  11. <action name="login" class="lee.LoginAction">
  12. <!-- 定義處理結果和視圖資源之間映射關系 -->
  13. <result name="error">/error.jsp</result>
  14. <result name="success">/welcome.jsp</result>
  15. </action>
  16. <!-- 定義獲取圖書的Action,對應實現類為lee.GetBooksAction -->
  17. <action name="getBooks" class="lee.GetBooksAction">
  18. <!-- 如果處理結果返回login,進入login.jsp頁面 -->
  19. <result name="login">/login.jsp</result>
  20. <!-- 如果處理結果返回success,進入showBook.jsp頁面 -->
  21. <result name="success">/showBook.jsp</result>
  22. </action>
  23. </package>
  24. </struts>

GetBooksAction.java

  1. import com.opensymphony.xwork2.Action;
  2. import com.opensymphony.xwork2.ActionContext;
  3. public class GetBooksAction implements Action
  4. {
  5. //該屬性並不用於封裝用戶請求參數,而用於封裝Action需要輸出到JSP頁面的信息
  6. private String[] books;
  7. //books屬性的setter方法
  8. public void setBooks(String[] books)
  9. {
  10. this.books = books;
  11. }
  12. //books屬性的getter方法
  13. public String[] getBooks()
  14. {
  15. return books;
  16. }
  17. //處理用戶請求的execute方法
  18. public String execute() throws Exception
  19. {
  20. //獲取Session中的user屬性
  21. String user = (String)ActionContext.getContext()
  22. .getSession().get("user");
  23. //如果user屬性不為空,且該屬性值為crazyit
  24. if (user != null && user.equals("crazyit"))
  25. {
  26. //創建BookService實例
  27. BookService bs = new BookService();
  28. //將業務邏輯組件的返回值設置成該Action的屬性
  29. setBooks(bs.getLeeBooks());
  30. return SUCCESS;
  31. }
  32. else
  33. {
  34. return LOGIN;
  35. }
  36. }
  37. }

BookService.java

  1. public class BookService
  2. {
  3. //以一個數組模擬從持久存儲設備(數據庫)中取出的數據
  4. private String[] books =
  5. new String[] {
  6. "瘋狂Java講義" ,
  7. "輕量級Java EE企業應用實戰",
  8. "瘋狂Ajax講義",
  9. "瘋狂XML講義",
  10. "Struts 2權威指南"
  11. };
  12. //業務邏輯方法,該方法返回全部圖書
  13. public String[] getLeeBooks()
  14. {
  15. return books;
  16. }
  17. }

showBooks.jsp

  1. <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
  2. <%@ page import="java.util.*,com.opensymphony.xwork2.util.*"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  4. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5. <html xmlns="http://www.w3.org/1999/xhtml">
  6. <head>
  7. <title>圖書</title>
  8. <meta name="website" content="http://www.crazyit.org" />
  9. </head>
  10. <body>
  11. <table border="1" width="360">
  12. <caption>圖書</caption>
  13. <%
  14. //獲取封裝輸出信息的ValueStack對象
  15. ValueStack vs = (ValueStack)request
  16. .getAttribute("struts.valueStack");
  17. //調用ValueStack的fineValue方法獲取books屬性值
  18. String[] books = (String[])vs.findValue("books");
  19. //迭代輸出全部圖書信息
  20. for (String book : books)
  21. {
  22. %>
  23. <tr>
  24. <td>書名:</td>
  25. <td><%=book%></td>
  26. </tr>
  27. <%}%>
  28. </table>
  29. </body>
  30. </html>
Copyright © Linux教程網 All Rights Reserved