First, let's start with the JavaBean in Model1. We know that the basic architecture of Model1 is to access the data layer through the JavaBean on the JSP page. What exactly is the JavaBean?
In fact, JavaBean is only a special Java class. In JavaBean, getXXX and setXXX are used for access to private fields, and a non-parameter constructor is required. The following class is a JavaBean file.
- PackageNet. moon. beans;
- Public ClassUserInfo {
- PrivateString userName;
- PrivateString password;
- PublicString getUserName (){
- ReturnUserName;
- }
- PublicString getPassword (){
- ReturnPassword;
- }
- Public VoidSetUserName (String userName ){
- This. UserName = userName;
- }
- Public VoidSetPassword (String password ){
- This. Password = password;
- }
- // If there is no constructor in a class, Java will add a default null non-parameter constructor for it,
- // Therefore, this constructor can be omitted if no other constructor exists.
- PublicUserInfo (){
- }
- }
Note the format requirements. The domain name is in the lower case (usernName) of the first word, corresponding to a domain, which has two methods: getUserName and setUserName to read and set this domain, method Name: get/set + domain name (uppercase ).
People who have developed Model1 will know that the introduction of JavaBean can be introduced using tags <jsp: useBean>. developers can generate an instance of this type without instantiating it, because the instance is generated by a Web Container, we must provide a non-parameter constructor called by the Web container.
Let's explain the MBean in JSF. In fact, MBean is a JavaBean, so it has the same requirements as JavaBean.
Finally, MBean configuration. The MBean of JSF needs to be configured in the faces-config file. The configuration method is as follows:
- <managed-bean>
- <description>demo of config</description>
- <display-name>userInfo</display-name>
- <managed-bean-name>user</managed-bean-name>
- <managed-bean-class>net.moon.beans.UserInfo</managed-bean-class>
- <managed-bean-scope>session</managed-bean-scope>
- </managed-bean>
Here, I will explain managed-bean-name, managed-bean-class, and managed-bean-scope.
Managed-bean-nameIs the name of this MBean for calling elsewhere.
Managed-bean-classIs the complete path of the MBean, used to specify the location of the MBean class file.
Managed-bean-scopeIs the valid range of this MBean.
Next we will explain in detail manage-bean-scope. The valid values are application, session, request, and none. It is easy to understand that their lifecycles are as follows:
Name Scope
Application entire Application
Session
Request the entire Request
When None is required
As we all know, JSF is Based on JSP. For the nine JSP objects, how are the information of these four scopes stored?
◆ The test proves that the MBean instance of scope is applicatoin is stored in ServletContext, that is, the application in JSP. Therefore, we can use the following method to get reference of a class: facesContext fc = FacesContext. getCurrentInstance ();
UserInfo ui = (serInfo) fc. getExternalContext (). getApplicationMap (). get ("user ");
◆ For session-level MBean, we can use the following method to get its reference:
FacesContext fc = FacesContext. getCurrentInstance ();
UserInfo ub = (UserInfo) fc. getExternalContext (). getSessionMap (). get ("userInfo ");
Of course, we can also use other methods to get the session object and retrieve the instance from the session.
◆ We can retrieve the request-level MBean from the request object. The Code is as follows:
FacesContext fc = FacesContext. getCurrentInstance ();
HttpServletRequest request = (HttpServletRequest) fc. getExternalContext (). getRequest ();
UserInfo ui = (UserInfo) request. getAttribute ("user ");
◆ For mbeans of the none type, only new instances can be obtained.
Of course, JSF provides another method to access MBean. We can use the following code to obtain MBean instances:
FacesContext context = FacesContext. getCurrentInstance ();
ValueBinding binding = context. getApplication (). createValueBinding ("# {user }");
UserBean user = (UserBean) binding. getValue (context );
You can also use the following code to directly obtain an attribute of MBean:
FacesContext context = FacesContext. getCurrentInstance ();
ValueBinding binding = context. getApplication (). createValueBinding ("# {user. name }");
String name = (String) binding. getValue (context );
- Self-learning Javabean quickly became a Java expert
- JavaBeans, EJB, and POJO
- Differences between JavaBean and EJB
- Add XPath to JavaBeans
- Tips for using JavaBean