问题
I have a Primefaces project with the following xhtml file:
<!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>
Here I have 3 commandButtons (p:commandButton). One is inside the dataTable (iterating component), one is outside the dataTable and one is outside h:form. Surprisingly, the only one working is the one outside the h:form (the last one). I've been searching the internet for 5 hours and already tried many things. I don't understand why this is happening. I read many posts regarding similar problems, but none of them solved my case. A pretty good one is this by BalusC. I have read all the cases and I can't find an answer. First I suspected that I have nested multiple UIForm components in each other (case 2) and I searched in the original xhtml file that includes the one above, but this is not the case. I have no other h:form tags in my code except this one. After all, if I completely remove the h:form tag, then no commandButton works and I also get the "The form component needs to have a UIForm in its ancestry. Suggestion: enclose the necessary components within h:form" warning. What could go wrong here?
回答1:
As @Omar points, you forgot that closing tag, even I suppose it's a mistyping when posting the question. Appart from that, I've tested by my own and it works for me (Primefaces 3.5, Mojarra JSF 2.1.26). I made this SSCCE, which is fully functional (buttons inside form work, not the one outside).
@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>
回答2:
It seems that you are missing a closing tag for <ui:composition>
.
来源:https://stackoverflow.com/questions/18873970/primefaces-commandbutton-action-doesnt-work