JSF 2.0,消息未显示在数据表内
我有一个带有数据表的表单,其中包含带有链接和输出文本的各个列。有一个输入字段是通过 ajax 请求进行评估的。自定义验证器可确保仅将整数添加到字段中。表格如下。
<form>
<h:dataTable var="item" value="#{listItems.model}" id="adminlistItems">
//other columns having commandLinks and outputTexts
<h:column>
<f:facet name="header" >
<h:outputText value="Quantity"/>
</f:facet>
<f:ajax listener="#{listItems.AddQuantityAction}">
<div style="padding:5px;float:left">
<h:inputText label="changeQuantity" id="addquantity" value="#{item.additionalQuantity}" maxlength="4" size="3">
<f:validator validatorId="integerValidator"/>
</h:inputText>
<h:outputText value=" "/>
<h:commandButton value="AddQuantity" />
<h:message for="addquantity"/>
</div>
</f:ajax>
</h:column>
</h:dataTable>
</h:form>
bean 的代码是:
@ViewScoped
@ManagedBean
public class ListItems implements Serializable {
//...
public String AddQuantityAction(){
//...
boolean result = //some action
FacesContext context=FacesContext.getCurrentInstance();
UIComponent component=UIComponent.getCurrentComponent(context);
String clientID=component.getClientId(context);
if (result) {
FacesMessage message = new FacesMessage("Quantity added successfully");
FacesContext.getCurrentInstance().addMessage(clientID, message);
} else {
FacesMessage message = new FacesMessage("Quantity not added.Processing error");
FacesContext.getCurrentInstance().addMessage(clientID, message);
}
return "adminListItems";
}
}
自定义验证器抛出一个不显示的验证器异常。侦听器也有未显示的消息代码。我读过几个类似的问题,这听起来也是一个常见的问题。但即使我错过了一些明显的东西,我也需要第三只眼睛来看到我没有看到的东西。
I have a form with a dataTable which has various columns having links and outputTexts. There is one input field which is evaluated through an ajax request . A custom validator makes sure that only integers are added to the field. The form is below.
<form>
<h:dataTable var="item" value="#{listItems.model}" id="adminlistItems">
//other columns having commandLinks and outputTexts
<h:column>
<f:facet name="header" >
<h:outputText value="Quantity"/>
</f:facet>
<f:ajax listener="#{listItems.AddQuantityAction}">
<div style="padding:5px;float:left">
<h:inputText label="changeQuantity" id="addquantity" value="#{item.additionalQuantity}" maxlength="4" size="3">
<f:validator validatorId="integerValidator"/>
</h:inputText>
<h:outputText value=" "/>
<h:commandButton value="AddQuantity" />
<h:message for="addquantity"/>
</div>
</f:ajax>
</h:column>
</h:dataTable>
</h:form>
The code for the bean is :
@ViewScoped
@ManagedBean
public class ListItems implements Serializable {
//...
public String AddQuantityAction(){
//...
boolean result = //some action
FacesContext context=FacesContext.getCurrentInstance();
UIComponent component=UIComponent.getCurrentComponent(context);
String clientID=component.getClientId(context);
if (result) {
FacesMessage message = new FacesMessage("Quantity added successfully");
FacesContext.getCurrentInstance().addMessage(clientID, message);
} else {
FacesMessage message = new FacesMessage("Quantity not added.Processing error");
FacesContext.getCurrentInstance().addMessage(clientID, message);
}
return "adminListItems";
}
}
The custom validator throws a validator exception which is not displayed. And the listener also has code for messages which too are not displayed. I have read several similar questions and this sounds a common question too. But even if i am missing something obvious,i am in need of a third eye to see what i dont.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
的execute
和render
默认为@this
。因此,只有当前活动的组件才会被处理和刷新。当您按下按钮时,不会发送输入值,也不会刷新消息组件。相应地修复它:
顺便说一下,为什么不直接使用内置的 javax.faces.Integer 转换器而不是验证器呢?
此外,ajax监听器方法的返回值应该是
void
。无论以何种方式,它都被完全忽略了。此外,方法名称应以小写字母开头。另请参阅 Java 命名约定。根据评论更新,这在验证方面似乎效果不佳。侦听器被调用 2 次,因为实际上发送了 2 个 ajax 请求,一个用于输入,一个用于命令。我建议将侦听器方法移至
。您只需将获取的客户端 ID 固定为输入 ID,而不是按钮 ID。
The
execute
andrender
of<f:ajax>
defaults to@this
. So only the currently active component will be processed and refreshed. When you press the button, this won't send the input value nor refresh the message component.Fix it accordingly:
By the way, why don't you just use the builtin
javax.faces.Integer
converter instead of that validator?Further, the return value of ajax listener methods should be
void
. It's totally ignored in any way. Also, method names should start with lowercase. See also Java naming conventions.Update as per the comment, that didn't seem to work out well with regard to validation. The listener is invoked 2 times because essentially 2 ajax requests are been sent, one for the input and one for the command. I suggest to move the listener method to the
<h:commandButton action>
.You'll only fix the obtained client ID to be the input ID, not the button ID.