首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

深入理解 JSF 作用域與受管 Bean:請求、會話、應用層級資料管理

     編輯:Java 相關知識

核心概念:三大作用域的生命週期與適用場景

在 JSF 應用中,資料存儲範圍決定了資料的存活時間與可見性。NetBeans Visual Web 自動建立三個預設受管 Bean:

  • RequestBean1(請求作用域):單次 HTTP 請求週期,回應完成即銷毀
  • SessionBean1(會話作用域):用戶首次訪問至會話逾時或失效
  • ApplicationBean1(應用作用域):應用啟動至關閉,全域共享

導覽視窗中的受管 Bean

作用域持續時間對照表

Web 應用程式作用域生命週期

作用域開始時間結束時間典型用途
Application應用啟動應用關閉下拉選單選項、系統配置、快取資料
Session用戶首次訪問會話逾時/失效用戶登入資訊、購物車、偏好設定
Request提交頁面回應完成表單暫存資料、單次計算結果

實戰案例:程式語言投票系統

建立 Scopes 專案(Visual Web JSF),完成四個受管 Bean 屬性配置:

ApplicationBean1 - 應用層級共享資料

private Option[] ballotOptions;  // 投票選項陣列
private HashMap tally;           // 計票統計

// init() 初始化
ballotOptions = new Option[] {
    new Option("java", "Java Programming Language"),
    new Option("cpp", "C++"),
    new Option("fortran", "Fortran")
};
tally = new HashMap();
for (int i = 0; i < ballotOptions.length; i++) {
    tally.put(ballotOptions[i].getValue(), "0");
}

// 業務方法
public void incrementTallyFor(String category) { ... }
public int getTallyFor(String category) { ... }

SessionBean1 - 會話層級用戶狀態

private boolean hasVoted;  // 防止重複投票

// init()
setHasVoted(false);

RequestBean1 - 請求層級暫存資料

private java.util.Date timestamp;  // 投票時間戳記

三大 Bean 屬性總覽

頁面設計與資料綁定

Page1.jsp - 投票頁面

  • 標籤:「Reader's Poll: What Is Your Favorite Programming Language?」
  • 單選按鈕組 voteRBGroup 綁定 ApplicationBean1.ballotOptions
  • Submit Vote 按鈕:disabled 綁定 SessionBean1.hasVoted,動作處理投票邏輯
  • View Results 按鈕:導向結果頁

投票頁面設計

public String voteButton_action() {
    if (voteRBGroup.getSelected() == null) return null;
    String votedFor = voteRBGroup.getSelected().toString();
    getApplicationBean1().incrementTallyFor(votedFor);
    getSessionBean1().setHasVoted(true);
    getRequestBean1().setTimestamp(new Date());
    return null;
}

Results.jsp - 結果頁面

  • 網格面板包含兩個靜態文本元件:resultsST(HTML 表格顯示統計)、messageST(投票時間)
  • escape 設為 false 允許 HTML 輸出
  • prerender() 動態生成統計表格與時間顯示

結果頁面設計 元件放入網格面板

public void prerender() {
    ApplicationBean1 appBean = getApplicationBean1();
    Option[] choices = appBean.getBallotOptions();
    String str = "";
    for (int i = 0; i < choices.length; i++) {
        int count = appBean.getTallyFor(choices[i].getValue().toString());
        str += "";
    }
    str += "
" + choices[i].getLabel() + "" + count + "
"; resultsST.setText(str); RequestBean1 reqBean = getRequestBean1(); Date timestamp = (Date) reqBean.getTimestamp(); if (timestamp != null) { messageST.setText("Your vote was recorded at " + DateFormat.getTimeInstance(DateFormat.LONG).format(timestamp)); } }

頁面導航與會話逾時設定

頁面流編輯器

  • Page1.jsp → Results.jsp(view results / vote)
  • Results.jsp → Page1.jsp(home)
  • web.xml 設定會話逾時 1 分鐘:

WEB-INF 目錄 會話逾時設定

執行驗證重點

  1. 投票後 Submit Vote 變灰(Session 作用域生效)
  2. 點 View Results 不顯示時間(Request Bean 已重建)
  3. 等待 1 分鐘會話逾時,新會話可再次投票,統計數字累計(Application Bean 持久)
  4. 多瀏覽器測試驗證跨會話資料共享

關鍵提醒

導航規則包含 <redirect/> 時,不可使用 Request Bean——重導向會在目標頁面可用前結束請求,導致 Request Bean 資料遺失。

擴充練習

  • 加入登入頁面,將用戶名存入 SessionBean
  • 建立統計頁顯示總訪客數(ApplicationBean 計數器)
  • 嘗試自訂受管 Bean(專案右鍵 New → Other → JavaServer Faces → Managed Bean)
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved