Primefaces commandButton操作不起作用 [英] Primefaces commandButton action doesn't work
问题描述
我有一个带有以下xhtml文件的Primefaces项目:
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>
这里我有3个commandButtons(p:commandButton)。一个在dataTable(迭代组件)内部,一个在dataTable外部,一个在h:form外部。
令人惊讶的是,唯一的一项工作是h:form之外的一项(最后一项)。我已经在互联网上搜索了5个小时,已经尝试了很多方法。我不明白为什么会这样。我阅读了许多有关类似问题的文章,但都没有解决我的问题。一个相当不错的是
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?
推荐答案
正如@Omar指出的那样,您忘记了该结束标记,即使我认为它在发布时也有误问题。 Appart对此进行了测试,它对我有用(Primefaces 3.5,Mojarra JSF 2.1.26)。我制作了这个 SSCCE ,它功能齐全(表单内部的按钮起作用,而不是外部的按钮)。
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>
这篇关于Primefaces commandButton操作不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!