JSP and JSF merge to create a perfect web-tier application

Source: Internet
Author: User
Tags html form html tags java se advantage

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 page becomes very complex and cumbersome ...

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" %>
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=GB2312">
<title>第一个JSF程序</title>
</head>
<body>
<f:view>
<h:form id="simpleForm">
<h:outputText id="favoriteLabel" value="请输入一个数字:"/>
<h:inputText id="favoriteValue" value="#{simple.longValue}">
<f:validateLongrange maximum="30" minimum="0"/>
</h:inputText>
<p/>
<h:commandButton id="submit" value="提交" action="#{simple.simpleActionMethod}"/>
</h:form>
</f:view>
</body>
</html>

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 from F:view, it's a JSF statement. This code looks like this:

<f:view>
<h:form id="simpleForm">
<h:outputText id="favoriteLabel" value="请输入一个数字:"/>
<h:inputText id="favoriteValue" value="#{simple.longValue}">
<f:validateLongrange maximum="30" minimum="0"/>
</h:inputText>
<p/>
<h:commandButton id="submit" value="提交"
action="#{simple.simpleActionMethod}"/>
</h:form>
</f:view>

The/f:view tag 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.

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.