使用JavaBean高效處理JSP(3)
2024-09-05 00:19:11
供稿:網友
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用來顯示用戶的錯誤:
?。糺sp: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調用,通過以下的方法:
?。糺sp:getproperty name="_votejspbean" property="candidatelist"/>
根據由數據庫得到的內容不同,該方法提供不同的動態html內容輸出。它需要開發javabean的java編程者懂得一些html知識。
你也可以使用一個利用html的獨立庫來格式化html,它可以接受一個預定義的輸入。例如iterator,然后以預定義的格式產生html輸出。另一個方法是使用標簽庫。