【发布时间】:2013-10-15 08:48:10
【问题描述】:
我有以下页面/bean 结构(使用 myfaces 2.0.11 + tomcat 6)
我有一个复选框,当检查 h:inputtext(我的bean中的 Integer variable中连接到 h:inputtext)旁边是启用了他的旁边,并且当未选中复选框时,输入是禁用的,我有一个提交按钮将它们都提交(整个表单)
这里是代码
<h:form prependId="false">
<h:selectBooleanCheckbox id="my_len" value="#{myBean.myLenBool}">
<f:ajax render="my_len_input_wrapper"/>
</h:selectBooleanCheckbox>
<h:panelGroup id="my_len_input_wrapper">
<h:inputText value="#{myBean.myLen}" id="my_len_input"
disabled="#{not myBean.myLenBool}" required="#{myBean.myLenBool}">
<f:validateLongRange minimum="1"/>
</h:inputText>
<h:message for="my_len_input"/>
</h:panelGroup>
<h:commandButton action="#{myBean.submit}" value="submit">
<f:ajax render="@form" execute="@form"></f:ajax>
</h:commandButton>
</h:form>
豆码
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class myBean {
Integer myLen;
boolean myLenBool;
public Integer getMyLen() {
return myLen;
}
public void setMyLen(Integer myLen) {
this.myLen = myLen;
}
public boolean isMyLenBool() {
return myLenBool;
}
public void setMyLenBool(boolean myLenBool) {
this.myLenBool = myLenBool;
}
public void submit() {
// submit
}
}
场景如下
1) 选中复选框(将启用输入)
2)输入一个无效值(例如0.5),其无效原因myLen是Integer
3) 点击提交 -> 错误信息将显示在h:message 导致转换错误的原因
4)取消选中复选框(它将禁用输入文本)
5)点击提交
所以问题是:如何提交带有禁用输入且出现转换错误的表单???
到目前为止,我发现的唯一解决方案是编写我自己的自定义 converter,如果该字段被禁用,它将忽略转换
@FacesConverter("APCustomConverter")
public class APCustomConverter extends IntegerConverter{
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (component.getAttributes().get("disabled") != null && component.getAttributes().get("disabled").equals(true)) {
return null;
}
Object retValue = super.getAsObject(context, component, value);
return retValue;
}
}
希望有比我更好的解决方案(使用CustomConverter),
有点离题/不太离题:
这个转换错误最终导致我遇到了一个非常烦人的场景:当我使用只有 render="@form" 的丢弃按钮时,复选框的状态和输入错误,以至于在单击丢弃后 -> 复选框保持选中状态(虽然它不应该导致表单没有真正提交)并且输入被禁用而不是只读导致真正的复选框值为假,并且当我再次点击提交时,复选框被真正选中,但输入让自己为空值(所有这些都会导致服务器上出现空指针异常),所以最终我不得不在丢弃按钮中使用<f:actionListener type="org.omnifaces.eventlistener.ResetInputAjaxActionListener" /> by omnifaces。
【问题讨论】:
标签: jsf-2