只读一个p:日历

时间:2013-07-14 12:32:45

标签: jsf jsf-2 primefaces

我想只读<p:calendar>以便用户只能选择日历,因为this问题(但这不是解决方案)。

对于这一点,我正在按照this的回答提到readonly="#{facesContext.renderResponse}"

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

这样可行但是当页面加载时(在地址栏中键入URL然后按回车键),facesContext.renderResponse将返回false并且日历不再是只读的。当我按true提交表单时,评估结果为<p:commandButton>

那么,如何在页面加载时只读日历?

P.S:我正在使用PrimeFaces 3.5和Mojarra 2.1.9。

1 个答案:

答案 0 :(得分:11)

自JSF 2.0以来,行为确实发生了变化。 FacesContext#getRenderResponse()仅在FacesContext#renderResponse() 显式被调用时才返回true。以前这发生在每个GET请求的恢复视图阶段。但是,自从引入<f:viewParam>以来,当至少存在一个视图参数时,JSF将不再这样做,它将继续执行每个阶段而不跳过任何阶段,以便正确处理视图参数。

您的网页显然有<f:viewParam>。这是完全正常的,但作为测试,尝试删除它,你会看到它在普通的GET请求上返回true

你基本上有2个选择来解决它:

  1. 同时检查FacesContext#isPostback()。它总是在GET请求上返回false

    readonly="#{not facesContext.postback or facesContext.renderResponse}"
    
  2. 请检查FacesContext#getCurrentPhaseId()。你只会得到更丑陋的代码(魔术数字)。

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

    如果您正在使用OmniFaces,则可以减少丑陋。

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