【问题标题】:Managed Bean as Facelet parameter lets composite component prevent resolving托管 Bean 作为 Facelet 参数让复合组件阻止解析
【发布时间】:2013-12-11 17:29:30
【问题描述】:

在给定的情况下,我想使用具有不同 ManagedBeans 的 facelet,因此相关的 action-bean 作为参数给出:

<?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:body>
    <ui:include src="ratings.xhtml" >
      <ui:param name="createAction" value="#{myOneCreateAction}" />
      <ui:param name="ratings" value="#{context.ratings}" />
    </ui:include>
</h:body>
</html>

我将创建操作作为参数value="#{myOneCreateAction}"

在这个 facelet 中有一个组件,在其他页面上也被多次使用 - 所以我尝试在一个复合组件中重构它。

<html xmlns="http://www.w3.org/1999/xhtml" 
  xmlns:ui="http://java.sun.com/jsf/facelets" 
  xmlns:h="http://java.sun.com/jsf/html" 
  xmlns:f="http://java.sun.com/jsf/core" 
  xmlns:io="http://java.sun.com/jsf/composite/inoutComponents">
<ui:composition>
    <rich:dataTable id="ratingTblId" 
    value="#{ratings}" 
    var="rating">
        <rich:column>
             <io:removeButton
                 id="removeButton"
                 actionMethod="#{createAction.removeRating}"
                 immediate="true"
                 render=":#{rich:clientId('ratingTblId')}" />

             <h:commandButton
                 id="removeButton2"
                 actionListener="#{createAction.removeRating}"
                 immediate="true" >                    
                    <f:ajax render="ratingTblId" />
              </h:commandButton>
        </rich:column>
</rich:dataTable>
</ui:composition>
</html>

看看,方法是如何以actionMethod="#{createAction.removeRating}" 的形式提供给组件的。该组件本身如下所示:

<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:f="http://java.sun.com/jsf/core"
    xmlns:cc="http://java.sun.com/jsf/composite">
    <!-- INTERFACE -->
    <cc:interface>
        <cc:attribute 
            name="actionMethod" 
            targets="remove" 
            method-signature="void f(javax.faces.event.ActionEvent)"/>
        <cc:attribute name="render" required="false" />
    </cc:interface>

    <!-- IMPLEMENTATION -->
    <cc:implementation> 
        <h:commandButton 
           id="remove"
           actionListener="#{cc.attrs.actionMethod}" 
           onclick="if (!confirm('Do you really?')) { return false; }">
           <f:ajax execute="@this" render="#{cc.attrs.render}" />
        </h:commandButton> 
    </cc:implementation>
</ui:composition>

最后但并非最不重要的一点是托管 bean

Name("myOneCreateAction")
@Scope(ScopeType.CONVERSATION)
public class MyOneCreateAction {
   ...
   public void removeRating(ActionEvent ev) {
        // do something
   }
   ...
}

令人惊讶的是,当 removeButton2 正确跳转到正确的函数时,复合组件版本返回一个

javax.faces.event.AbortProcessingException: Target Unreachable, 
      identifier 'createAction' resolved to null

相反。我正在使用 Mojarra JSF 2.1.26 和 Seam 2.3.1.CR1。没有嵌套的复合组件。将复合组件参数替换为#{myOneCreateAction.removeRating} 时,效果如预期。

以前有人见过吗?我瞎了吗?任何已知的解决方法...?提前致谢!

【问题讨论】:

  • 很好的解释和组成。第一次命中做得很好 ;-) 顺便问一下,你看过that 吗?
  • 嗨@XtremeBiker,感谢您的积极反馈。嗯,是的,在写这个问题之前用它作为资源。调试了整个方法,并希望至少跳进去。但是没有,没有发生。 :-(

标签: jsf jsf-2 facelets composite-component


【解决方案1】:

作为一种变通方法,我重写了组件以将操作 bean 和操作方法作为两个单独的参数来解决它们,如下所示

xhtml:

<io:removeButton
             id="removeButton"
             actionBean="#{createAction}"
             actionMethod="removeRating"
             immediate="true"
             render=":#{rich:clientId('ratingTblId')}" />

复合组件:

<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:f="http://java.sun.com/jsf/core"
xmlns:cc="http://java.sun.com/jsf/composite">
<!-- INTERFACE -->
<cc:interface>
    <cc:attribute name="actionBean" />
    <cc:attribute name="actionMethod" />
    <cc:attribute name="render" required="false" />
</cc:interface>

<!-- IMPLEMENTATION -->
<cc:implementation> 
    <h:commandButton 
       id="remove"
       action="#{cc.attrs.actionBean[cc.attrs.actionMethod]}" 
       onclick="if (!confirm('Do you really?')) { return false; }">
       <f:ajax execute="@this" render="#{cc.attrs.render}" />
    </h:commandButton> 
</cc:implementation>
</ui:composition>

还将操作方法​​签名更改为返回 String 而不是 void。这看起来不再那么超级性感了,但是很有效。 :-/

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,发现它已在 2.2.15 版本中修复: https://github.com/javaserverfaces/mojarra/issues/4271

    【讨论】:

      猜你喜欢
      • 2011-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-29
      相关资源
      最近更新 更多