JSF Quickstart With MyEclipse---4
天下维客,你可以修改的网络知识库
|
JSF编辑 相关文档:
其他资源: |
Creating the Managed Beans
In this section we'll see how to create the javabean that will perform the login operation when prompted by the login JSP page. For the purpose of this demo, the login operation is simply to check that the username and password are both "myeclipse" and then redirect the user to the userLoginSuccess.jsp page if this is the case.
Before creating the login javabean, we'll create a new Java package to contain the class using the New Package Wizard that can be launched by selecting File > New > Package, as shown in Figures 10.
Figure 10 - Creating a Java Package
After the New Package Wizard finishes execution, we can use the New Class Wizard to generate the the javabean class itself. The New Class Wizard can be launched by selecting it at File > New > Class and can be configured to create the UserBean.java javabean class as shown in Figure 11, below.
Figure 11 - Creating the UserBean class
After creating the UserBean class, we need implement the accessor methods that will make it conform to the javabean specification as well as perform the login operation. When these simple tasks are completed, the javabean class will look like Figure 12.
UserBean.java
package com.jsfdemo.bean;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
public class UserBean {
private String userName;
private String password;
public UserBean() {
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
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";
}
}
Figure 12 - UserBean.java
Looking at the bean code, we can notice some unique properties about it. For example, the UserBean class doesn't extend or implement any classes or interfaces tied to JSF. It is simply a javabean that includes the additional logic to perform a useful operation. In Struts terms, it contains all the functionality of a Struts Form and a Struts Action, conveniently located in one class.
Another thing to note is that unlike Struts, these methods do not return special classes, like an ActionForward, because navigation is specified externally in a declarative fashion in the faces-config.xml deployment descriptor. And, in Section 8 we will show how this descriptor is created and configured.




