JavaBean and MBean

Source: Internet
Author: User

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.

 
 
  1. PackageNet. moon. beans;
  2. Public ClassUserInfo {
  3. PrivateString userName;
  4. PrivateString password;
  5. PublicString getUserName (){
  6. ReturnUserName;
  7. }
  8. PublicString getPassword (){
  9. ReturnPassword;
  10. }
  11. Public VoidSetUserName (String userName ){
  12. This. UserName = userName;
  13. }
  14. Public VoidSetPassword (String password ){
  15. This. Password = password;
  16. }
  17. // If there is no constructor in a class, Java will add a default null non-parameter constructor for it, 
  18. // Therefore, this constructor can be omitted if no other constructor exists. 
  19. PublicUserInfo (){
  20. }
  21. }

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:

 
 
  1. <managed-bean>   
  2.     <description>demo of config</description>   
  3.     <display-name>userInfo</display-name>   
  4.     <managed-bean-name>user</managed-bean-name>   
  5.     <managed-bean-class>net.moon.beans.UserInfo</managed-bean-class>   
  6.     <managed-bean-scope>session</managed-bean-scope>   
  7. </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 );

  1. Self-learning Javabean quickly became a Java expert
  2. JavaBeans, EJB, and POJO
  3. Differences between JavaBean and EJB
  4. Add XPath to JavaBeans
  5. Tips for using JavaBean

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.