[SOL]Problem updating a component and instantiating its bean

UI Components for JSF
Post Reply
x1sc0
Posts: 38
Joined: 21 Jun 2010, 10:55

29 Sep 2010, 17:11

Hello everybody,

basically what i'm trying is to update an image with regard to the node selected in a tree.it would work perfectly if I used the same backing bean for both components but I need to use different beans for each component. I've tried to intanciate the image's bean from the tree's bean but it doesn't work. I suppose the image component is updated before the images class is called so it doesn't have the data on time. Here you have the code to keep it clearer:

Code: Select all

<p:tree id="tree" value="#{TreeBean.root}" var="node"  
            selectionMode="single"
            ajax="false"
            selection="#{TreeBean.selectedNode}"  
            nodeSelectListener="#{TreeBean.onNodeSelect}"
            update="image">  
        <p:treeNode type="location" >  
            <h:outputText value="#{node}" />  
        </p:treeNode>
        <p:treeNode type="device">  
            <h:outputText value="#{node}" />  
        </p:treeNode>
    	</p:tree>

<h:graphicImage id="image" value="images/#{ImagesBean.currentImage.url}">
TreeBean

Code: Select all

    public void onNodeSelect(NodeSelectEvent event) { 
    	if(event.getTreeNode().getType().equals("location")){
    		Images aux = new Images();
    		aux.getImage(selectedNode);
    } 
ImageBean

Code: Select all

public void getImage(String selectedNode){
		Iterator iLocation = locationCol.iterator();
		while (iLocation.hasNext()){
		   	Location l = (Location)iLocation.next();
		   	if(selectedNode.equals(l.getId())){
		   		currentImage = l.getImage();	
		}
}
Last edited by x1sc0 on 29 Sep 2010, 23:13, edited 1 time in total.

callahan
Posts: 768
Joined: 27 May 2010, 22:52

29 Sep 2010, 21:06

Hi,

To access the ImegesBean used on the page, use smething similar to the following instead of "Images aux = new Images();"

Code: Select all

FacesContext context = FacesContext.getCurrentInstance();
Images aux = (Images) context.getApplication().getExpressionFactory()
    .createValueExpression(context.getELContext(), "#{ImagesBean}", Images.class)
        .getValue(context.getELContext());

x1sc0
Posts: 38
Joined: 21 Jun 2010, 10:55

29 Sep 2010, 23:15

It works like a charm. Thanks
btw, is there any tutorial where this stuff is explained ?

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 19 guests