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
Code content
Code content <% @ 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
Code content
Code content ... <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
Code content
Code content ... <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: Code content
Code content <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:
Code content <% 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.