Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Modify components in backing bean

 
Curtis Vasbinder
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Curtis Vasbinder
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
}
}
}
}
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Curtis Vasbinder
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic