aspose file tools*
The moose likes JSF and the fly likes Modify components in backing bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Modify components in backing bean" Watch "Modify components in backing bean" New topic
Author

Modify components in backing bean

Curtis Vasbinder
Greenhorn

Joined: Mar 26, 2008
Posts: 6
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

Joined: Oct 08, 2006
Posts: 2458
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

Joined: Mar 26, 2008
Posts: 6
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

Joined: Oct 08, 2006
Posts: 2458
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

Joined: Mar 26, 2008
Posts: 6
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Modify components in backing bean
 
Similar Threads
Making JSF compnents
Illegal use of 1PC datasource in Transaction
Omit part of the view tree
Authorization over UIComponents with JSF 2.0
Issues with facelets and saveState?