Accessing the attributes of the composite JSF2 component in the backing component

I am developing a JSF2 / Primefaces application and am having problems accessing the attributes defined in the component interface in this bean component support.

My component is defined as follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:composite="http://java.sun.com/jsf/composite"> <composite:interface componentType="testComponent"> <composite:attribute name="text" required="true" type="java.lang.String" /> </composite:interface> <composite:implementation> <h:outputText value="Passed text is: #{cc.attrs.text}" /> </composite:implementation> </html> 

It is stored in a file called text.xhtml located in the application/src/main/webapp/resources/my_component .

I use this component on another page (which is an element of the Facelets composition) as follows:

 <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:myc="http://java.sun.com/jsf/composite/my_component" template="./resources/templates/template.xhtml"> <ui:define name="content"> <h:form id="products"> <myc:test id="test" text="A text" /> </h:form> </ui:define> </ui:composition> 

The support component class is defined as follows:

 package my.application.component; import javax.faces.component.FacesComponent; import javax.faces.component.UINamingContainer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @FacesComponent ( value="testComponent" ) public class TestComponent extends UINamingContainer { Logger log = LoggerFactory.getLogger(TestComponent.class); public TestComponent() { log.debug("TestComponent constructor"); log.debug("getAttributes().size(): " + getAttributes().size()); } } 

The component itself works as expected. The usage page displays the output of Passed text is: A text . In addition, the log output outputs the log messages from the TestComponent constructor, so it seems that the definition of the xml component is correctly associated with the TestComponent class.

Now the problem The method getAttributes() , called in the TestComponent constructor, always returns a map of zero size. If I understand this correctly, I must have access to the text attribute declared in the component interface with a call:

getAttributes().get("text");

in the TestComponent class, but it always returns null since the attribute map is empty.

I also tried to access the text attribute using a call:

String text = FacesContext.getCurrentInstance().getApplication(). evaluateExpressionGet(FacesContext.getCurrentInstance(), "#{cc.attrs.text}", String.class));

but it also solves null .

What am I doing wrong? Any advice would be greatly appreciated as I have no idea what to do next.

/ Tukasz.

+6
source share
1 answer

I assume that the constructor is too early to refer to these attributes.

JSF will first build an instance of the support component, and then at some point provide a link to its attributes. In a method called later, for example, in code, you must have access to them.

+5
source

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


All Articles