JSP and JSF double swords combine to create perfect Web applications

Source: Internet
Author: User
Tags html form html tags implement interface return client java se
Js|web

Java has warmed up in recent years, and with the introduction of Java SE 5 and Java SE 6, the future of Java is even more glorious.

But Java-based JSP has not looked up until Java SE 5 was launched, the most important reason is that JSP although the function is very powerful, but the biggest advantage is its biggest drawback, powerful means complex, especially the design of the front-end interface of the visual tools are not much, nor strong enough. Therefore, the design of JSP pages becomes very complex and cumbersome. However, while Java SE 5 was launched, Sun introduced the new JavaServer Faces (JSF) specification to simplify the development of JSP. So that the JSP embarked on a broad road.

first, what is JSF

JSF and JSP are a couple of new partners. JSP is a technique for logic processing in the background, and JSF, on the contrary, is a presentation layer technology that enables developers to quickly develop a java-based Web application. Currently, JSF1.2 has officially joined Java EE 5 as a standard.

As a highly modular technology, developers can implement a drag-and-drop editing operation with the support of some development tools, and the user simply drags the JSF components onto the page, making it easy to develop the WEB. This is its greatest benefit as a modular technology, and the components we can use are not just some simpler input boxes, but also more complex components that can be used, such as table components such as a DataTable, tree-like trees, and so on.

As a standard technology, JSF has also been supported by a considerable number of tool providers. At the same time we will have a lot of good free development tools to use, not long ago, Sun Java Studio Creator 2 and Oracle jdeveloper 10g as a free JSF development tools to release, to bring a little anger to JSF. In addition, we also have some excellent business development tools to choose from, BEA Workshop (formerly M7 Nitrox), exadel,myeclipse, such as the Eclipse-based plug-in development tools, for the current vast number of Eclipse users have brought great convenience, IBM's The JBuilder of the Rational application Developer and Borland is also a good commercial development tool that supports JSF visualization development.

JSF and traditional Web technology are fundamentally different, in the traditional Web technology requires the user to capture the browser request, save the client state, and manually control the page's turn, and so on. The advent of JSF has undoubtedly brought us great convenience, and JSF provides an event-driven page navigation model that enables application developers to design the page flow of an application. In a manner similar to Struts, all page flow information is defined in the JSF configuration XML file (faces-config.xml) rather than hard-coded in the application. This largely simplifies developer development and simplifies application development.

JSF is also a framework that follows the model-view-controller (MVC) pattern. The complete separation of view and application logic (Model) enables applications that use JSF technology to achieve a good separation of the page from the code. All requests for JSF pages are processed by a front-end controller (Facesservlet), which automatically processes the user's request and returns the result to the user. This is not much different from the traditional MVC framework.

Not only is the POJO technology used in JSF, but also the control inversion (IoC) (or Dependency injection-di) technology similar to Spring is used, in the JSF backing Bean, we can put the data and operations required by the view into a backing bean. At the same time, thanks to the DI technology used by JSF, we can initialize the Managed Bean in the configuration file, and we can also use this technology to easily integrate with Spring with similar technology.

Ii. How to use JSF in JSP

JSF is only combined with JSP to give full play to its effectiveness. JSF is integrated through the tag library and JSP. The tag library is equivalent to the ASP.net server component. JSF provides a very rich library of tags that allow you to generate a variety of client models, such as HTML, WML, XML, and JavaScript.

With these tags, you can easily build large-scale client models and automatically process client requests by these tags.

Next, let's look at an example of how JSF and JSP work together. There are two libraries in JSF. The first is called the Kernel library, which contains a variety of key tags, such as configuring components, managing events, validating input information, and so on. The main function of the second library is to correspond the various tags of HTML and JSF. Each JSF label will correspond to an HTML component. such as the uiinput tag corresponds to the text box or password box in HTML.

The text input box in the JSF label is called Inputtext, and the password input library is called Inputsecret. Here is a simple JSF and JSP combination of user interface programs.

       
        
         
        <%@ taglib uri= "http://java.sun.com/jsf/html" prefix= "H"%><%@ taglib uri= "Http://java.sun.com/jsf/core" prefix= "F"% >   
          
          first JSF program    
          
           
            
            
              
            

In the code above, we can see how JSF is integrated with JSP. We can first see a kernel tag: view. Then there are several JSF components. such as form, Outputtext, Inputtext, and CommandButton. These components are placed in the form and are then opened as part of the form. At the beginning of the program, you must import two tag libraries using import. The code is as follows.

<%@ taglib uri= "http://java.sun.com/jsf/html" prefix= "H"%>

<%@ taglib uri= "Http://java.sun.com/jsf/core" prefix= "F"%>

The above 2 lines of code declare which JSF tag library to use in the JSP. The kernel library uses the prefix (prefix) f declaration, while the HTML library uses the prefix (prefix) H declaration. These two prefixes do not have to be used, but are only a suggestion. In a program, the kernel library must be used because view must be used in all JSF pages. While HTML tags convert JSF tags into HTML components at run time, this h prefix is not required, but is recommended by the JSF specification, so we make our JSF programs easier to read.

After the declaration is a few lines of standard HTML statements, this article is no longer detailed. Starting with , a JSF statement. This code looks like this:

       
        
        
          
          
          
             
           

The label indicates the beginning of JSF, and its next tab form creates an HTML form. The Outputtext label corresponds to the label component in HTML. The Inputtext label corresponds to the TextField component in HTML. The CommandButton tag corresponds to the Submit button in HTML.

third, JSP how to respond to the JSF request

From the example above we already know how to use JSF in JSP, and in this section let's look at how JSF handles requests.

First, let's take a look at an example that converts Fahrenheit to degrees Celsius. The program converts when the user clicks on the Submit button.

 
       
         <%@ taglib uri= "http://java.sun.com/jsf/html" prefix= "h"%><%@ taglib uri= "Http://java.sun.com/jsf/core" prefix= "F"%>   
         
         temperature converter    
         
          
          
     
div>

The first two lines of the program are the import of the JSF core library and the HTML library, which has been discussed previously and is no longer detailed here.

Let's look at how the JSF tags interact with the backend. Since we are using JSF in the JSP, this is no different from the normal JSP; The JSP is actually a servlet, When JSP is first run, the JSP compiler compiles the. jsp file into a servlet, which is called by the servlet, and then the servlet receives the data stream from the client. Unlike a typical JSP program, however, JSF tags are called by the JSF API (which separates the logical layer from the presentation layer), and they are no different from the normal JSP tags.

JSF uses these properties to set the value of the label when the uicomponent tag receives the doStartTag method. The inputtext tag in this example will be set according to its property value. The following is a code snippet for JSF.

The Inputtext label sets the ID and Value properties according to the corresponding values. In JSF, each property value is set by SetAttribute (String name, Object value). But we need to be aware that the JSF tags can specify the corresponding default values. This is similar to the system properties in Java, if you give a property name, the system will return the value of this property, if you specify its default value, and this property does not exist, it will return this default value.

Let's take a look at the most important part of the program above, the event handling of the Uiinput component.

In JSF, event handling is done by the valuechangelistener tag. This label represents an event that raises an event when the value of the text box changes. But the interesting thing is that this event is not submitted immediately, but until the user clicks the "Submit" button and the event is submitted to the backend, along with the corresponding data. Therefore, this event request is also called pre-submit. Finally, let's look at the Uicommand code implementation.

The code above connects the convert () method with the Uicommand, which means that the convert () method is executed when the "submit" button is clicked. After the view tag is encountered, the JSF program results, Jsfapi the last call to the Doend method to end the JSF program. After parsing this program, the JSF engine converts the corresponding JSF tags to HTML components.

Finally, let's look at how JSP responds to JSF events. The following is a section of Java code that responds to JSF events.

       
        
         
        public class Tcchangedlistener implements Valuechangelistener {public Tcchangedlistener () {super ();}//Event handling Public V OID Processvaluechange (Valuechangeevent event) throws Abortprocessingexception {uicomponent comp = event.getcomponent (  );  Object value = Event.getnewvalue ();   if (null!= value) {Float Curval = ((number) value). Floatvalue ();   Map values = Comp.getattributes ();   if (Curval 0) {values.put ("StyleClass", "Red");   else {values.put ("StyleClass", "Black"); }  }}
       
        

To respond to JSF events, you must implement the Valuechangelistener interface in the JSF library. The above program should note that the last set the corresponding color according to the value entered. These values do not depend on JSPs. Of course, you can also set them to NULL, and the JSP tags to set their color.



Related Article

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.