如何将对象从primefaces上的树传递给视图

时间:2012-09-30 17:05:37

标签: java ajax jsf primefaces

我开发了一个带有primefaces树组件的导航菜单。树节点有一个Project对象作为数据。我希望能够加载一个包含项目详细信息的视图,当我单击每个项目时,该视图会动态加载。

为此,我在树上定义了一个ajax事件选择:

<h:form>
    <p:tree value="#{treeBean.root}" var="node"
                    selection="#{treeBean.selectedNode}" id="tree" >
                    <p:ajax event="select" listener="#treeBean.onNodeSelect}"/>
        <p:treeNode styleClass="treeNode">
        <h:outputText value="#{node}" />
        </p:treeNode>
    </p:tree>
</h:form>

在treeManagedBean中调用onNodeSelectMethod:

@ViewScoped
@Component("treeBean")
@ManagedBean
public class TreeManagedBean{

private Project selectedProject;

public Project getSelectedProject(){
    return selectedProject;
}

public void setSelectedProject(Project project){
    this.selectedProject = project;
}

    public void onNodeSelect(NodeSelectEvent event) {
            this.selectedProject = (Project)event.getTreeNode().getData();
            FacesContext context = FacesContext.getCurrentInstance();
            ConfigurableNavigationHandler handler = (ConfigurableNavigationHandler)             context.getApplication().getNavigationHandler();
            handler.performNavigation("viewProject");
        }

然后我通过faces-config.xml加载viewProject.xhtml,并在其他managedBean中填充该属性:

@Component("projectView")
@ViewScoped
@ManagedBean
public class ProjectView {
    @ManagedProperty("#{treeBean.selectedProject}")
    private Project project; 
}

视图加载时没有任何错误,但永远不会设置该属性。 ajax调用有效,并在第一个bean中设置属性。这是这样做的吗?

1 个答案:

答案 0 :(得分:0)

我解决了它只是为managedBeans添加了一个标识符,因为我不得不提供一个默认的构造函数:

@ViewScoped
@Component("treeBean")
@ManagedBean("treeBean")
public class TreeManagedBean{ 
   public TreeManagedBean(){}
}

@Component("projectView")
@ViewScoped
@ManagedBean("projectView")
public class ProjectView {
    @ManagedProperty("#{treeBean.selectedProject}")
    private Project project; 
    public ProjectView(){}
}