JSF 1.2: Can I create a reusable component inside a JSF view

Is something like this possible in jsf?

<ui:composition> <x:reusableCode id="editScreen">InnerHtml ... </x:reusableCode> code... <x:use component="editScreen"/> </ui:composition 

I know that I can create my own component and register it in jsf tagLib, but I need to reuse HTML only in jsf view file.

+6
source share
1 answer

In Facelets 1.x, you can create a tag file for this purpose.

Here is an example of a basic launch. Create /WEB-INF/tags/some.xhtml :

 <ui:composition xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets" > <h:outputText value="#{foo}" /> </ui:composition> 

Define it in /WEB-INF/my.taglib.xml :

 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE facelet-taglib PUBLIC "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN" "http://java.sun.com/dtd/facelet-taglib_1_0.dtd"> <facelet-taglib> <namespace>http://example.com/jsf/facelets</namespace> <tag> <tag-name>some</tag-name> <source>/WEB-INF/tags/some.xhtml</source> </tag> </facelet-taglib> 

Register it in /WEB-INF/web.xml :

 <context-param> <param-name>facelets.LIBRARIES</param-name> <param-value>/WEB-INF/my.taglib.xml</param-value> </context-param> 

(note that if you have several, use a semicolon ; to separate them)

Finally, just declare it in the homepage templates.

 <ui:composition xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:my="http://example.com/jsf/facelets" > <my:some foo="value1" /> <my:some foo="value2" /> <my:some foo="value3" /> </ui:composition> 

A more complex example can be found here: How to mesh a composite JSF component? Note: JSF 2.0 is targeted, but with minor changes based on the above example, it works fine on Facelets 1.x.

+4
source

Source: https://habr.com/ru/post/916630/


All Articles