【发布时间】:2014-03-21 00:27:20
【问题描述】:
在以下情况下,它永远不会调用侦听器方法。
TestBean.java
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
@ViewScoped
@ManagedBean(name="testBean")
public class TestBean implements Serializable {
private int score1;
private int score2;
private int score3;
public void resetPage(){
score1 = 35;
score2 = 35;
score3 = 35;
}
public int getScore1() {
return score1;
}
public void setScore1(int score1) {
this.score1 = score1;
}
public int getScore2() {
return score2;
}
public void setScore2(int score2) {
this.score2 = score2;
}
public int getScore3() {
return score3;
}
public void setScore3(int score3) {
this.score3 = score3;
}
}
requirements.xhtml
<h:form id="requirementsForm" prependId="false">
<h:messages style="color: #FF0000; font-size: 13px; margin: 10px; white-space: nowrap;" />
<table>
<tr>
<td> Score 1: </td>
<td>
<h:inputText value="#{testBean.score1}" required="true" />
</td>
</tr>
<tr>
<td> Score 2: </td>
<td>
<h:inputText value="#{testBean.score2}" required="true" />
</td>
</tr>
<tr>
<td> Score 3: </td>
<td>
<h:inputText value="#{testBean.score3}" required="true" />
</td>
</tr>
<tr>
<td colspan="2">
<h:commandLink style="margin-left: 1px;" styleClass="submitLink">
<img src="#{request.contextPath}/circuit_images/refresh.png" />
<f:ajax execute="@this"
render="@form"
listener="#{testBean.resetPage}"/>
</h:commandLink>
</td>
</tr>
</table>
</h:form>
我知道执行的默认值是@this, 我删除了它并尝试了同样的问题。 它从不调用 testBean#resetPage() 方法。
<f:ajax execute="@this"
render="@form"
listener="#{testBean.resetPage}"/>
当我更新 execute="@form" 时,它可以工作。
<f:ajax execute="@form"
render="@form"
listener="#{testBean.resetPage}"/>
表单有一些带有必需和其他验证的输入字段。 但是对于重置按钮,我不希望处理这些验证。
我在<h:commandLink> & <f:ajax> 上都添加了 immediate="true",但没有成功。
我正在使用 JSF 2.2 和 Tomcat 6.0.35。
我也提到了 http://www.primefaces.org/showcase/ui/resetInput.jsf & http://omnifaces.org/docs/javadoc/current/org/omnifaces/eventlistener/ResetInputAjaxActionListener.html 我无法解决这个问题。
【问题讨论】:
-
你为什么在表单中使用
prependId="false"? -
为了避免表单 id 作为每个字段 id 的前缀,同时也为了避免所有元素的 id 过长,当我需要在 javascript 或 JQuery 中使用它们时,每次我需要转义默认的 prepender '\\:' .
-
你可以试试这个 button.execute="@form" 和 immediate="true"。
-
对于按钮或链接,实际上如果我将 execute="@form" 与 immediate=true 一起使用,它正在调用该方法,但如果我提交带有空值的表单,我仍然会看到空字段验证方法调用后的错误,尽管方法用一些默认值更新了它们。