问题描述
我知道创建自定义JSF组件的两种方法:1.本机JSF方式:创建JSF组件类,标签等.2. Facelets方式:在xhtml文件中定义组件,然后在facelets taglib中创建适当的说明.
I know of two ways of creating custom JSF components:1. Native JSF way: creating JSF component class, tag, etc.2. Facelets way: defining component in a xhtml file and then creating appropriate decrption in facelets taglib.
当前,我正在一个项目中,不幸的是引入了facelets.另一方面,以标准JSF方式创建自定义组件似乎很麻烦.
Currently I work on a project in which introducing facelets is unfortunately out of the question. On the other hand, creating custom components the standard JSF way seems like a pain in the ass.
是否存在第三方库,该库允许以类似于facelets的方式创建自定义组件,但不需要使用非标准渲染器?
Is there maybe a third party library that allows creating custom components in the way similar to facelets but doesn't entail the need of using non-standard renderer?
推荐答案
您可以使用(例如) jsp:include 和 f:subview .
You can do a limited amount of templating using (for example) jsp:include and f:subview.
或者,您可以扩展 UIComponent 覆盖选定的方法,然后使用binding
属性通过现有标签和托管Bean提供它.这仍然需要对组件开发(以及这种选择的后果)有一个相当详细的了解,但是可能会大大减少文件数量/代码量.
Alternatively, you can extend a UIComponent overriding selected methods and then provide it via an existing tag and a managed bean using the binding
attribute. This still requires a reasonably detailed understanding of component development (and the consequences of this choice), but could cut down the number of files/volume of code significantly.
这种方法有点破烂,但对于短期工作来说可能还可以.对于要分发的组件库或需要长期维护的组件,您不会这样做.
This approach is a bit of a hack, but might be OK for short-term stuff. You wouldn't do it for component libraries you want to distribute or components requiring long term maintenance.
新组件:
public class QuickComponent extends HtmlOutputText {
@Override public void encodeAll(FacesContext context) throws IOException {
ResponseWriter writer = context.getResponseWriter();
writer.writeText("I'm not really a HtmlOutputText", null);
for (UIComponent kid : getChildren()) {
if (kid instanceof UIParameter) {
UIParameter param = (UIParameter) kid;
writer.startElement("br", this);
writer.endElement("br");
writer.writeText(param.getName() + "=" + param.getValue(), null);
}
}
}
}
提供实例的bean:
/**Request-scope managed bean defined in faces-config.xml*/
public class QuickComponentProviderBean {
private QuickComponent quick;
public void setQuick(QuickComponent quick) {
this.quick = quick;
}
public QuickComponent getQuick() {
if (quick == null) {
quick = new QuickComponent();
}
return quick;
}
}
注意:不要在视图中的多个标签上重复使用单个bean属性,否则它们将引用相同的对象实例.
将新组件添加到视图:
<h:outputText binding="#{quickComponentProviderBean.quick}">
<f:param name="Hello" value="World" />
</h:outputText>
注意:可以定义的属性没有更改.它们由TLD固定.
这篇关于创建JSF定制组件的快速方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!