使用JavaBean高效處理JSP(3)

- 中國WEB開發者網絡 (http://www.webasp.net)
-- 技術教程 (http://www.webasp.net/article/)
--- 使用JavaBean高效處理JSP(3) (http://www.webasp.net/article/8/7525.htm)
-- 作者:未知
-- 發佈日期: 2003-07-26
form處理、動態內容和bean通信

  列表4展示了一個具體的JSP JavaBean--LoginJSPBean,用來實現特定的頁面處理

  列表4。LoginJSPBean


package lbm.examples;

import lbm.jsputil.*;
import java.util.*;

public class LoginJSPBean extends AbstractJSPBean {

public static final String PAGE_CODE = "login";

private String _voterId;
private String _password;
private Voter _voter = null;

public LoginJSPBean() {
}

public void setVoterId (String newVoterId) {
_voterId = newVoterId;
}

public String getVoterId() {
if (_voterId == null) return "";
else return _voterId;
}

public void setPassword (String newPassword) {
_password = newPassword;
}

public String getPassword() {
if (_password == null) return "";
else return _password;
}

public Voter getVoter () {
return _voter;
}

protected void beanProcess () throws java.io.IOException {
if (_voterId == null || _voterId.equals("")) {
addErrorMsg("Voter must be entered");
}

if (_password == null || _password.equals("")) {
addErrorMsg("Password must be entered");
}

if (getState() != ERR) {
file://If all the fields are entered, try to login the voter
Voter voter = VoteDB.login(_voterId, _password);
if (voter == null) {
addErrorMsg("Unable to authenticate the Voter. Please try again.");
}
else {
_voter = voter;

if (_voter.getVotedForCandidate() != null) {
// if the voter has already voted, send the voter to the last page
redirect("confirmation.jsp");
}
else {
// go to the Vote page
redirect("vote.jsp");
}
}
}
}

protected void beanFirstPassProcess() throws java.io.IOException {
}

protected void beanFooterProcess() throws java.io.IOException {
}

protected String getJSPCode() {
return PAGE_CODE;
}
}



  觀察LoginJSPBean類中的set和get方法。就像上面提及的,它們用作動態的匹配,並且用來在form字段(request參數)和bean屬性間傳送值。

  列表4中的beanProcess()方法,展示了form處理的一些基本點。這個方法發生在頁面輸出前,在第二和全部後來的頁面調用期間執行。這意味著它將僅在用戶按下登錄按鈕並且提交form後執行。

  你首先要驗證voteId和password的輸入,產生的錯誤將通過addErrorMsg方法記錄下來。這個方法設置AbstractJSPBean類的errorMsg屬性。該屬性可被JSP用來顯示用戶的錯誤:

   <jsp:getProperty name="_loginJSPBean" property="errorMsg"/>

  如果數據的輸入成功通過,beanProcess()方法將會調用數據庫來驗證用戶。最後,它通過調用AbstractJSPBean類中實現的redirect()方法,將請求重定向到相應的頁面。

  以下我們將討論VoteJSPBean類中的一些方法。它們將可以解釋該架構的一些其它方面,例如JSP JavaBean之間的通信和應用的流程控制。

  列表5。VoteJSPBean類中的beanFirstPassProcess()


protected void beanFirstPassProcess() throws java.io.IOException {
// get the Voter from Login page
_voter = null;

LoginJSPBean loginJSPBean =
(LoginJSPBean) getSharedSessionBean().getJSPBean(LoginJSPBean.PAGE_CODE);

if (loginJSPBean != null) {
_voter = loginJSPBean.getVoter();
}

if (_voter == null) {
// voter is not logged in yet. Send it to Login page
setState(NEW);
redirect("login.jsp");
}
}



以上的方法使用了AbstractJSPBean類中_sharedSessionBean對象。SharedSessionBean類通過使用一個簡單的方法,讓所有的JSP JavaBean對像在一個HTTP session中進行通信。它保存有一個session內的全部JSP JavaBean中的一個Map。Map是Java Collections框架的一個接口,它是Java 1.2推出的。對熟悉Java 1.1的人來說,它與Hashtable非常類似。一個JSP JavaBean的主鍵是它的PAGE_CODE,它作為一個常數存儲在每個JSP JavaBean類中。

  在這個例子中,beanFirstPassProcess()方法首先定位到LoginJSPBean對象。接著,它由LoginJSPBean對像中得到Voter對象,並且存儲一個到它的引用,以便以後使用。如果Voter為null,這意味著用戶沒有首先登錄就進入Voter頁面,因此它重定向到登錄頁面。這是一個應用流程控制的簡單例子。你可以設計更複雜的方法,例如使用一個智能的調度程序,不過這些討論已經超出了本文的範圍。

  列表6。VoteJSPBean類的getCandidateList()方法


public String getCandidateList () {
StringBuffer candidateList = new StringBuffer();
Candidate candidate;

Iterator candidates = VoteDB.getCandidates();
while (candidates.hasNext()) {
candidate = (Candidate) candidates.next();

candidateList.append("<input type=radio name=\"candidateName\" value=\"");
candidateList.append(candidate.getName());
candidateList.append("\"> ");
candidateList.append(candidate.getName());
candidateList.append("<br>\n");
}

return candidateList.toString();
}



  以上的getCandidateList()方法被vote.jsp調用,通過以下的方法:

   <jsp:getProperty name="_voteJSPBean" property="candidateList"/>

  根據由數據庫得到的內容不同,該方法提供不同的動態HTML內容輸出。它需要開發JavaBean的Java編程者懂得一些HTML知識。

  你也可以使用一個利用HTML的獨立庫來格式化HTML,它可以接受一個預定義的輸入。例如Iterator,然後以預定義的格式產生HTML輸出。另一個方法是使用標籤庫。

webasp.net