Roles played by JSP in JSF
Learn how to use JSP with JSF
Summary
In this excerpt from assumerver Faces in Action (Manning, November 2004), the author Kito Mann explains how JSP works with JSF (1,800 words; December 13,200 4)
JSF applications require some display technology, such as JSP. One of the cool features of JSP is that it can be expanded with custom tags. A custom tag is a special XML Element supported by JAVA code. In addition to standard JSP elements or HTML elements, custom tags can also be used in JSP. Custom tags can do almost anything: display the value of a variable, process XML, display a part of the page conditionally, access a database, and so on (whether anyone should use JSP tags to do all these things, that is a future issue ...). their main purpose is to separate java code from the page and allow front-end developers to replace it with simple and familiar markup. A set of associated custom tags constitute a tag library. JSF and JSP use custom tags together. All the JSF tags we have used in this book-URI name prefix description
Http://java.sun.com/jsf/core Core f Contains tags that are independent of a particle render kit (like <f: view>, <validator>, and so on)
The http://java.sun.com/jsf/html HTML h Contains tags for all of the standard components and the HTML render kit
All the tags in these tag libraries must be named and implemented in a special way. This method ensures that JSP-based applications access different JSF implementations. Most ides support JSP. To a large extent, using JSF and JSP together is like using JSF to customize the tag library. However, there are some nuances that you should understand, such as using JSP Federation des.
Using JSP Federation des
One of the key features of JSP is that it can include the content of multiple JSP pages to a single JSP page. This feature is often used to execute interesting tasks such as including a title or footer. JSP supports two types of Federation des: Dynamic and Static. Dynamic inclusion (run with the <JSP: Include> tag or jstl <C: Import> tag) to access resources at runtime. In this case, the control goes to the included JSP page, and the response from the contained page is combined with the response from the contained page. When the content of a dynamic page is changed, new changes are automatically displayed. Static content inclusion is already combined with content that contains pages when compiled into Java code. Essentially, the content of these contained pages is copied to the contained pages. Changes to the content of the contained page generally do not automatically display new changes because they already have copies of their previous content. Only new content can be correctly displayed. (JSP 2.0 implicit nodes can be accessed through the web. xml configuration. The processing method is like static inclusion.) JSF uses two types of JSP nodes. for dynamic des, there are two necessary conditions:
The included page must be closed with the JSF core tag <F: subview>. <F: subview> the tag can be used inside the contained page. It can also be used to close the include statement. As shown below (here is the code added by the webmaster)
<F: View>
...
<F: subview id = "Header">
<C: Import url = "header. jsp"/>
</F: subview>
...
</F: view>
All template text and non-JSF tags on the included JSP page should be closed with the JSF core tag <f: verbatim>;
Therefore, if we have the following code in a JSP page:
<F: view>
...
<Jsp: include page = "foo. jsp"/>
...
</F: view>
Foo. jsp should be like the following:
<F: subview>
<H: outputText value = "heyah! "/>
...
<F: verbatim>
<B> Template text. </B>
<Customtag: dothis/>
</F: verbatim>
</F: subview>
As you can see, the entire included page is enclosed in a <f: subview> tag, and all the non-JSF tags and template text are marked <f: verbatim>. In this case, you can put the <f: subview> tag on the initial Page to close <jsp: include>. Static include is very simple and has no restrictions-you don't even need to use the <f: subview> flag. In the final example, we demonstrate a custom tag <customtag: dothis>, which executes some random tasks, which emphasizes the following: you can use other JSP custom tags with JSF.
Use JSTL and other JSP custom tags with JSF
All the JSF custom tag libraries mentioned here are good. But what if I have my own custom tag or a third-party tag library? Or will I use the JSP standard tag Library (JSTL )? It is a set of tag libraries that can do everything we just mentioned. To a great extent, these tags can be used in combination with JSF tags. Faces tags can be nested inside other tags, and vice versa. Some products, such as IBM WebSphere Application Developer, encourage this method. Others, such as Sun's Java Creator Studio, choose pure JSF tag. On the other hand, Oracle's JDeveloper allows you to mix and use it together, but also encourages the use of pure JSF tag.
Note: Whenever you nest a JSF tag in a non-JSF custom tag, you must assign a component identifier to the JSF tag. Because JSTL is standard and many people are familiar with it, we will use it to demonstrate how to use it with the JSF custom tag. (If You Want To fully understand JSTL, please read an excellent book written by Shawn Bayern, JSTL in Action .) let's start with a simple example (shown in Listing 1) to mix and use the JSTL tag and the JSF tag. The Code introduces two JSF tag libraries and the core JSTL tag library.
Listing 1. Mixed Use of JSTL tag and JSF tag
<% @ Taglib uri = "http://java.sun.com/jsf/core" prefix = "f" %>
<% @ Taglib uri = "http://java.sun.com/jsf/html" prefix = "h" %>
<% @ Taglib uri = "http://java.sun.com/jstl/core" prefix = "c" %>
<Html>
<Head>
<Title> JSF in Action: JSTL Example 1-Mixing JSF with other custom tags
</Title>
</Head>
<Body bgcolor = "# FFFFFF">
<F: view>
<H1>
<H: outputText value = "Example of using JSF tags with other custom tags"/>
</H1>
<P>
<B>
<C: out value = "Here's the value of your web. xml (don't do this at home):"/>
</B>
<Blockquote>
<F: verbatim>
<C: import url = "WEB-INF/web. xml"/>
</F: verbatim>
</Blockquote>
</P>
</F: view>
</Body>
</Html> </code>
In this example, the JSTL and JSF tags are nested in the JSF tag <f: view>. <f: view> defines the start of the JSF component tree. This example uses the JSF h tag (
Listing 2. JSF, JSTL tag, and the same backing bean
...
<F: view>
<Jsp: useBean class = "org. jia. examples. TestForm" id = "exampleBean" scope = "session"/>
<H1>
<H: outputText value = "Example of using JSF and JSTL expression versions"/>
</H1>
<H: form>
<H: outputLabel for = "inputInt">
<H: outputText value = "How many times do you want to repeat the Oracle's prophecy? "/>
</H: outputLabel>
<H: inputText id = "inputInt" value = "# {sessionScope. exampleBean. number}"/>
<H: commandButton value = "Go! "/>
<P>
<C: if test = "$ {sessionScope. exampleBean. number> 0}">
<C: forEach begin = "0" end = "$ {sessionScope. exampleBean. number-1}" var = "count">
Queen Tracey will achieve world domination. <br>
</C: forEach>
</C: if>
</P>
</H: form>
...
</F: view>
...
Warning if you use JSP or JSTL expressions to access managed beans, you must ensure that the beans have been created because these old expression languages do not know how to create Managed beans in JSF. This example defines a examan named exampleBean, which has a number attribute of the int type. Use the HtmlInputText component to update bean property values based on user input. When you click Go! Button (an HtmlCommandButton component), update the value of the number attribute and re-display the page. When all this happens, the JSTL <c: forEach> MARK repeatedly displays the text exampleBean. number Using the JSTL <c: out> mark. When the value of exampleBean. number is greater than 0, the <c: forEach> flag is executed, which is tested by JSTL <c: if>.
You cannot use the JSF component tag inside the loop tag of the stacked entity, such as the <c: forEach> tag of JSTL. The recommended method is to use the HtmlDataTable component or other components to stack a dataset or set. In this example, no JSF component is nested in the JSTL <c: if> tag. But what happens if the widget is displayed once and the page is hidden by the condition mark (for example, <c: if>) when the page is displayed again? When a component is displayed for the first time, the component is added to the view. if the <c: if> flag does not display the component for the second time, JSF deletes the component from the view. This means that any input control will lose their local values. You cannot reference these components any more. For example, see listing 3, which comes from the same page of Listing 2.
Listing 3. mark the condition with jstl to display the JSF component
...
<H: form>
<H: outputText value = "If you entered a number greater than 10,
Two input controls will display below. "/>
<P>
<C: if test = "$ {sessionScope. exampleBean. number> 10}">
<H: outputLabel id = "inputStringLabel" for = "inputString">
<H: outputText id = "outputStringLabel" value = "Enter in your string.
JSF will remember the value unless this control is hidden. "/>
</H: outputLabel>
<H: inputText id = "inputString"/>
<H: commandButton value = "Go! "/>
</C: if>
</P>
</H: form>
...
If the value of exampleBean. number is greater than 10, the JSTL <c: if> mark will execute its body. If the subject is executed, all nested components are added to the view and displayed. If it is not executed, the component will be deleted (if previously added). This is shown in Figure 1. If you use the JSTL condition tag (or other custom tag) to control the visibility of the component. If they are not displayed, the component is removed from the view. This means that components will forget their local values.
Figure 1. The JSTL <c: if> tag will execute its body if the value of exampleBean. number is greater than 10. Click on thumbnail to view full-sized image.
Figure 2 shows the output of Listing 2 and listing 3. The value of the input field (an HtmlInputText component) at the top is associated with exampleBean. number attribute, JSTL <c: forEach> uses it to display a string exampleBean. number of times, at the bottom of the page, if exampleBean. the value of number is greater than 10. The JSTL <c: if> MARK displays a table with JSF components. No, the component will not be displayed and removed from the view (the input control will lose its value ).
Figure 2. The output of the JSP page shown in Listings 2 and 3. Click on thumbnail to view full-sized image.
You can achieve the same effect of the Code in listing 3 by placing the component into an HtmlPanelGroup and setting its rendered attribute to the same expression. HtmlPanelGroup can be used as a container for multiple components. The following is an example:
<H: panelGroup rendered = "# {sessionScope. exampleBean. number> 10}">
<H: outputLabel id = "inputStringLabel2" for = "inputString">
<H: outputText id = "outputStringLabel2" value = "Enter in your string. JSF
Will remember the value. "/>
</H: outputLabel>
<H: inputText id = "inputString2"/>
<H: commandButton value = "Go! "/>
</H: panelGroup>
If exampleBean. number is greater than 10, this panel will become visible. In this case, the widget is not deleted if it is not displayed. This is a good example of using pure JSF instead of JSTL.
Tip: Even if you use JSTL to provide a custom tag with many features, if you develop (or redo) from scratch ), you should first check whether the standard JSF component can implement the behavior you want. Good components and well-designed backing beans can eliminate many JSTL tags on the page. With standard JSF, you can show or hide the Panel and do a variety of powerful functions. The following are several conditions that allow the JSF tag to work collaboratively with the JSTL internationalized and formatted Tag:
<Fmt: parseDate> and <fmt: parseNumber> are not recommended. You should use an HtmlInputText component with a date or digital converter.
<Fmt: requestEncoding> should not be used to mark the character encoding of the specified page. Generally, it is automatically processed by JSF. If you want to force a special character encoding, you should use the JSP page to indicate:
<% Page contentType = "[contenttype]; [charset]" %>.
You should not use the <fmt: setLocale> flag because it does not know that it may cause your JSTL flag to use one region while your JSF component uses another, to replace this disaster, you should use JSF's internationalization features. To control the location of a special page, use the locale attribute of the UIViewRoot component. JSF's internationalization features can work in JSF and JSTL.
The combination of JSF and JSTL can become very powerful. Here we demonstrate that your custom tag or tag obtained from a third party works with JSF and JSTL. Generally, when possible, you should try to use the JSF tag.