我的用例是为客户端提供动态选择模板的便利。我正在尝试使用托管bean选择模板,如下所示:
testTemplate1.xhtml
<?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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="./resources/css/default.css" rel="stylesheet" type="text/css" />
<link href="./resources/css/cssLayout.css" rel="stylesheet" type="text/css" />
<title>Facelets Template</title>
</h:head>
<h:body>
Template 1
<div id="content" class="center_content">
<ui:insert name="content">Content</ui:insert>
</div>
</h:body>
</html>
testTemplate2.xhtml
<?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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="./resources/css/default.css" rel="stylesheet" type="text/css" />
<link href="./resources/css/cssLayout.css" rel="stylesheet" type="text/css" />
<title>Facelets Template</title>
</h:head>
<h:body>
Template 2
<div id="content" class="left_content">
<ui:insert name="content">Content</ui:insert>
</div>
</h:body>
</html>
testTemplateClient.xhtml
<?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:ui="http://java.sun.com/jsf/facelets">
<body>
<ui:composition template="#{templateSelection.selectedTemplate}">
<ui:define name="content">
Hello World
</ui:define>
</ui:composition>
</body>
</html>
TemplateSelection.java
/*
* 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;
}
}
我无法动态选择模板,而是根本没有选择模板,只显示客户端。知道怎么解决吗?
实际上我复制了您的代码,只更改了名称,就成功了。你确定你的路径/名称是正确的吗?