【问题标题】:Accessing JSF2 composite component attributes in backing component在支持组件中访问 JSF2 复合组件属性
【发布时间】: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


    【解决方案1】:

    我的猜测是构造函数引用这些属性为时尚早。

    JSF 将首先构造一个支持组件的实例,然后在某个时候给它一个对其属性的引用。在稍后调用的方法中,例如 encode 方法,您应该可以访问它们。

    【讨论】:

    • 嗨,迈克。谢谢。我试试看encode方法中的属性是否可以访问。
    • 你好。是的。可以在 encode[All|Begin|End|Childen](FacesContext context) 方法中访问这些属性。非常感谢你的帮助。但是我仍然想知道如果我想在渲染组件之前以某种方式使用这些方法中的一种是否是访问属性的正确位置。在@ManagedBeans 中,我为此目的使用@PostConstruct 方法,但这似乎不适用于@FacesComponent 类。
    • 也许有人应该为此提出请求? @PostConstruct @FacesComponent 会很棒!
    猜你喜欢
    • 2012-10-09
    • 2011-10-16
    • 2011-11-10
    • 2015-02-26
    • 2012-06-19
    • 2011-11-27
    • 2011-07-24
    • 2012-04-29
    • 2013-08-22
    相关资源
    最近更新 更多