我想显示一个JSF自定义组件(我认为它与普通组件一样)。
List<String> components = new ArrayList<String>();
components.add("<p:commandButton />");
这是它的工作原理,我想显示渲染的Jsf组件。目前,它被渲染为富文本。
有办法吗?
<p:outputPanel>#{list.get(0)}</p:outputPanel>
答案 0 :(得分:0)
您不能以这种方式动态添加JSF组件。
正确的方法是:
首先创建一个按钮属性模型。例如,
public class ButtonProperties {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
定义并初始化bean内的按钮列表。例如
List<ButtonProperties> buttonList=new ArrayList<>();
public List<ButtonProperties> getButtonList() {
return buttonList;
}
public void setButtonList(List<ButtonProperties> buttonList) {
this.buttonList = buttonList;
}
//for example call this inside @PostConstruct method
private void initButtons(){
for (int i=0;i<5;i++){
ButtonProperties buttonInfo=new ButtonProperties();
buttonInfo.setName("Button "+i);
buttonList.add(buttonInfo);
}
}
在xhtml页面中使用ui:repeat
组件
<p:outputPanel>
<ui:repeat value="#{yourBean.buttonList}" var="button">
<p:commandButton value="#{button.name}"/>
</ui:repeat>
</p:outputPanel>