< h:inputText readonly ="true">中的数据单击命令按钮时消失 [英] Data in <h:inputText readonly="true"> disappears when command button is clicked

查看:101
本文介绍了< h:inputText readonly ="true">中的数据单击命令按钮时消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSF 1.1.我有一个JSF页面,其中包含一个请求范围的bean和一个只读输入字段.

I am using JSF 1.1. I have a JSF page with a request scoped bean and a readonly input field.

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

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

When I set the input value using JavaScript and click on command button, then the data in input field disappears.

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

How is this caused and how can I solve it.

推荐答案

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

That's because the property is set to readonly. If this evaluates true, then JSF won't process the submitted value and hence the model won't be updated. If you want to set it to readonly on rendering the view and have JSF to process the submitted value, then you'd need to make it to evaluate true on render response phase only. You can use FacesContext#getRenderResponse() for this. You'd need to do this in your isDisable() method.

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

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

Note: in JSF2 you could access FacesContext#getCurrentInstance() by #{facesContext} in the view as well, this saves some boilerplate in the model:

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

还请注意,当您使用JSF2 <f:viewParam>时,这种方法将不再适用于GET请求.另请参见将p:calendar设为只读以获取说明和解决方法.

Also note that when you're using JSF2 <f:viewParam>, then this approach won't work on GET requests anymore. See also Make a p:calendar readonly for the explanation and workaround.

这篇关于&lt; h:inputText readonly ="true"&gt;中的数据单击命令按钮时消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆