點(diǎn)擊完成以后,發(fā)現(xiàn)在Outline View中新增加了一個(gè)UserBean.
Figure 4.5: UserBean now shown in the Outline View
UserBean.java的源代碼也出現(xiàn)在 java編輯器中。
Figure 4.6: UserBean Java source opened up in an editor
Username 和password的setters和getters方法已經(jīng)為我們產(chǎn)生了,接下來(lái)就是要在這個(gè)類中增加一個(gè)方法loginUser來(lái)處理用戶登錄的操作。
代碼如下所示:
UserBean.java
public String loginUser() ...{
if("myeclipse".equals(getUserName()) && "myeclipse".equals(getPassword()))
return "success";
FacesContext facesContext = FacesContext.getCurrentInstance();
FacesMessage facesMessage = new FacesMessage(
"You have entered an invalid user name and/or password");
facesContext.addMessage("loginForm", facesMessage);
return "failure";
}
我們注意到,UserBean類沒(méi)有繼承任何JSF的類或接口,它只是一個(gè)簡(jiǎn)單的JavaBean包括額外邏輯來(lái)執(zhí)行操作。他包括了類似Struts中的Struts Form 和 Struts Action的功能,將二者集成到一個(gè)類中.
另外,這些方法并沒(méi)有返回到指定的類,像Struts中的ActionForward那樣.
五.創(chuàng)建JSP頁(yè)面
在這部分我們將創(chuàng)建兩個(gè)jsp頁(yè)面,一個(gè)是用戶登錄的頁(yè)面,另一個(gè)是表明登錄成功的頁(yè)面.這兩個(gè)頁(yè)面各自為loginUser.jsp和 loginUserSuccess.jsp,為了使應(yīng)用程序簡(jiǎn)單,如果登錄出現(xiàn)異常,我們將返回loginUser.jsp頁(yè)面,并沒(méi)有增加任何驗(yàn)證.我們可以通過(guò)faces-config.xml文件來(lái)創(chuàng)建jsp頁(yè)面.點(diǎn)擊如圖所示的JSP按鈕來(lái)創(chuàng)建userLogin.jsp頁(yè)面,出現(xiàn)JSP建立向?qū)?
Figure 5.1: Creating userLogin.jsp using the faces-config.xml editor
以同樣的方式來(lái)創(chuàng)建userLoginSuccess.jsp頁(yè)面
Figure 5.2: Creating userLoginSuccess.jsp using the faces-config.xml editor
接下來(lái)編輯userLogin.jsp頁(yè)面
Figure 5.3: Begin editing the userLogin.jsp page
MyEclipse JSP Designer可以采用Source 方式, Design 方式, Design/Source 方式來(lái)編輯JSP文件.在這個(gè)Demo中采用Design/Source方式,如圖:
Figure 5.4: Switched to Design/Source mode and expanded JSF palettes
接下來(lái)我們需要在頁(yè)面中添加如下代碼:
為用戶名增加h:inputText組件
為密碼增加h:inputSecret組件
增加用戶名輸入框h:outputLabel
增加密碼輸入框h:outputLabel
同時(shí)頁(yè)面要使用我們自己的MessageBundle.
Figure 5.5: Remove template text and add our MessageBundle to the JSP page
接下來(lái),我們創(chuàng)建HTML form元素,用來(lái)包括登錄控件,如圖:
Figure 5.5a: Create the new form
現(xiàn)在來(lái)創(chuàng)建username 的組件h:inputText,如圖:
Figure 5.6: Adding new inputText component
Figure 5.7: Adding new inputText component continued
1