0

i want to display a JSF custom Component (i think it's the same way like a normal component).

List<String> components = new ArrayList<String>();
components.add("<p:commandButton />");

This is how it works, and i want to display the Jsf component rendered. At the moment, it's rendered as rich text.

Is there a way?

<p:outputPanel>#{list.get(0)}</p:outputPanel>
mistermm
  • 81
  • 1
  • 2
  • 8
  • you want display component from list or you want to add them into list? – Amol Raje Nov 20 '17 at 09:25
  • @AmolRaje I want to display this component, maybe i should use another Object Type, then String ? – mistermm Nov 20 '17 at 09:36
  • Very probably Primefaces CSS styles are missing. Please post [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) of your code so that anybody can understand better what are you trying to achieve. – Dusan Kovacevic Nov 20 '17 at 09:46

1 Answers1

0

You cannot dynamically add JSF components on that way.

Correct way would be:

  1. First create a model of button properties. For example,

    public class ButtonProperties {
        String name;
    
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
    }
    
  2. Define and initialize button list inside bean. For example

    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);
        }
    }
    
  3. Use ui:repeat component inside xhtml page

            <p:outputPanel>
                <ui:repeat value="#{yourBean.buttonList}" var="button">
                    <p:commandButton value="#{button.name}"/>
                </ui:repeat>
            </p:outputPanel>
    
Dusan Kovacevic
  • 1,377
  • 1
  • 13
  • 19
  • This is to dispaly a button, what if it's a or something else (your answer is just for buttons, and this is not difficult), is there also no possibilties?` – mistermm Nov 20 '17 at 10:36
  • 1
    This is just an example explaining concepts. Replace button with p:outputLabel and this example will work. The same approach can be used for any other basic or complex component or group of components. Use conditional rendering to make it more flexible. Etc...etc... – Dusan Kovacevic Nov 20 '17 at 10:44
  • ok but ther are only custom components, and i don't know how they are... – mistermm Nov 20 '17 at 11:51
  • Then create a 'model' in java (not xhtml) and add all possible components in the xhtml and use the 'rendered' attribute on them, or use a c:foreach or... Be creative. But if you seriously want it to dynamically create 'xhtml' with all risks involved, read https://stackoverflow.com/questions/13292272/obtaining-facelets-templates-files-from-an-external-filesystem-or-database – Kukeltje Nov 20 '17 at 21:25