How do I break the tyranny of the clientID?

后端 未结 1 2005
别那么骄傲
别那么骄傲 2020-12-11 00:01

My least favorite part of coding JSF 2.0 forms has to do with the handing of the id attributes of the various input elements. I am forever having trouble c

相关标签:
1条回答
  • 2020-12-11 00:11

    since I have to create UIComponent properties, getters and setters, and bind them in the form with binding attributes. Can anyone suggest a better way of doing this?

    It's not required to bind the component to some backing bean if you don't use it in there at all. Just bind it to the view instead:

    <p:inputText value="#{editUser.user.fullName}"
                 binding="#{compFullName}"/>
    <p:message for="#{compFullName.clientId}" />
    

    To make the code more self-documenting, I suggest to put a HashMap in the request scope by faces-config.xml:

    <managed-bean>
        <description>Holder of all component bindings.</description>
        <managed-bean-name>components</managed-bean-name>
        <managed-bean-class>java.util.HashMap</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
    

    with

    <p:inputText value="#{editUser.user.fullName}"
                 binding="#{components.fullName}"/>
    <p:message for="#{components.fullName.clientId}" />
    

    Adding messages is supposed to be done by a Converter or a Validator which is trowing it as a ConverterException or ValidatorException respectively. It will automatically end up in the right message holder. Or if it are informal messages, just add it on the client ID of the UIComponent which is already available as method argument.

    See also:

    • JSF component binding without bean property
    0 讨论(0)
提交回复
热议问题