从辅助bean获取JSF 2自定义组件的动态参数

时间:2012-10-31 16:57:36

标签: jsf dynamic parameters components el

我正在使用(简单)JSF 2自定义组件。

我的意思是我使用的taglib.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<facelet-taglib id="sentest"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"
    version="2.0"
>
    <namespace>http://www.senat.fr/taglib/sentest</namespace>

[...]
    <tag>
        <description>
            <![CDATA[
            À COMPLÉTER
            ]]>
        </description>
        <tag-name>senMandats</tag-name>
    <source>tags/sen/senMandats.xhtml</source>
        <attribute>
            <description>
                <![CDATA[
        Identifiant unique.
        ]]>
            </description>
            <name>id</name>
            <required>true</required>
            <type>java.lang.String</type>
        </attribute>
        <attribute>
            <description>
                <![CDATA[
     Contexte de sélection du Sénateur.
    ]]>
            </description>
            <name>context</name>
            <required>true</required>
        </attribute>
    </tag>
</facelet-taglib>

使用ui成分定义组件:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html lang="fr"
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:composite="http://java.sun.com/jsf/composite"
    xmlns:p="http://primefaces.org/ui"
    xmlns:sen="http://java.sun.com/jsf/composite/sen"
    xmlns:fn="http://java.sun.com/jsp/jstl/functions"
    xmlns:sf="http://www.senat.fr/taglib/senfunctions"
    xmlns:st="http://www.senat.fr/taglib/sentest"
    xmlns:o="http://omnifaces.org/ui"
    xmlns:of="http://omnifaces.org/functions">
    <h:head/>
    <h:body>
        <ui:composition>
    [...]
        </ui:composition>
    </h:body>
</html>

除了之外,它可以很好地处理一个非常令人尴尬的问题。

我将动态el值传递给那些自定义组件。

示例:

<st:senMandats id="toto" context="#{selectionContext}"/>

selectionContext bean在别处定义。

在st:senMandats中,我以嵌套的方式使用其他自定义组件。类似的东西:

<st:listeMandats mandatContext="#{sensContext}" asen="#{selectionContext.selectedSen}"/>

listeMandat组件使用primefaces dataTable显示上下文中的一些列表。所以,我的代码如下:

<h:outputLabel value="listeMandats de #{asen.libelleLong}" styleClass="bigTitleMandats" rendered="#{not empty asen}" />
<p:dataTable id="tableMandatsSenatoriaux" value="#{mandatContext.asList}" /* lots of other parameters */>

当我在选择器中选择一个条目时,自定义组件会得到适当的更新。我可以看到h:outputLabel的值显示已正确更新,具体取决于选择。

当调用#{mandatContext.AsList}时,我正在从应用程序上下文中检索#{asen}并在返回请求的列表之前执行一些内部更新。问题出现了:如果在渲染#{asen.libelleLong}时#{asen}似乎没问题,我无法从支持bean获取更新的值。

我正在使用以下功能:

@SuppressWarnings("unchecked")
public static <T> T findBean(String name) {
    if ((name == null) || name.isEmpty())
        return null;
    FacesContext fc = FacesContext.getCurrentInstance();
    logger.debug("Retrieving #{" + name + "}");
    return (T) fc.getApplication().evaluateExpressionGet(fc,
            "#{" + name + "}", Object.class);
}
AsList方法中的

JSFUtils.findBean("#{asen}")

始终返回旧的,未更新的值。

我应该怎么做才能让我的bean可以访问更新的值?我尝试了http://www.mkyong.com/jsf2/access-a-managed-bean-from-event-listener-jsf/中mykong提出的解决方案4,5和6,但仍然没有得到正确的价值。

我是否被迫编写自定义组件类,派生自特定类?如果是这样,我是否也被迫编写渲染器?我想避免这种情况,因为我很欣赏在xhtml文件中使用布局。

提前致谢。

我正在使用:

  • PrimeFaces 3.4.1
  • CODI 1.0.5
  • OpenWebBeans 1.1.6
  • MyFaces 2.1.9
  • Tomcat 7.0.32 (编辑:将“额外问题”设置为另一个问题)

0 个答案:

没有答案
相关问题