JSF and JSP are new partners

Source: Internet
Author: User

Java has gradually warmed up in recent years. With the introduction of Java SE 5 and Java SE 6, the future of Java is even more brilliant. However, Java-based JSP has been unable to lift the header before Java SE 5 was launched. The most important reason is that although JSP is very powerful, however, its biggest advantage is its biggest disadvantage. Its powerful functionality means complexity. In particular, there are not many visual tools for designing front-end interfaces, and they are not powerful enough. Therefore, designing JSP pages becomes very complicated and cumbersome...

Java has gradually warmed up in recent years. With the introduction of Java SE 5 and Java SE 6, the future of Java is even more brilliant. However, Java-based JSP has been unable to lift the header before Java SE 5 was launched. The most important reason is that although JSP is very powerful, however, its biggest advantage is its biggest disadvantage. Its powerful functionality means complexity. In particular, there are not many visual tools for designing front-end interfaces, and they are not powerful enough. Therefore, designing JSP pages becomes complicated and cumbersome. However, at the same time as Java SE 5 was launched, Sun introduced a new assumerver Faces (JSF) specification to simplify JSP development. Thus, JSP is on the road to prosperity.

As a highly componentized technology, developers can drag-and-drop edit operations with the support of some development tools. Users only need to drag the JSF component to the page, you can easily perform Web development. This is its biggest advantage as a componentized technology. The components we can use are not only simple input boxes, but also more complex components, such as table components such as able and Tree components.

As a standard technology, JSF has received support from many tool providers. At the same time, we will also have a lot of good free development tools available, not long ago Sun Java Studio Creator 2 and Oracle JDeveloper 10g were released as free development tools supporting JSF, it makes JSF quite angry. In addition, we also have some excellent commercial development tools to choose from, such as BEA Workshop (formerly M7 NitroX), Exadel, and MyEclipse plug-in development tools based on Eclipse, it brings great convenience to the vast majority of Eclipse users. IBM's Rational Application Developer and Borland's JBuilder are also good commercial development tools that support JSF visual development.

JSF is essentially different from traditional Web technologies. In traditional Web technologies, users need to capture browser requests, save the client status, and manually control page redirection. The emergence of JSF undoubtedly brings us great convenience. JSF provides an event-driven page navigation model that enables application developers to design the page stream of applications. Similarly to Struts, all page stream information is defined in the JSF configuration XML file (faces-config.xml), rather than hardcoded in the application. This greatly simplifies the development difficulty of developers and application development.

JSF is also a framework that follows the Model-View-controller (MVC) pattern. The View code and the application logic (Model) are completely separated, so that the applications using the JSF technology can well implement the separation of pages and code. All requests to the JSF page are processed by a front-end controller (FacesServlet). The system automatically processes the user's requests and returns the results to the user. This is not much different from the traditional MVC framework.

In JSF, not only the POJO technology is used, but also the IoC (or dependency injection-DI) technology similar to Spring is used. In the Backing Bean of JSF, 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 easily integrate with Spring using similar technologies through this technology.

How to Use JSF in JSP

Only by combining JSF and JSP can we make full use of its functions. JSF is integrated with JSP through the tag library. The tag library is equivalent to the server component of ASP. NET. JSF provides rich tag libraries that can generate various client models, such as HTML, WML, XML, and JavaScript. With these tags, you can easily create a large-scale client model and use these tags to automatically process client requests.

Next let's look at an example of how to make JSF and JSP work together. There are two libraries in JSF. The first is the kernel library, which contains various major labels, such as configuration components, management events, and verification input information. The main function of the second library is to correspond HTML and JSF tags. Each JSF tag corresponds to an HTML component. For example, the UIInput tag corresponds to the text box or password box in HTML.

In the JSF tag, the text input box is called inputText, And the password input library is called inputSecret. The following is a simple user interface program that combines JSF and JSP.

  1. <% @ TaglibUri=Http://java.sun.com/jsf/html" Prefix="H"%>
  2. <% @ TaglibUri=Http://java.sun.com/jsf/core" Prefix="F"%>
  3. <Html>
  4. <Head>
  5. <MetaHTTP-EQUIV="Content-Type" CONTENT="Text/html; CHARSET = GB2312">
  6. <Title> first JSF Program </title>
  7. </Head>
  8. <Body>
  9. <F: view>
  10. <H: formId="SimpleForm">
  11. <H: outputTextId="FavoriteLabel" Value="Enter a number :"/>
  12. <H: inputTextId="FavoriteValue" Value="# {Simple. longValue }">
  13. <F: validateLongrangeMaximum="30" Minimum="0"/>
  14. </H: inputText>
  15. <P/>
  16. <H: commandButtonId="Submit" Value="Submit" Action="# {Simple. simpleActionMethod }"/>
  17. </H: form>
  18. </F: view>
  19. </Body>
  20. </Html>

In the above code, we can see how JSF is integrated with JSP. First, we can see a kernel label: view. Then there are several JSF components. Such as form, outputText, inputText, and commandButton. These components are put into the form to form a part. At the beginning of the program, you must use import to import two tag libraries. The Code is as follows.

  1. <% @ TaglibUri=Http://java.sun.com/jsf/html" Prefix="H"%>
  2. <% @ TaglibUri=Http://java.sun.com/jsf/core" Prefix="F"%>

The above two lines of code declare the JSF tag library to be used in JSP. The kernel library uses prefix) f declaration, while the HTML library uses prefix h declaration. These two prefixes are not mandatory, but only a suggestion. In the 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 runtime. This h prefix is not mandatory, but is recommended by the JSF specifications. In this way, our JSF program is easier to read.

There are several standard HTML statements after declaration, which will not be detailed in this article. Starting from f: view, it is a JSF statement. The Code is as follows:

  1. <F: view>
  2. <H: formId="SimpleForm">
  3. <H: outputTextId="FavoriteLabel" Value="Enter a number :"/>
  4. <H: inputTextId="FavoriteValue" Value="# {Simple. longValue }">
  5. <F: validateLongrangeMaximum="30" Minimum="0"/>
  6. </H: inputText>
  7. <P/>
  8. <H: commandButtonId="Submit" Value="Submit"
  9. Action="# {Simple. simpleActionMethod }"/>
  10. </H: form>
  11. </F: view>

/F: The view tag indicates the beginning of JSF, and its next tag form creates an HTML Form. The outputText label is equivalent to the label component in HTML. The inputText tag is equivalent to the textField component in HTML. The commandButton tag is equivalent to the submit button in HTML.

  1. Ajax events and errors in JSF2
  2. JSF program Configuration
  3. Status Quo and Development of Java Development sanjiasek JSF2.0, EJB3.1, and JPA2.0
  4. Integrate FCKEditor in JSF/JSP
  5. Learning from the JSF + Seam framework

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.