首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSF 2:封装公共属性值的标签包装器?

JSF 2:封装公共属性值的标签包装器?
EN

Stack Overflow用户
提问于 2010-12-20 09:51:33
回答 1查看 1.4K关注 0票数 2

基于前面的问题How to get ID of calling component in the getter method?,我想问您另一个想法:

在jsf页面上有大量重复代码,例如以下示例(注意重复大小和最大长度属性):

代码语言:javascript
复制
<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)}"
/>

我在想:

  1. 使用复合组件作为输入文本标记,
  2. 硬编码复合组件
  3. 的实现部分中的大小和最大长度,这样每当我需要使用该组件时都不必重复所有这些内容。
  4. ,但是我必须在复合组件

G 211的接口部分打开所有属性

这个想法可以吗,还是有其他更好的方法来解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-12-20 22:23:14

你可以这么做。我也在一些项目中实现了它。它只会增加一些(小的)开销。为了这个特定的目的,您也可以只使用Facelets标记文件,而不是JSF复合组件。因此,定义属性并不是强制性的。在您的特殊情况下,如果您将bean属性名称作为消息包标签的id和key重用,那么您几乎可以重构很多重复的属性。

例如。

代码语言:javascript
复制
<my:input type="text" bean="#{userBean}" property="userId" required="true" />
<my:input type="secret" bean="#{userBean}" property="password" required="true" />

在Facelets标记文件中使用以下内容:

代码语言:javascript
复制
<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>实现了它,这使得像这样的重构更加合理。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4488498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档