Based on this previous question How to get ID of calling component in the getter method? , here’s another idea i want to ask your opinion about :
there are lots of duplicated code across jsf pages such as these examples (notice the repeating size and maxlength attribute) across components :
<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)}"
/>
Im thinking of :
- using composite component for that
input text tag, - hardcoding the size and maxlength in the implementation section of that composite component,
- so that i dont have to duplicate all
these stuffs everytime i need to use
that component. - but i’ll have to open
up all attributes in the interface
section of that composite component
Is this idea ok, or perhaps there are other better ways to solve this problem ?
You could do so. I’ve also implemented it in some projects. It only adds some (minor) overhead. You could for this particular purpose also just use a Facelets tag file instead of a JSF composite component. It’s not mandatory to define the attributes then. In your particular case you can refactor pretty much duplicates if you reuse the bean property name as id and key for message bundle label.
E.g.
with the following in a Facelets tag file:
I’ve however implemented it with an
<h:outputLabel>before and a<h:message>after which makes refactoring like this more reasonable.