JSF readonly inputText问题

时间:2013-09-09 07:16:59

标签: 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>

1 个答案:

答案 0 :(得分: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>

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

参考文献: