javax.el.PropertyNotWritableException 在 UIInput 的值中使用 EL 条件运算符时

javax.el.PropertyNotWritableException when using EL conditional operator in value of UIInput

我收到此错误消息:

javax.el.PropertyNotWritableException: /u/editProfile.xhtml @64,140 value="#{empty userProfile.cfg.gpu or userProfile.cfg.gpu == '' ? usernavmsg.EditMe: userProfile.cfg.gpu}": null

问题是,当 bean 属性 的值为 null 时,inputText 字段的值从我的 ManagedBean 属性 切换到资源字符串。所以我不能坚持那个价值。

我在托管 bean 的数据库中请求用户的配置文件。如果所述配置文件的 属性 为空,则 inplace text holder 值为 "Edit Me"。如果它不为空,则它是 属性 的值。哪个有效!但是,当我提交表单并尝试保留一个新值或根本没有任何值时,会出现错误。该错误仅出现在开头为空的字段(当我从数据库中请求它们时)。所以问题是当值为 null 时,它从我的 ManagedBean 属性 切换到资源字符串。

我只有一个表格:

<h:outputLabel value="#{usernavmsg.GPU}: "/>
        <p:inplace styleClass="lessDark">
            <p:inputText value="#{empty userProfile.cfg.gpu ? usernavmsg.EditMe: userProfile.cfg.gpu}" />
        </p:inplace>

当我提交表单时,我想将 userProfile.cfg 保存在数据库中,但这并没有发生。

我可以解决这个问题吗?有可能吗?

EL中的条件语句确实是不可写的。它们是只读的。故事结局。这同样适用于 EL 中的方法引用,如 #{bean.property()}。它必须是真值表达式,如 #{bean.property}.

您使用 (HTML5) “占位符”的方法实际上是错误的。为此,您应该使用 placeholder 属性,而不是 value 属性。在没有 placeholder 属性的旧版本 PrimeFaces 中,您需要 passthrough。

<html ... xmlns:a="http://xmlns.jcp.org/jsf/passthrough">
...
<p:inputText value="#{userProfile.cfg.gpu}" a:placeholder="#{usernavmsg.EditMe}" ... />

现在 PrimeFaces 提供 placeholder 属性。

<p:inputText value="#{userProfile.cfg.gpu}" placeholder="#{usernavmsg.EditMe}" ... />