使ap:日历只读

Tin*_*iny 6 jsf primefaces jsf-2

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

对于这一点,我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"/>
Run Code Online (Sandbox Code Playgroud)

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

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

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

Bal*_*usC 12

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

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

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

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

    readonly="#{not facesContext.postback or facesContext.renderResponse}"
    
    Run Code Online (Sandbox Code Playgroud)
  2. 检查一下FacesContext#getCurrentPhaseId().你只会得到更丑陋的代码(魔术数字).

    readonly="#{facesContext.currentPhaseId.ordinal eq 6}"
    
    Run Code Online (Sandbox Code Playgroud)

    如果你正在使用OmniFaces,你可以减少它的难度.

    <o:importConstants type="javax.faces.event.PhaseId" />
    ...
    readonly="#{facesContext.currentPhaseId eq PhaseId.RENDER_RESPONSE}"
    
    Run Code Online (Sandbox Code Playgroud)