0

我有一个带有以下 xhtml 文件的 Primefaces 项目:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
  <h:body>
     <ui:composition>       
        <h:form>
            <p:dataTable id="parameters" var="parameter" value="#{devTestController.parameters}" 
                editable="true" editMode="cell" widgetVar="parameterTable" 
                selection="#{devTestController.selectedRow}" selectionMode="single"
                rowKey="#{parameter}">  

                <f:facet name="header">
                    Parameters
                </f:facet>

                <p:column headerText="Parameter Name" style="width:30%">  
                    <p:cellEditor>  
                        <f:facet name="output"><h:outputText value="#{parameter.label}" /></f:facet>  
                        <f:facet name="input"><p:inputText id="parameterNameInput" value="#{parameter.label}" style="width:96%"/></f:facet>  
                    </p:cellEditor>  
                </p:column>  

                <p:column headerText="Parameter Value" style="width:60%">  
                    <p:cellEditor>  
                        <f:facet name="output"><h:outputText value="#{parameter.value}" /></f:facet>  
                        <f:facet name="input"><p:inputText value="#{parameter.value}" style="width:96%" label="Parameter Value"/></f:facet>
                    </p:cellEditor>  
                </p:column>

                <p:column headerText="Action" style="width:10%">
                    <p:commandButton value="Button in table" action="#{devTestController.doAction()}"/>  
                </p:column>
            </p:dataTable>  
            <p:commandButton value="Button outside table" action="#{devTestController.doAction()}"/>
        </h:form>
        <p:commandButton value="Button outside form" action="#{devTestController.doAction()}"/>
      </ui:composition>     
   </h:body>
</html>

这里我有 3 个命令按钮(p:commandButton)。一个在dataTable(迭代组件)内部,一个在dataTable 外部,一个在h:form 外部。令人惊讶的是,唯一有效的是 h:form 之外的那个(最后一个)。我已经在互联网上搜索了 5 个小时,并且已经尝试了很多东西。我不明白为什么会这样。我读了很多关于类似问题的帖子,但没有一个能解决我的问题。BalusC的一个相当不错. 我已经阅读了所有案例,但我找不到答案。首先,我怀疑我已经相互嵌套了多个 UIForm 组件(案例 2),我在包含上述内容的原始 xhtml 文件中进行了搜索,但事实并非如此。除了这个之外,我的代码中没有其他 h:form 标签。毕竟,如果我完全删除 h:form 标记,那么commandButton将不起作用,并且我还会收到“表单组件需要在其祖先中有一个 UIForm。建议:将必要的组件包含在 h:form 中”警告。这里有什么问题?

4

2 回答 2

2

正如@Omar 指出的那样,您忘记了那个结束标签,即使我认为发布问题时输入错误。从此,我自己进行了测试,它对我有用(Primefaces 3.5,Mojarra JSF 2.1.26)。我制作了这个SSCCE,它功能齐全(表单里面的按钮,而不是外面的按钮)。

@ManagedBean
@RequestScoped
public class DevTestController {

    public class Parameter {
        private String label;

        private String value;

        public Parameter(String label, String value) {
            this.label = label;
            this.value = value;
        }

        public String getLabel() {
            return label;
        }

        public String getValue() {
            return value;
        }

        public void setLabel(String label) {
            this.label = label;
        }

        public void setValue(String value) {
            this.value = value;
        }
    }

    private Parameter selectedRow;

    private List<Parameter> parameters = Arrays.asList(new Parameter("param1",
            "p1"), new Parameter("param2", "p2"));

    public DevTestController() {

    }

    public void doAction() {
        System.out.println("Done");
    }

    public List<Parameter> getParameters() {
        return parameters;
    }

    public Parameter getSelectedRow() {
        return selectedRow;
    }

    public void setSelectedRow(Parameter selectedRow) {
        this.selectedRow = selectedRow;
    }

}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:body>
        <h:form>
            <p:dataTable id="parameters" var="parameter"
                value="#{devTestController.parameters}" editable="true"
                editMode="cell" widgetVar="parameterTable"
                selection="#{devTestController.selectedRow}" selectionMode="single"
                rowKey="#{parameter}">

                <f:facet name="header">
                    Parameters
                </f:facet>

                <p:column headerText="Parameter Name" style="width:30%">
                    <p:cellEditor>
                        <f:facet name="output">
                            <h:outputText value="#{parameter.label}" />
                        </f:facet>
                        <f:facet name="input">
                            <p:inputText id="parameterNameInput" value="#{parameter.label}"
                                style="width:96%" />
                        </f:facet>
                    </p:cellEditor>
                </p:column>

                <p:column headerText="Parameter Value" style="width:60%">
                    <p:cellEditor>
                        <f:facet name="output">
                            <h:outputText value="#{parameter.value}" />
                        </f:facet>
                        <f:facet name="input">
                            <p:inputText value="#{parameter.value}" style="width:96%"
                                label="Parameter Value" />
                        </f:facet>
                    </p:cellEditor>
                </p:column>

                <p:column headerText="Action" style="width:10%">
                    <p:commandButton value="Button in table"
                        action="#{devTestController.doAction()}" />
                </p:column>
            </p:dataTable>
            <p:commandButton value="Button outside table"
                action="#{devTestController.doAction()}" />
        </h:form>
        <p:commandButton value="Button outside form"
            action="#{devTestController.doAction()}" />
</h:body>
</html>
于 2013-09-18T14:51:12.840 回答
0

您似乎缺少<ui:composition>.

于 2013-09-18T14:45:19.847 回答