The Java Bean Class File – Develop Java Web Applications to Access Databases

8.1.6.3   The Java Bean Class File

The Java bean class used in JSF pages is very similar to the FacultyBean class we built in Section 8.1.5.1. Like most Java bean classes, it should contain setter and getter methods as well as some special methods to process the business logic.

In addition, Java beans need to be conFigured in the application configuration resource file faces-config.xml so that the implementation can automatically create new instances of the beans as needed. The <managed-bean> element is used to create a mapping between a bean name and class. The first time the QueryBean is referenced, the object is created and stored in the appropriate scope. You can use the code elements shown in Figure 8.27 to register a Java bean in the faces-config.xml file: Besides registering the Java bean class, you also need to use the configu-ration file to conFigure and define all properties created inside the Java bean. In this example, only two properties, userName and passWord, have been defined in this Java bean. Therefore, you need to use the <managed-property> element to do the configuration, as shown in Figure 8.28.

In fact, you do not need to worry about the configuration if you are using an IDE such as the NetBeans IDE, which can do the configuration automatically for you as you build the Java bean class file.

Next let’s take a look at the Web deployment descriptor file.

8.1.6.4   The Web Deployment Descriptor File, web.xml

Before you can use and access a Servlet such as FacesServlet on the server side from a Web browser, you need to map the FacesServlet to a path in your deployment descriptor file, web.xml. By

FIGURE 8.27   A piece of sample code to register a Java bean.

FIGURE 8.28   A piece of code to define all properties in a Java bean class.

FIGURE 8.29   Example code for the Web deployment descriptor file.

using this deployment descriptor file, you can register Servlet and FacesServlet, register listeners and map resources to URLs. Figure 8.29 shows a piece of example code used in the web.xml file for the FacesServlet class.

Most code in this file will be created automatically if you are using the NetBeans IDE to build your Web application.

As we discussed in section 8.1.6.1, usually, JSP pages use the <jsp:useBean> tag to instantiate JavaBeans. When using the JSF framework, you do not have to specify the Java bean class names in your Web pages anymore. Instead, you can conFigure your bean instances in the application con-figuration resource file faces-config.xml using the <managed-bean> element. You may use multiple configuration files if you develop a large application. In that case, you must add a javax. faces.CONFIG _ FILES parameter in the deployment descriptor file, web.xml.

Now that we have worked through all the main techniques of JSF, let’s get a full picture of the complete run procedure of JSF Web applications.

Leave a Comment



                    Blog, Contribute ,Privacy,Terms of Use,Trademarks,© estherknow 2024