【问题标题】:Make a p:calendar readonly使 p:calendar 只读
【发布时间】:2013-07-14 12:32:44
【问题描述】:

我想让<p:calendar> 只读,这样用户就只能从日历中选择一个日期,因为this 问题(虽然这不是解决方案)。

为此,我正在做readonly="#{facesContext.renderResponse}" 所提到的this 回答喜欢,

<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,然后按 Enter 键),facesContext.renderResponse 返回false 并且日历不再是只读的。当我按下&lt;p:commandButton&gt; 提交表单时,它的计算结果为true

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

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

【问题讨论】:

    标签: jsf jsf-2 primefaces


    【解决方案1】:

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

    您的页面中显然有一个&lt;f:viewParam&gt;。这完全没问题,但作为测试,尝试删除它,您会看到它在普通 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}"
      

    【讨论】:

    • 这是一个很好的解决方案:)
    猜你喜欢
    • 1970-01-01
    • 2013-05-28
    • 2014-07-23
    • 2015-10-14
    • 2020-05-27
    • 2016-04-25
    • 2011-01-26
    • 1970-01-01
    • 2019-09-17
    相关资源
    最近更新 更多