JSF tag, JSP tag and jstl tag

Source: Internet
Author: User

JSF integrates with JSP through custom tags. All the previously displayed JSF labels, such as

Table 3-6 JSF custom tag Library

Uri

Name

General prefix

Description

Http://java.sun.com/jsf/core

Core

F

Tags that are independent from specific Renderer (such as <F: View>, <validator>)

Http://java.sun.com/jsf/html

Html

H

Contains all standard components and HTML Rendering packages

All the tags in these libraries are named and implemented in a specific way. In this way, JSP-based applications can be transplanted between different JSF implementations. Most ides, including those mentioned in this book, can be used for JSP development.

Because JSP is the only display technology required by all implementations, we use it as an example in this book. It doesn't matter if you don't use JSP. Most of the concepts we talk about are not closely related to some display technology. You can read information about using other display technologies in Appendix.

For most of the problems, using JSF in JSP is only a problem of using JSF to customize the tag library. However, there are also some key points you need to know, such as JSP inclusion.

3.2.1 use JSP to include

One of the key features of JSP is that it can integrate content from multiple JSP pages into one page. This is usually used to include headers and footers. JSP supports two types: Dynamic and Static. Dynamic inclusion (implemented through the <JSP: Include> label or the jstl <C: Import> label) can access resources at runtime. In this case, the control is forwarded to the included JSP. The response returned from the contained JSP will be merged with the response sent back from the call page. After dynamic pages are modified, they are automatically updated on all call pages.

Static inclusion in JSP conversion-integrates resources when pages are converted to Java code and compiled. The content of the original page is basically copied to the calling page. Modification to the contained page cannot be automatically updated when the page is called. Because they already have their own content copies. They must be editable so that they can be re-compiled during content Update (implicit inclusion of JSP 2.0 can be implemented on the web. XML, and the processing is similar to static inclusion ).

JSF supports two types. There are two requirements for dynamic inclusion:

L The contained page must be encapsulated in the JSF <F: subview> core tag. The tag can be located on the contained page or around the containing statement;

L all template text and non-JSF labels on the page must be in the JSF <F: verbatim> core tag.

Therefore, we assume that the following JSP page code snippets exist:

Foo. jsp may be like this:

As you can see, the entire contained page is encapsulated in the <F: subview> label, and all the non-JSF labels and template text are encapsulated in the <F: verbatim> label. In addition, you can move the <F: subview> tag to the first page, outside the <JSP: Include> tag.

Static inclusion is simpler. There are no special restrictions-you don't even have to use the <F: subview> label.

In the above example, we show a hypothetical custom tag, <customtag: dothis>, which may execute any task. In this way, the emphasis is emphasized: JSF can be used with other JSP custom labels.

3.2.2 use JSF together with jstl and other JSP custom labels

These discussions about the JSF custom tag library are good, but what if you want to have your own custom tags or use third-party tags? What if you are using the JSP standard tag Library (jstl), which is a set of standard tags that can elegantly complete all the things just described? In most cases, you can mix them with your JSF tags. Faces labels can be nested in other labels. Some products, such as IBM WebSphere Application Developer [IBM, wsad], are encouraged to use this method. Other products, such as Sun's Java creator studio [sun, creator], the pure JSF method is encouraged. Oracle's jdeveloper [Oracle, jdeveloper] allows you to use it together, but also encourages you to use pure JSF.

Annotation specifies a component identifier for a JSF tag whenever it is nested into a non-JSF tag (see the previous chapter for more information about the component identifier ).

Because jstl is a standard and many people are familiar with it, we will use it to demonstrate how to use JSF with custom labels (if you want to obtain details of jstl, refer to a great book from Shawn Bayern.Jstl in action[Bayern]). Start with a simple example (as shown in code listing 3-2 ). This example uses a mix of jstl labels and JSF labels. This code imports both the JSF tag Library and the core jstl tag library.

Code List 3-2 mixed jstl labels and JSF labels

In this example, the jstl and JSF labels are nested in the JSF <F: View> label, which defines the start of the JSF component tree. The example uses the JSF htmloutputtext component (

Note: We nested the jstl tag into the JSF <F: verbatim> tag. Generally, it is easy to nest JSF tags into other tags. In fact, any component that can display its child components, such as htmldatatable and htmlpanelgrid, requires that the template text and nested labels be In the <F: verbatim> label (<F: verbatim> label is discussed in Chapter 4th ).

The biggest advantage of using jstl along with JSF is that the two use similar expression languages to reference objects (this is also true for JSP 2.0 expressions ). This allows you to easily share data between jstl and JSF tags in an intuitive way. To demonstrate this feature, let's look at another example. You can enter a value in the htmlinputtext control and use the value to repeatedly display the string using the jstl <C: foreach> label. The code is shown in code listing 3-3.

Code List 3-3 Use JSF and jstl labels for the same backend Bean

Warning If JSP or jstl expressions are used with managed beans, you need to ensure that the referenced bean has been created by using JSF expressions, Java code, or your own custom labels. This is because these old expression languages do not know the JSF managed bean creation tool (refer to Section 3.3 about creating managed beans ).

This code references a examan called examplebean, which has a number attribute of the int type. The htmlinputtext component is used to update bean attribute values based on user input. Click Go! When the button (htmlcommandbutton component), The number attribute is updated and the page is re-displayed. In this way, the jstl <C: foreach> label uses the <C: Out> label to repeatedly display the text examplebean. Number. <C: foreach> the label is executed only when examplebean. Number is greater than 0. This is controlled by the jstl <C: If> label.

You cannot use the JSF component tag in a tag that iterates its own body. For example, the JSF tag is used in the jstl <C: foreach> tag. The recommended method is to use the htmldatatable component or other components to iterate on a dataset or set.

In this example, no JSF component is nested in the jstl <C: If> label. However, what if a component is hidden by a condition tag such as <C: If> when it is displayed again on the page? When the component is displayed for the first time, it is added to the view. The second time, if the <C: If> label does not display this component, JSF will delete it from the view. This means that some input controls will lose their local values, and you can no longer reference these components (referenced by client identifiers or in code ). Let's look at another example, as shown in code listing 3-4, which comes from the same page in code listing 3-3.

When the jstl <C: If> label is greater than 10 in examplebean. Number, the body part of the label is executed. If the body part is executed, all nested components are added to the view and displayed. Otherwise, these components will be deleted (if they have already been added ). This situation is shown in 3-4. Figure 3-5 shows the output of the pages used in the code list 3-3 and 4-4.

Figure 3-4 assumes that you control the visibility of a component through the jstl condition tag (or other custom tag). If a component is not displayed, It is deleted from the view. This means that the component will also forget its local value

Code List 3-4 Use jstl to display JSF components with conditions

You can also place these components in htmlpanelgroup and set the rendered attribute to be the same expression to achieve the same effect as the code listing 3-4. Htmlpanelgroup is used as a container for multiple components. For example:

Figure 3-5 shows the JSP page output in code listing 3-3 and 3-4. The value of the input field (htmlinputtext component) at the top is associated with the examplebean. Number backend bean attribute, and this value is used by the jstl <C: foreach> label to repeatedly display the string examplebean. Number. At the bottom of the page, if examplebean. Number is greater than 10, the jstl <C: If> label displays a form using the JSF component. Otherwise, the widget is not displayed and deleted from the view (and the value of the input control is also lost)

If examplebean. Number is greater than 10, the panel becomes visible. If the component is not displayed, it will not be deleted. This is a good example of using pure JSF without using jstl.

It is prompted that many functions can be provided even if you customize tags, such as those provided by jstl, but if you are developing (or restructuring) from scratch ), you should first check whether the required functions can be implemented using the standard JSF component. Using excellent components and well-designed backend beans can avoid using too many jstl labels on pages. You can use JSF standard components to hide and display the entire panel, or complete various powerful tasks.

Below are several interoperability constraints for using JSF tags with jstl internationalization and formatting tags:

L The <FMT: parsedate> and <FMT: parsenumber> labels are not recommended. The htmlinputtext component (discussed in chapter 6th) with the datetime or number converter should be used );

L The <FMT: requestencoding> label used to determine or specify the character set encoding of the page should not be used. In general, JSF will automatically process this, and if special encoding needs to be forcibly specified, the JSP page command should be used: <% PAGE contenttype = "[Content-Type]; [charset] "%>;

L should not use the <FMT: setlocale> label. Because it does not support JSF, it may cause the jstl tag to use one site and the JSF component to use another site, which is a disaster. Instead, you should use JSF's internationalization features (discussed in chapter 6th ). To control the location of a specific page, use the locale attribute of the uiviewroot component, which will be discussed in chapter 4th. JSF's internationalization features support JSF and jstl.

The combination of JSF and jstl is very powerful. Custom tags developed by yourself or obtained from a third party should work well with the JSF and jstl labels described above. Generally, you should stick to the JSF tag as much as possible.

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.