inputtext returns null 将 disabled/readonly 选项更新为 false 后

inputtext returns null after update the disabled/readonly option true to false

我有一个开关(布尔值)。当开关为真时,输入文本禁用选项为 true.When 开关为假,输入文本禁用选项为假,因此我可以输入 data.But 此数据 returns null.

    <h:form id="form">
                <p:inputSwitch value="#{switchMB.isTrue}" onLabel="True"
                    offLabel="False">
                    <p:ajax update="inputText" />
                </p:inputSwitch>

                <p:inputText id="inputText" value="#{switchMB.inputTextValue}"
                    disabled="#{switchMB.isTrue}"  />

                <p:commandButton action="#{switchMB.addValue}" 
                    update=":form" value="Add"></p:commandButton>
    </h:form>

当我在inputText中输入数据时,这个数据总是returns null.How我可以解决这个问题吗?如何更新 inputText 中的禁用选项并正确输入数据?在此先感谢..

作为防止 tampered/hacked 请求的一部分,disabled 属性在表单提交的应用请求值阶段 评估。所以基本上您需要确保 #{switchMB.isTrue} 的计算结果与呈现表单期间的计算结果完全相同。最简单的方法是将 #{switchMB} 托管 bean 放在视图范围内而不是请求范围内。

@Named // Or @ManagedBean
@ViewScoped // Watch out that you import from the right package!
public class SwitchMB {}

即,当您将其放入请求范围内时,所有属性都将重置为其默认值,在您的特定情况下,这显然会触发 disabled="true".

另请参阅:

  • commandButton/commandLink/ajax action/listener method not invoked or input value not updated (#5)
  • How to choose the right bean scope?