Search code examples
jsfprimefacesjavabeansmanaged-bean

Can I append HTML elements in current page from ManagedBean?


So I have a managed viewscope bean, and I have a html page with a container

<div id="container>...</div>

I want to go through a list of Strings in the managedBean and append a whole bunch of selectOneMenu items into the container from the Java side based on what's in that list.

Can I do that? And if so, what is the easiest way. Also I am using Primefaces and am open to use Javascript but would prefer to avoid it.


Solution

  • There are, in fact, two ways I know to do that. The first (and probably the easiest) one is to inject html directly via the following method:

    org.primefaces.context.RequestContext.getCurrentInstance().execute("<input type='text' />");
    

    In this case, you will just attatch static html or javascript to your page.

    In the case you want to add jsf components, you can manipulate components from the backing bean by using binding. Then you can instantiate components as simple java objects. (What is component binding in JSF? When it is preferred to be used?)