【发布时间】:2016-04-27 10:44:13
【问题描述】:
我有h:selectBooleanCheckbox,但它总是将'false' 传递给validator.。
<h:panelGroup id="tncPanel">
<label>
<h:selectBooleanCheckbox id="tncSelection" value="#{businessBean.tncSelection}">
<f:validator validatorId="checkboxValidator"/>
<f:attribute name="label" value="Please agree terms and conditions."/>
</h:selectBooleanCheckbox>
I have read and agreed to all the
<a href="#" data-toggle="modal" data-target="#modal-term-and-conditions">
Terms and Conditions.
</a>
</label>
</h:panelGroup>
<h:panelGroup id="buttonPanel">
<h:commandLink id="nextButton" action="#{businessBean.goNext}">
Submit
</h:commandLink>
</h:panelGroup>
为什么我在这里有panelGroup,基于页面顶部的逻辑,我有一个逻辑来显示/不显示button 和checkbox
这是我的Validator。
@FacesValidator("checkboxValidator")
public class CheckboxValidator implements Validator {
private static final String DEFAULT_LABEL = "Please confirm the address.";
@Override
public void validate(FacesContext context, UIComponent component, Object value){
String checkBoxValue = String.valueOf(((HtmlSelectBooleanCheckbox) component).getSubmittedValue());
System.out.println("checkBoxValue " + checkBoxValue);
System.out.println("value2: " + value);
String label = String.valueOf(component.getAttributes().get("label"));
if ("null".equals(label)) {
label = DEFAULT_LABEL;
}
if("false".equalsIgnoreCase(checkBoxValue)){
FacesMessage msg = new FacesMessage(null, label);
msg.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(msg);
}
}
}
验证器sysout 总是打印checkBoxValue false
更新
在评论完 Balusc 之后,我再添加一个sysout 来直接打印参数value。但它仍然打印为value2: false。
【问题讨论】:
-
@BalusC 我不清楚你提到了什么。我是 JSF 的新手。
-
@BalusC 这是现有代码。不知道他们是怎么做到的。但我现在也尝试这样
HtmlSelectBooleanCheckbox checkbox = (HtmlSelectBooleanCheckbox) component;仍然是false -
您的代码完全合法。您是否有任何听众注册到您的项目任何
implments javax.faces.event.PhaseListener的类?这些侦听器可以在验证阶段之前运行并更改复选框值 -
同时使用 Chrome 检查器检查检查表单或在提交表单时切换到网络并检查请求表单数据,您必须看到类似
j_idt5:tncSelection:on // true case的内容,以确保将值正确发送到服务器 -
@YouYou 我需要提一下,这个提交按钮和复选框包含的面板会根据用户输入进行更新。这是作为 ajax 更新发生的。那么有什么与此更新有关的吗?更新后发生了什么?