【问题标题】:Data in <h:inputText readonly="true"> disappears when command button is clicked单击命令按钮时 <h:inputText readonly="true"> 中的数据消失
【发布时间】:2012-04-30 12:51:19
【问题描述】:

我正在使用 JSF 1.1。我有一个带有请求范围 bean 和只读输入字段的 JSF 页面。

<h:inputText id="dt" value="#{bean.sdate}" readonly="#{bean.disable}" />
<a onclick="cal('dt');"><img src="fr.gif" border="0"></a>

当我使用 JavaScript 设置输入值并单击命令按钮时,输入字段中的数据会消失。

这是怎么引起的,我该如何解决。

【问题讨论】:

  • 我知道有些回答 SO 问题的人可能被认为是魔术师,但他们没有代码示例什么也做不了。
  • 我真的不知道cal(some String variable)方法会发生什么...
  • 如果您的 readonly 设置为 true,#{bean.sdate} 的值将不会发送到服务器......因此会丢失......
  • @Daniel:你描述的是disabled,而不是readonly
  • @BalusC 它也发生在 readonly=true :)

标签: jsf


【解决方案1】:

这是因为属性设置为readonly。如果这评估了true,那么 JSF 将不会处理提交的值,因此模型不会被更新。如果您想在渲染视图时将其设置为readonly并且让 JSF 处理提交的值,那么您需要使其仅在渲染响应阶段评估 true。您可以为此使用FacesContext#getRenderResponse()。您需要在 isDisable() 方法中执行此操作。

public boolean isDisable() { // TODO: rename to isReadonly().
    return FacesContext.getCurrentInstance().getRenderResponse();
}

注意:在 JSF2 中,您也可以在视图中通过 #{facesContext} 访问 FacesContext#getCurrentInstance(),这样可以节省模型中的一些样板:

<h:inputText ... readonly="#{facesContext.renderResponse}" />

还请注意,当您使用 JSF2 &lt;f:viewParam&gt; 时,这种方法将不再适用于 GET 请求。有关说明和解决方法,另请参阅 Make a p:calendar readonly

【讨论】:

  • 同时我想知道是否假设我想在“过程验证”等任何其他阶段更新模型,那么我该如何像你在这里所做的那样做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多