6

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Jacob
  • 14,463
  • 65
  • 207
  • 320
  • 6
    I know that some people answering question on SO may be considered magicians, but they can do nothing **without code sample**. – maialithar Apr 30 '12 at 13:00
  • I really don't know what happens in `cal(some String variable)` method ... – maialithar Apr 30 '12 at 14:12
  • if your readonly is set to true the value of #{bean.sdate} wont be sent to server... and thus get lost... – Daniel Apr 30 '12 at 14:19
  • @Daniel: you're describing `disabled`, not `readonly`. – BalusC Apr 30 '12 at 18:26
  • @BalusC it happened for readonly=true also :) – Ajay Sharma Apr 15 '16 at 07:07
  • @Ajay: Wrong. Check Network panel in browser to see it yourself. HTML `disabled` won't send value. HTML `readonly` will send value. It's only that JSF itself skips processing of both. Taking a step back and learning some basic HTML before diving further deep in JSF wouldn't be a bad idea. – BalusC Apr 15 '16 at 07:10
  • @BalusC yes you are correct JSF itself is skipping readonly=true – Ajay Sharma Apr 15 '16 at 07:13

1 Answers1

19

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.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • at the same time I was wondering if suppose I want to update the model at any other phase like "process validation" then how can I do the same like you did here. – Addicted May 30 '12 at 04:34