Note that there are some explanatory texts on larger screens.

plurals
  1. POJSF dynamic template selection through manage bean
    text
    copied!<p>My use case is to provide facility to clients to select template dynamically. I am trying to select template using managed bean, as follows:</p> <p><strong>testTemplate1.xhtml</strong></p> <pre><code>&lt;?xml version='1.0' encoding='UTF-8' ?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html"&gt; &lt;h:head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt; &lt;link href="./resources/css/default.css" rel="stylesheet" type="text/css" /&gt; &lt;link href="./resources/css/cssLayout.css" rel="stylesheet" type="text/css" /&gt; &lt;title&gt;Facelets Template&lt;/title&gt; &lt;/h:head&gt; &lt;h:body&gt; Template 1 &lt;div id="content" class="center_content"&gt; &lt;ui:insert name="content"&gt;Content&lt;/ui:insert&gt; &lt;/div&gt; &lt;/h:body&gt; &lt;/html&gt; </code></pre> <p><strong>testTemplate2.xhtml</strong></p> <pre><code>&lt;?xml version='1.0' encoding='UTF-8' ?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html"&gt; &lt;h:head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt; &lt;link href="./resources/css/default.css" rel="stylesheet" type="text/css" /&gt; &lt;link href="./resources/css/cssLayout.css" rel="stylesheet" type="text/css" /&gt; &lt;title&gt;Facelets Template&lt;/title&gt; &lt;/h:head&gt; &lt;h:body&gt; Template 2 &lt;div id="content" class="left_content"&gt; &lt;ui:insert name="content"&gt;Content&lt;/ui:insert&gt; &lt;/div&gt; &lt;/h:body&gt; &lt;/html&gt; </code></pre> <p><strong>testTemplateClient.xhtml</strong></p> <pre><code>&lt;?xml version='1.0' encoding='UTF-8' ?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets"&gt; &lt;body&gt; &lt;ui:composition template="#{templateSelection.selectedTemplate}"&gt; &lt;ui:define name="content"&gt; Hello World &lt;/ui:define&gt; &lt;/ui:composition&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>TemplateSelection.java</strong></p> <pre><code>/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import javax.inject.Named; import javax.enterprise.context.Dependent; /** * * @author aneeshaider */ @Named(value = "templateSelection") @Dependent public class TemplateSelection { /** * Creates a new instance of TemplateSelection */ public TemplateSelection() { } private String selectedTemplate="./testTemplate1.xhtml"; public String getSelectedTemplate() { return selectedTemplate; } public void setSelectedTemplate(String selectedTemplate) { this.selectedTemplate = selectedTemplate; } } </code></pre> <p>I am unable to select the template dynamically, instead no template is selected at all, only client is displayed. any idea, how to work it out?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload