使 p:calendar 只读 [英] Make a p:calendar readonly

查看:23
本文介绍了使 p:calendar 只读的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作 <p:calendar> readonly 以便用户只能从日历中选择日期,因为 this 问题(虽然这不是解决方案).

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).

为此,我正在执行 readonly="#{facesContext.renderResponse}" 正如 这个 回答就像,

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

这有效,但是当页面加载时(在地址栏中输入 URL,然后按 Enter 键),facesContext.renderResponse 返回 false 并且日历是不再是只读的.当我按 提交表单时,它的计算结果为 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?

PS:我使用的是 PrimeFaces 3.5 和 Mojarra 2.1.9.

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

推荐答案

自 JSF 2.0 以来,行为确实发生了变化.FacesContext#getRenderResponse() 仅在 FacesContext#renderResponse()显式调用.以前,这发生在每个 GET 请求的恢复视图阶段.但是,由于引入了 ,当至少存在一个视图参数时,JSF 将不再这样做,它只会继续执行每个单独的阶段,而不会跳过任何阶段,以便正确处理视图参数.

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.

您的页面中显然有一个 .这完全没问题,但作为测试,尝试删除它,您会看到它也在普通 GET 请求中返回 true.

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.

您基本上有两种选择:

  1. 检查FacesContext#isPostback() 也是如此.它总是在 GET 请求上返回 false.

readonly="#{not facesContext.postback or facesContext.renderResponse}"

  • 检查 FacesContext#getCurrentPhaseId() 代替.你最终只会得到更丑陋的代码(幻数).

  • Check the FacesContext#getCurrentPhaseId() instead. You only end up with uglier code (magic numbers).

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

    如果您使用 OmniFaces,您可以让它不那么难看.

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

  • 这篇关于使 p:calendar 只读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆