Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data in <h:inputText readonly="true"> disappears when command button is clicked

Tags:

jsf

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>

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.

like image 201
Jacob Avatar asked Apr 30 '12 12:04

Jacob


1 Answers

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();
}

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}" />

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.

like image 190
BalusC Avatar answered Sep 19 '22 17:09

BalusC