Forums Register Login

Modify components in backing bean

+Pie Number of slices to send: Send
My project requirement is to modify the background color on some JSF components depending on the user logged on.

I was trying to do this in a phase listener in the Render_Response phase. Loop thru the UIComponents and

UIViewRoot root = e.getFacesContext().getViewRoot();
UIComponent comp = root.getComponent("testform:inputfield");
comp.getAttributes().put("style","display:none");

Can something like this be done? If I click on the submit button on the page, when it renders again it works but not on the first time.
+Pie Number of slices to send: Send
You need UIViewRoot#findComponent() rather than UIViewRoot#getComponent().

And this should be done in the beforePhase of the render response.

Said that, I would rather just use the component's 'rendered' attribute to fulfill this requirement. Do you also know that you can just use EL in the styleClass attribute?
+Pie Number of slices to send: Send
OK, here is my code in the phase listener. In the Before phase there is no children to the UIViewRoot. The children components are not available until the LifeCycleImpl, so I tried this in the AfterPhase. The components are found but nothing happens. It seems the components are already rendered, and the response is complete so I cannot change them. What am I missing. by the way I took this code from another post on this site.

public class LifeCycleListener implements PhaseListener
{
public void beforePhase(PhaseEvent event)
{

System.out.println("Before " + event.getPhaseId());
if (event.getPhaseId() == this.getPhaseId())
{
FacesContext context = event.getFacesContext();

if (context != null && context.getViewRoot() != null)
{
setInputReadOnly(context.getViewRoot().getChildren());
}
}
}
public void afterPhase(PhaseEvent event)
{
System.out.println("After " + event.getPhaseId());
if (event.getPhaseId() == this.getPhaseId())
{
FacesContext context = event.getFacesContext();

if (context != null && context.getViewRoot() != null)
{
setInputReadOnly(context.getViewRoot().getChildren());
}
}
}
public PhaseId getPhaseId()
{
return PhaseId.RENDER_RESPONSE;
}
private void setInputReadOnly(List children)
{
for (Iterator child = children.iterator(); child.hasNext();)
{
UIComponent component = (UIComponent) child.next();
if (component == null)
{
continue;
}
System.out.println(component.getId());
if (component instanceof HtmlInputText)
{
((HtmlInputText) component).setReadonly(true);
}
if (component instanceof HtmlInputTextarea)
{

((HtmlInputTextarea) component).setReadonly(true);
}
if (component.getChildCount() > 0)
{
// Component has more children?
setInputReadOnly(component.getChildren());
// Loop through it's children.
}
}
}
}
+Pie Number of slices to send: Send
The afterPhase of RENDER_RESPONSE is been executed after the render response.

The component tree will indeed be null if it concerns a simple GET request.

But why do you want to do it the hard way instead of using the component's 'rendered' or 'styleClass' attributes?
+Pie Number of slices to send: Send
The reason why I am trying this is for field level security. Based on the user and or group we will need to hide/show, disable/enable, for almost all the fields on the page. This was just one idea to stream line the process so we did not fill our backing beans with all this code. The idea was to loop thru the components on the page, call a database (would be cached for session) and set the appropriate attribute for all the necessary fields.

Just an idea and I saw that based on several web pages that this can be done, but apparently it cannot be.
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 8561 times.
Similar Threads
Authorization over UIComponents with JSF 2.0
Issues with facelets and saveState?
Illegal use of 1PC datasource in Transaction
Making JSF compnents
Omit part of the view tree
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:59:04.