JSF readonly inputText问题

Pya*_*ung 9 jsf primefaces

Readonly inputText不验证是否required="true"已设置.

<h:panelGrid columns="3" id="townShipPanelGroup">
    <p:inputText value="#{AddNewLifeProposalActionBean.beneficiariesInfoDTO.residentAddress.township == null ? '' : AddNewLifeProposalActionBean.beneficiariesInfoDTO.residentAddress.township.name}" 
        style="width:250px;margin-left:-4px;" id="townShip" readonly="true">
            <f:validateLength maximum="36"/>
    </p:inputText>
    <p:commandLink immediate="true" oncomplete="selectTownShipDialog.show()" id="selectTownShipDialogLink" action="#{AddNewLifeProposalActionBean.loadTownshipList()}">  
    <h:graphicImage url="/images/search.png" style="height:30px;width:30px"/>
    </p:commandLink>
</h:panelGrid>
Run Code Online (Sandbox Code Playgroud)

kol*_*sus 14

这表现得如预期.出于所有意图和目的,JSF不会评估readonly="true"在整个请求生命周期中指定的值.完成此操作的推荐方法是RENDER_RESPONSE在将视图呈现给用户的阶段中使值只读.在任何其他阶段,您希望JSF运行时将输入字段解释为可写.为此,您可以使用:

<p:inputText value="#{AddNewLifeProposalActionBean.beneficiariesInfoDTO.residentAddress.township == null ? '' : AddNewLifeProposalActionBean.beneficiariesInfoDTO.residentAddress.township.name}" 
        style="width:250px;margin-left:-4px;" id="townShip" readonly="#{facesContext.renderResponse}">
            <f:validateLength maximum="36"/>
    </p:inputText>
Run Code Online (Sandbox Code Playgroud)

这样,该readonly属性仅在用户查看页面时才为true.对于所有其他阶段,JSF运行时将该字段视为可写,因此将在该字段上执行验证

参考文献:

  • 由于JSF2,`#{facesContext.renderResponse}`不再保证在所有情况下都能正常工作.http://stackoverflow.com/questions/17639415/make-pcalendar-readonly/17639554#17639554 (3认同)