问题
I have the following custom PrimeFaces Panel
In the page
<x:myPanel panelName="TEST123" />
And the class
@FacesComponent(namespace="test",tagName="myPanel", createTag=true)
public class MyPanel extends Panel {
public MyPanel() {
panelName = (String)getAttributes().get("panelName");
}
}
Why when getting the attribute from the class itself does not work
getAttributes().get("panelName"); >> it returns null
I even tried to use the following as well, it also returns null at the class:
<f:attribute name="panelName" value="TEST123"/>
Thanks in advance
回答1:
Basically, behind the scenes, JSF creates the component and sets attributes as below:
MyPanel myPanel = new MyPanel();
myPanel.getAttributes().put("panelName", "TEST123");
It should speak for itself that it's not possible to set a component attribute before it's constructed. However, you're attempting to access it in the constructor!
You should access it in one of the standard UIComponent
methods where you actually need it. E.g. encodeBegin()
or so.
来源:https://stackoverflow.com/questions/30404268/cannot-get-custom-component-attribute-from-backing-bean