【发布时间】:2012-03-10 15:05:52
【问题描述】:
我正在开发一个 JSF2/Primefaces 应用程序,但在访问该组件的支持 bean 中复合组件的接口中定义的属性时遇到问题。
我的组件定义如下:
<!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://java.sun.com/jsf/html"
xmlns:composite="http://java.sun.com/jsf/composite">
<composite:interface componentType="testComponent">
<composite:attribute name="text" required="true" type="java.lang.String" />
</composite:interface>
<composite:implementation>
<h:outputText value="Passed text is: #{cc.attrs.text}" />
</composite:implementation>
</html>
它存储在一个名为text.xhtml 的文件中,该文件位于:application/src/main/webapp/resources/my_component 目录中。
我在另一个页面(这是一个 Facelets 组合元素)上使用这个组件,如下所示:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:myc="http://java.sun.com/jsf/composite/my_component"
template="./resources/templates/template.xhtml">
<ui:define name="content">
<h:form id="products">
<myc:test id="test" text="A text" />
</h:form>
</ui:define>
</ui:composition>
后台组件类定义如下:
package my.application.component;
import javax.faces.component.FacesComponent;
import javax.faces.component.UINamingContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@FacesComponent ( value="testComponent" )
public class TestComponent extends UINamingContainer {
Logger log = LoggerFactory.getLogger(TestComponent.class);
public TestComponent() {
log.debug("TestComponent constructor");
log.debug("getAttributes().size(): " + getAttributes().size());
}
}
组件本身按预期工作。使用页面呈现Passed text is: A text 输出。
此外,记录器输出显示来自 TestComponent 构造函数的日志消息,因此似乎组件 xml 定义与 TestComponent 类正确绑定。
现在,问题是在TestComponent 构造函数中调用的getAttributes() 方法总是返回一个零大小的地图。
如果我理解正确,我应该能够使用调用访问组件接口中声明的text 属性:
getAttributes().get("text");
在TestComponent 类中,但由于属性映射为空,它总是返回null。
我还尝试使用调用访问text 属性:
String text = FacesContext.getCurrentInstance().getApplication().
evaluateExpressionGet(FacesContext.getCurrentInstance(), "#{cc.attrs.text}", String.class));
但它也解析为null。
我做错了什么?任何提示将不胜感激,因为我不知道下一步该尝试什么。
/图卡兹。
【问题讨论】:
标签: jsf-2 primefaces composite-component