在运行时从支持 bean 添加 JSF 2 复合组件
已编辑的问题...
您好,
我想从支持 bean 加载复合组件的 .xhtml 文件,并将其动态添加到页面中。 .xhtml 文件的名称来自变量。
例如:
public MyBean (){
String componentFile = "myCompositeComponent.xhtml"
public String addComponentToPage(){
//how do that?...
return null;
}
}
谢谢!
Edited question...
Hello,
I would like to load a .xhtml file of my composite component from a backing bean, and add it to the page dynamically. The name of the .xhtml file comes form a variable.
Ex.:
public MyBean (){
String componentFile = "myCompositeComponent.xhtml"
public String addComponentToPage(){
//how do that?...
return null;
}
}
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是不可能的。复合组件是基于模板的,只能在视图中使用。最好的选择是准确地重复您最初在模型的复合组件中编写的 JSF 代码。更好的是创建一个完整的有价值的
@ FacesComponent
类,扩展UIComponent
,包含@FacesRenderer
。确实,这是一项乏味且不透明的工作,但这样您最终将得到一个可以通过单个代码行在视图和模型中重用的组件。一个丑陋的替代方案是将所有可能的组件放置在视图中并使用
rendered
属性。如有必要,请将其包装在 Facelets 标记文件中,以便可以将其隐藏并在多个位置重复使用。
That's not possible. A composite component is template-based and can only be used in views. Your best bet is to repeat exactly the JSF code which you've originally written in the composite component in the model. Better would be to create a full worthy
@FacesComponent
class which extendsUIComponent
, complete with a@FacesRenderer
. True, it's a tedious and opaque job, but this way you'll end up with a component which is reuseable in both the view and the model by a single code line.An -ugly- alternative is to place all possible components in the view and use the
rendered
attribute.Wrap this if necessary in a Facelets tag file so that it can be hidden away and reused in several places.
我不明白为什么你想从支持 bean 添加复合组件。我猜你想让它在发生事件时动态可见,但为此有 AJAX reRender。
例如,您可以执行以下操作:
renderComponent
属性存储一个boolean
值。您可以切换该值并使用例如 Richfaces 的
重新渲染composite
。希望有帮助,丹尼尔
I don't understand why do you want to add a composite component from a backing bean. I guess you want to make it visible dynamically in case of an event, but for that there is AJAX reRender.
For example you can do the following:
The
renderComponent
property stores aboolean
value. You can switch that value and reRendercomposite
with for e.g. Richfaces's<a4j:commandLink>
.Hope that helps, Daniel