?? 使用javabean高效處理jsp(3).txt
字號(hào):
作者:Eazily
email: Eazily@yeah.net
日期:6/12/2001 2:50:16 PM
form處理、動(dòng)態(tài)內(nèi)容和bean通信
列表4展示了一個(gè)具體的JSP JavaBean--LoginJSPBean,用來實(shí)現(xiàn)特定的頁(yè)面處理
列表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方法。就象上面提及的,它們用作動(dòng)態(tài)的匹配,并且用來在form字段(request參數(shù))和bean屬性間傳送值。
列表4中的beanProcess()方法,展示了form處理的一些基本點(diǎn)。這個(gè)方法發(fā)生在頁(yè)面輸出前,在第二和全部后來的頁(yè)面調(diào)用期間執(zhí)行。這意味著它將僅在用戶按下登錄按鈕并且提交form后執(zhí)行。
你首先要驗(yàn)證voteId和password的輸入,產(chǎn)生的錯(cuò)誤將通過addErrorMsg方法記錄下來。這個(gè)方法設(shè)置AbstractJSPBean類的errorMsg屬性。該屬性可被JSP用來顯示用戶的錯(cuò)誤:
<jsp:getProperty name="_loginJSPBean" property="errorMsg"/>
如果數(shù)據(jù)的輸入成功通過,beanProcess()方法將會(huì)調(diào)用數(shù)據(jù)庫(kù)來驗(yàn)證用戶。最后,它通過調(diào)用AbstractJSPBean類中實(shí)現(xiàn)的redirect()方法,將請(qǐng)求重定向到相應(yīng)的頁(yè)面。
以下我們將討論VoteJSPBean類中的一些方法。它們將可以解釋該架構(gòu)的一些其它方面,例如JSP JavaBean之間的通信和應(yīng)用的流程控制。
列表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對(duì)象。SharedSessionBean類通過使用一個(gè)簡(jiǎn)單的方法,讓所有的JSP JavaBean對(duì)象在一個(gè)HTTP session中進(jìn)行通信。它保存有一個(gè)session內(nèi)的全部JSP JavaBean中的一個(gè)Map。Map是Java Collections框架的一個(gè)接口,它是Java 1.2推出的。對(duì)熟悉Java 1.1的人來說,它與Hashtable非常類似。一個(gè)JSP JavaBean的主鍵是它的PAGE_CODE,它作為一個(gè)常數(shù)存儲(chǔ)在每個(gè)JSP JavaBean類中。
在這個(gè)例子中,beanFirstPassProcess()方法首先定位到LoginJSPBean對(duì)象。接著,它由LoginJSPBean對(duì)象中得到Voter對(duì)象,并且存儲(chǔ)一個(gè)到它的引用,以便以后使用。如果Voter為null,這意味著用戶沒有首先登錄就進(jìn)入Voter頁(yè)面,因此它重定向到登錄頁(yè)面。這是一個(gè)應(yīng)用流程控制的簡(jiǎn)單例子。你可以設(shè)計(jì)更復(fù)雜的方法,例如使用一個(gè)智能的調(diào)度程序,不過這些討論已經(jīng)超出了本文的范圍。
列表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調(diào)用,通過以下的方法:
<jsp:getProperty name="_voteJSPBean" property="candidateList"/>
根據(jù)由數(shù)據(jù)庫(kù)得到的內(nèi)容不同,該方法提供不同的動(dòng)態(tài)HTML內(nèi)容輸出。它需要開發(fā)JavaBean的Java編程者懂得一些HTML知識(shí)。
你也可以使用一個(gè)利用HTML的獨(dú)立庫(kù)來格式化HTML,它可以接受一個(gè)預(yù)定義的輸入。例如Iterator,然后以預(yù)定義的格式產(chǎn)生HTML輸出。另一個(gè)方法是使用標(biāo)簽庫(kù)。
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -