JSF 2:标记包装器来封装常见的属性值?

基于前一个问题如何在getter方法中获取调用组件的ID? ,这是另一个想法,我想问你的意见: jsf页面中有许多重复的代码,例如这些示例(注意重复大小和maxlength属性)跨组件:
<h:inputText label="#{msgs.userId}" id="UserId" value="#{userBean.userId}" 
   required="true" 
   size="#{variableConfigBean.getSize(component.id)}" 
   maxlength="#{variableConfigBean.getMaxLength(component.id)}"
/>
<h:inputSecret label="#{msgs.password}" id="Password" value="#{userBean.password}" 
   required="true"  
   size="#{variableConfigBean.getSize(component.id)}" 
   maxlength="#{variableConfigBean.getMaxLength(component.id)}"
/>
我想: 使用复合组件 输入文字标签, 硬编码该复合组件的实现部分中的size和maxlength, 所以我不必复制所有 每次我需要使用这些东西 那个组件。 但我必须打开 界面中的所有属性 该复合组件的一部分 这个想法是否正确,或者还有其他更好的方法来解决这个问题?     
已邀请:
你可以这样做。我也在一些项目中实现了它。它只增加了一些(次要的)开销。您可以为此特定目的使用Facelets标记文件而不是JSF复合组件。然后,不必定义属性。在您的特定情况下,如果您将bean属性名称重用为消息包标签的id和key,则可以重构几乎重复的内容。 例如。
<my:input type="text" bean="#{userBean}" property="userId" required="true" />
<my:input type="secret" bean="#{userBean}" property="password" required="true" />
在Facelets标记文件中包含以下内容:
<c:set var="id" value="#{not empty id ? id : property}" />
<c:set var="required" value="#{not empty required and required}" />

<c:choose>
    <c:when test="#{type == 'text'}">
        <h:inputText id="#{id}" 
            label="#{msgs[property]}"
            value="#{bean[property]}" 
            size="#{config.size(id)}" 
            maxlength="#{config.maxlength(id)}" 
            required="#{required}" />
    </c:when>
    <c:when test="#{type == 'secret'}">
        <h:inputSecret id="#{id}" 
            label="#{msgs[property]}"
            value="#{bean[property]}" 
            size="#{config.size(id)}" 
            maxlength="#{config.maxlength(id)}" 
            required="#{required}" />
    </c:when>
    <c:otherwise>
        <h:outputText value="Unknown input type: #{type}" />
    </c:otherwise>            
</c:choose>
不过我之前用
<h:outputLabel>
<h:message>
实现了它,之后使这样的重构变得更合理。     

要回复问题请先登录注册