Nikola
Nikola

Reputation: 624

Programmatically created html components in jsf managed beans

I have a following code:

<div id="mws-navigation">
    <ul>
        <li><p:commandLink
                value="#{contentMB.msg.welcome_title.value}" actionListener="#{cleanUpMB.alChangeArea}"
                styleClass="mws-i-24 i-home" action="#{welcomeMB.aLoadDashboard}" global="false" /></li>
        <li><p:commandLink
                value="#{contentMB.msg.layout_menu_measures.value}"
                rendered="#{userSessionMB.measureEdit or userSessionMB.measureCreate}"
                styleClass="mws-i-24 i-table-1" global="false" />
            <ul>
                <li><p:commandLink
                        value="#{contentMB.msg.layout_menu_mm_findMeasures.value}"
                        rendered="#{userSessionMB.measureEdit}"
                        actionListener="#{cleanUpMB.alChangeArea}"
                        action="#{chooseMeasureControllerMB.aChoose}" /></li>
                <li><p:commandLink
                        value="#{contentMB.msg.layout_menu_mm_newMeasures.value}"
                        rendered="#{userSessionMB.measureCreate}"
                        actionListener="#{cleanUpMB.alChangeArea}"
                        action="#{newMeasureControllerMB.aNew}" /></li>
            </ul>
      </li>
   </ul>
</div>

which I want to generate from the backing JSF managed bean. I know that for Primefaces exist java components that I can use and bind them as a model, but how can I generate pure HTML tags:

<ul>
<div>
...

?

Upvotes: 1

Views: 2652

Answers (1)

joerno
joerno

Reputation: 1101

A simple way would be to return some HTML from the backing bean:

@Named
@RequestScoped
public class HtmlController implements Serializable {

private static final long serialVersionUID = 1L;

    public String getSomeHtml(){
        return "<h1>Some HTML from a bean</h2>";
    }
}

And paste this into the JSF part:

<h:outputText value="#{htmlController.someHtml}" escape="false" />

But I think for your case it would be better to create your own component, there you can also do some binding to backing beans. One example how to do that can be found here or take look at the tutorial of Java EE 6 .

Upvotes: 2

Related Questions