问题描述
|
JSF 2复合组件中是否有继承之类的东西?
据我所知,没有。
我只是确定。
谢谢!
解决方法
复合组件的继承是不可能的。为了避免代码重复,我们要做的是装饰JSF2复合组件的实现。
装饰程序模板中提供了我们应用程序所有输入字段共享的内容,如下所示:
<ui:composition xmlns=\"http://www.w3.org/1999/xhtml\"
xmlns:cc=\"http://java.sun.com/jsf/composite\"
xmlns:h=\"http://java.sun.com/jsf/html\"
xmlns:f=\"http://java.sun.com/jsf/core\"
xmlns:ui=\"http://java.sun.com/jsf/facelets\"
xmlns:cu=\"http://mytags.de/jsftags\">
<!-- provides a common set of layout information for inputfields -->
<ui:param name =\"fieldStyle\" value=\"#{propertiesController.get(\'FIELD_STYLE\',cc.attrs.name)}\" />
<h:panelGroup id=\"basicInputField\" styleClass=\"basicInputField\" layout=\"block\" style=\"width: #{cc.attrs.width}; height: #{cc.attrs.height};\">
<ui:insert name=\"component\">
no component given...
</ui:insert>
</h:panelGroup>
</ui:composition>
复合组件使用模板来装饰自己:
<html xmlns=\"http://www.w3.org/1999/xhtml\"
xmlns:cc=\"http://java.sun.com/jsf/composite\"
xmlns:h=\"http://java.sun.com/jsf/html\"
xmlns:f=\"http://java.sun.com/jsf/core\"
xmlns:ui=\"http://java.sun.com/jsf/facelets\"
xmlns:cu=\"http://mytags.de/jsftags\">
<cc:interface>
<cc:attribute name=\"name\" required=\"true\" />
<cc:attribute name=\"width\" required=\"false\" default=\"auto\" />
<cc:attribute name=\"height\" required=\"false\" default=\"auto\" />
<cc:attribute name=\"inset\" required=\"false\" default=\"0px\" />
</cc:interface>
<cc:implementation>
<ui:decorate template=\"basicInputField.xhtml\">
<ui:define name=\"component\">
<h:inputText id=\"inputText\" style=\"#{fieldStyle} width: 100%;\" value=\"#{levelContent.test}\" />
</ui:define>
</ui:decorate>
</cc:implementation>
</html>
这样,当我们获取字段属性(即只读,必填,样式等)的方式发生变化时,我们只需要更改装饰模板即可。