基于前面的问题How to get ID of calling component in the getter method?,我想问您另一个想法:
在jsf页面上有大量重复代码,例如以下示例(注意重复大小和最大长度属性):
<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)}"
/>我在想:
G 211的接口部分打开所有属性
这个想法可以吗,还是有其他更好的方法来解决这个问题?
发布于 2010-12-20 22:23:14
你可以这么做。我也在一些项目中实现了它。它只会增加一些(小的)开销。为了这个特定的目的,您也可以只使用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>实现了它,这使得像这样的重构更加合理。
https://stackoverflow.com/questions/4488498
复制相似问题