Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
772 views
in Technique[技术] by (71.8m points)

jsf - Make a p:calendar readonly

I want to make <p:calendar> readonly so that users can only choose a date from the calendar because of this issue (this is not a solution though).

For this to be so, I'm doing readonly="#{facesContext.renderResponse}" as mentioned by this answer like,

<p:calendar id="calendarId" 
        value="#{bean.property}" 
        converter="#{jodaTimeConverter}" 
        pattern="dd-MMM-yyyy hh:mm:ss a" 
        showOn="button" 
        readonly="#{facesContext.renderResponse}" 
        effect="slideDown"
        required="true" 
        showButtonPanel="true" 
        navigator="true"/>

This works but when the page is loaded (typing the URL in the address bar and then pressing the enter key), facesContext.renderResponse returns false and the calendar is no longer readonly. It evaluates to true, when I submit the form by pressing <p:commandButton>.

So, how to make the calendar readonly, when the page is loaded?

P.S : I'm using PrimeFaces 3.5 and Mojarra 2.1.9.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The behavior has indeed changed since JSF 2.0. The FacesContext#getRenderResponse() only returns true if FacesContext#renderResponse() is explicitly been called. Previously this happened during restore view phase of every GET request. However, since the introduction of <f:viewParam>, JSF will not do that anymore when at least one view parameter is present, it will just continue executing every single phase without skipping any phase in order to properly process the view parameters.

You apparently have a <f:viewParam> in your page. That's totally fine, but as a test, try removing it and you'll see that it returns true on a plain GET request as well.

You've basically 2 options to get around it:

  1. Check the FacesContext#isPostback() as well. It always returns false on GET requests.

    readonly="#{not facesContext.postback or facesContext.renderResponse}"
    
  2. Check the FacesContext#getCurrentPhaseId() instead. You only end up with uglier code (magic numbers).

    readonly="#{facesContext.currentPhaseId.ordinal eq 6}"
    

    If you're using OmniFaces, you could make it less ugly.

    <o:importConstants type="javax.faces.event.PhaseId" />
    ...
    readonly="#{facesContext.currentPhaseId eq PhaseId.RENDER_RESPONSE}"
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...