p:未在动态页面中调用commandButton操作

时间:2013-08-02 15:16:09

标签: primefaces actionlistener commandbutton

我的应用程序具有左侧和中间布局单元的布局(demoLayout.xhtml)。在主页面(main.xhtml)上,我在左侧布局单元(demoTree.xhtml)上有p:tree,在中心布局单元上有三个不同的表单(first.xhtml,second.xhtml,third.xhtml)。中心表单使用树节点单击进行切换。我的默认中心表单是first.xhtml,当我没有把第二个xhtml上的first.xhtml命令按钮上的p:commandButton和第三个.html动作没有被调用时。当我在first.xhtml上放置p:commandButton时,其他命令按钮可以正常工作,但我不想把p:commandButton放在first.xhtml上。我该怎么办?

demoLayout.xhtml

    <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:p="http://primefaces.org/ui">

    <f:view id="mainPanel" encoding="UTF-8" contentType="text/html">

        <h:head>
            <f:facet name="first">
                <meta http-equiv="X-UA-Compatible" content="EmulateIE8" />
                <meta content='text/html; charset=UTF-8' http-equiv="Content-Type" />
                <title>#{title}</title>
            </f:facet>

        </h:head>

        <h:body>

            <p:layout fullPage="true">

                <p:layoutUnit id="left" position="west" size="300" resizable="true" closable="true" collapsible="true" header="Quick Links" visible="true" minSize="200">
                    <div id="west">
                        <ui:insert name="west">
                            Default West Content
                        </ui:insert>
                    </div>
                </p:layoutUnit>

                <p:layoutUnit id="center" position="center">
                    <div id="centerDiv">
                        <ui:insert name="center">
                            Default Center Content
                        </ui:insert>
                    </div>
                </p:layoutUnit>

            </p:layout>

        </h:body>
    </f:view>

    </html>

main.xhtml

<ui:composition template="demoLayout.xhtml"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:p="http://primefaces.org/ui">

        <ui:param name="title" value="demo" />

    <ui:define name="west">
        <ui:include src="demoTree.xhtml" />
    </ui:define>

    <ui:define name="center">
        <ui:include src="#{demo3MBean.activePanel}" />
    </ui:define>

</ui:composition>

demoTree.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

    <h:form id="treeForm">

    <p:growl id="messages" showDetail="true" sticky="false" />

        <p:tree value="#{demoTreeBean.root}" var="node" id="tree" animate="true"    style="width:350px;height:720px" dynamic="true" cache="false"
        selectionMode="single">

            <p:treeNode type="First">
                <h:outputText value="#{node}" id="lblNode1" />
            </p:treeNode>
            <p:treeNode type="Second">
                <h:outputText value="#{node}" id="lblNode2" />
            </p:treeNode>
            <p:treeNode type="Third">
               <h:outputText value="#{node}" id="lblNode3" />
            </p:treeNode>
            <p:ajax event="select" update=":rightForm" listener="#{demo3MBean.onNodeSelect}" />
        </p:tree>

        <p:blockUI block=":center" trigger="tree">  
        LOADING<br />
            <p:graphicImage value="/images/ajax-loader.gif" />
        </p:blockUI>

    </h:form>

 </ui:composition>

first.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:p="http://primefaces.org/ui">

    <h:form id="rightForm">

    <p:growl id="messages" showDetail="true" sticky="false" />

        <br></br>

        <p:fieldset id="resourceList" legend="1 nolu grup">

            <h:outputText value="1 Nolu XHTML" />
        <br />
            <ui:remove> 
            <p:commandButton id="buton" value="Print Me 1" actionListener="#{demo3MBean.printMe1}" />
            </ui:remove>
        </p:fieldset>

    </h:form>

</ui:composition> 

second.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:p="http://primefaces.org/ui">

    <h:form id="rightForm">

        <p:growl id="messages" showDetail="true" sticky="false" />

        <br></br>

        <p:fieldset id="resourceList" legend="2 nolu grup">

            <h:outputText value="2 Nolu XHTML" />
            <br />
            <p:commandButton id="buton" value="Print Me 2" actionListener="#{demo3MBean.printMe2}" />

        </p:fieldset>

    </h:form>

</ui:composition>

third.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:p="http://primefaces.org/ui">

    <h:form id="rightForm">

        <p:growl id="messages" showDetail="true" sticky="false" />

        <br></br>

        <p:fieldset id="resourceList" legend="3 nolu grup">

            <h:outputText value="3 Nolu XHTML" />
            <br />
            <p:commandButton id="buton" value="Print Me 3" actionListener="#{demo3MBean.printMe3}" />

        </p:fieldset>

    </h:form>

</ui:composition>

Demo3MBean.java

@ManagedBean(name = "demo3MBean")
@ViewScoped
public class Demo3MBean extends TlosSWBaseBean implements Serializable {

    private static final long serialVersionUID = -504537811128309503L;

    private String activePanel = FIRST_PANEL;

    public final static String FIRST_PANEL = "first.xhtml";
    public final static String SECOND_PANEL = "second.xhtml";
    public final static String THIRD_PANEL = "third.xhtml";

    public void onNodeSelect(NodeSelectEvent event) {

        String nodeType = event.getTreeNode().getType();
        if (nodeType.equals("First")) {

            activePanel = FIRST_PANEL;

        } else if (nodeType.equals("Second")) {

            activePanel = SECOND_PANEL;

        } else if (nodeType.equals("Third")) {

            activePanel = THIRD_PANEL;
        }

    }

    public void printMe1(ActionEvent e) {
        System.out.println("Me 1");
    }

    public void printMe2(ActionEvent e) {
        System.out.println("Me 2");
    }

    public void printMe3(ActionEvent e) {
        System.out.println("Me 3");
    }

    public String getActivePanel() {
        return activePanel;
    }

    public void setActivePanel(String activePanel) {
        this.activePanel = activePanel;
    }

}

DemoTreeBean.java

@ManagedBean(name = "demoTreeBean")
public class DemoTreeBean {

    private TreeNode root;

    @SuppressWarnings("unused")
    public DemoTreeBean() {
        root = new DefaultTreeNode("Root", null);
        TreeNode node0 = new DefaultTreeNode("First", "First Node", root);
        TreeNode node1 = new DefaultTreeNode("Second", "Second Node", root);
        TreeNode node2 = new DefaultTreeNode("Third", "Third Node", root);

    }

    public TreeNode getRoot() {
        return root;
    }
}

我也读过这些:

3 个答案:

答案 0 :(得分:1)

main.xhtml中的这一部分是导致问题的原因。

<ui:define name="center">
    <ui:include src="#{demo3MBean.activePanel}" />
</ui:define>

在运行时更改src的值不起作用。

尝试使用静态值,如

<ui:define name="center">
    <ui:include src="third.xhtml" />
</ui:define>

你会看到按钮有效。

答案 1 :(得分:1)

在运行时更改 src 的值将无效。

<ui:define name="center">
    <ui:include src="#{demo3MBean.activePanel}" />
</ui:define>

解决问题的替代解决方案是为您的xhtml网页使用呈现标记。例如

<ui:define name="center">

<s:div rendered="#{demo3MBean.firstFlag}">
    <ui:include src="first.xhtml" />
</s:div>

<s:div rendered="#{demo3MBean.secondFlag}">
    <ui:include src="second.xhtml" />
</s:div>

<s:div rendered="#{demo3MBean.thirdFlag}">
    <ui:include src="third.xhtml.xhtml" />
</s:div>

</ui:define>

将所有页面标记设置为FALSE。仅在单击相关树节点时才将特定标志值设置为TRUE。不要忘记设置所包含页面的所有组件的唯一“id”

使用上面的解决方案,诸如commandButtons,commandLinks等组件的action和actionListener等属性也可以正常且完美地工作。

希望这能解决您的问题。 如果它有帮助,也不要忘记接受我的答案。祝你今天愉快!!

答案 2 :(得分:0)

通过更新所包含文件的内部,只能包含不同的文件。由于每个文件中都有rightForm形式,因此在选择树节点时,只更新当前加载的文件的rightForm。所以永远不会加载不同的文件 此外,当每个文件中存在相同的表单时,这是一种不好的方法(类似于growl)。

要解决此问题,请移除h:formfirst.xhtmlsecond.xhtml中的third.xhtml,然后将ui:define中的第二个main.xhtml更改为以下内容:

<ui:define name="center">
    <h:form id="rightForm">
        <ui:include src="#{demo3MBean.activePanel}" />
    </h:form>
</ui:define>

为什么要在p:treeNode中定义三个p:tree?在初始化DemoTreeBean中的树时,无需手动定义treenodes。正如您在showcase中看到的那样,p:tree只是迭代给定值的节点。尝试以下代码以减少添加更多树节点时的工作量:

<p:tree value="#{demoTreeBean.root}" var="node" id="tree" animate="true" 
  style="width:350px;height:720px" dynamic="true" cache="false"
  selectionMode="single">
    <p:treeNode type="#{node.data}">
        <h:outputText value="#{node}" id="lblNode1" />
    </p:treeNode>
    <p:ajax event="select" update=":rightForm" listener="#{demo3MBean.onNodeSelect}" />
</p:tree>