Skip to content

Instantly share code, notes, and snippets.

@tuner
Created June 4, 2015 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuner/c2380f4795c90f062ea8 to your computer and use it in GitHub Desktop.
Save tuner/c2380f4795c90f062ea8 to your computer and use it in GitHub Desktop.
JSF dynamic <ui:include> problem
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:composite="http://java.sun.com/jsf/composite"
>
<composite:interface>
</composite:interface>
<composite:implementation>
<p>
<h:outputText id="outputText" value="OutputText!" />
</p>
</composite:implementation>
</html>
<ui:fragment xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:components="http://xmlns.jcp.org/jsf/composite/components"
>
<components:comp id="includedComponent" />
</ui:fragment>
<ui:fragment xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:components="http://xmlns.jcp.org/jsf/composite/components"
>
</ui:fragment>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:jsf="http://xmlns.jcp.org/jsf"
xmlns:components="http://xmlns.jcp.org/jsf/composite/components"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:body>
<h:form>
<h:commandButton value="Toggle include!" actionListener="#{includeBean.show}" />
</h:form>
<fieldset jsf:id="dynamicInclude">
<legend>Here we include a composite component dynamically.</legend>
<p>Currently including: #{includeBean.facelet}</p>
<ui:include src="#{includeBean.facelet}" />
</fieldset>
<fieldset jsf:id="problem">
<legend>But an outputText inside this component gets duplicated!</legend>
<components:comp id="notIncluded" />
</fieldset>
</h:body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:jsf="http://xmlns.jcp.org/jsf"
xmlns:components="http://xmlns.jcp.org/jsf/composite/components"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:body>
<h:form>
<h:commandButton value="Toggle include!" actionListener="#{includeBean.show}" />
</h:form>
<fieldset jsf:id="problem">
<legend>Here we have a composite component. It is not affected by the dynamic include below.</legend>
<components:comp id="notIncluded" />
</fieldset>
<fieldset jsf:id="dynamicInclude">
<legend>Here we include a composite component dynamically.</legend>
<p>Currently including: #{includeBean.facelet}</p>
<ui:include src="#{includeBean.facelet}" />
</fieldset>
</h:body>
</html>
import javax.faces.bean.SessionScoped;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.io.Serializable;
/**
* @author Kari
*/
@Named
@ViewScoped
public class IncludeBean implements Serializable {
private boolean include;
public void show() {
include = !include;
}
public String getFacelet() {
return include ? "/resources/includes/componentInclude.xhtml" : "/resources/includes/emptyInclude.xhtml";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment