1

我正在运行 Tomcat 7、JSF 2.2、OpenWebBeans 1.6、Omnifaces 1.8 和 Richfaces 3。我最近添加了 Omnifaces CombinedResourceHandler,并注意到它会导致调用数据表的 value 方法,尽管该方法位于未渲染块中,当页面上的表单已提交。

例子:

<h:body styleClass="bdyPage">
    <h:form id="form">
        <h:commandButton value="test button" />
        <h:panelGroup rendered="false">
            <h:outputText value="#{testBean.getTestString()}" />
            <h:dataTable id="testtable22" value="#{testBean.strings}" var="str"
                rowClasses="odd, even">
                <h:column>
                    <f:facet name="header">
                        test
                    </f:facet>
                    <h:outputText value="str" />
                </h:column>
            </h:dataTable>
        </h:panelGroup>
    </h:form>
</h:body>

我有一个简单的测试bean:

@Named("testBean")
@RequestScoped
public class TestBean1 implements Serializable {

  private static final long serialVersionUID = 1L;

    public String getTestString() {
        System.out.println("Test string");
        return "test string";
    }

    public List<String> getStrings() {
        List<String> strings = new ArrayList<String>();
        strings.add("hej");
        strings.add("hejsan");
        strings.add("hej hej!");
        System.out.println("getting strings");
        return strings;
    }

}

通常,不会调用 getStrings(),因为 panelGroup 设置为不呈现。但是,当我同时运行Omnifaces CombinedResourceHandler 和 Richfaces 时,多次按下提交按钮时会调用 getStrings()。这似乎只影响 h:dataTable 组件的 value 属性。其他 el 表达式不会被调用。

我建立了一个带有以下 maven 依赖项的小示例项目:

    <dependencies>
    <dependency>
        <groupId>org.richfaces.ui</groupId>
        <artifactId>richfaces-components-ui</artifactId>
        <version>4.3.7.Final</version>
    </dependency>
    <dependency>
        <groupId>org.richfaces.core</groupId>
        <artifactId>richfaces-core-impl</artifactId>
        <version>4.3.7.Final</version>
    </dependency> 
<!--    <dependency>  
    <groupId>org.richfaces</groupId>  
    <artifactId>richfaces</artifactId>  
  <version>5.0.0.Alpha3</version>  
</dependency>  
     -->
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.0.0.GA</version>
        <type>jar</type>
        <classifier>sources</classifier>
    </dependency>
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.0.0.GA</version>
        <type>jar</type>
    </dependency>
    <dependency>
        <groupId>org.omnifaces</groupId>
        <artifactId>omnifaces</artifactId>
        <version>1.8</version>
    </dependency>
    <dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.openwebbeans</groupId>
        <artifactId>openwebbeans-impl</artifactId>
        <version>1.6.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.openwebbeans</groupId>
        <artifactId>openwebbeans-jsf</artifactId>
        <version>1.6.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.openwebbeans</groupId>
        <artifactId>openwebbeans-el22</artifactId>
        <version>1.6.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.openwebbeans</groupId>
        <artifactId>openwebbeans-web</artifactId>
        <version>1.6.2</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.faces</artifactId>
        <version>2.2.6</version>
    </dependency>
</dependencies>

我没有在项目或测试页面上使用任何richfaces 组件。我只是将 Richfaces 作为依赖项包括在内。我没有任何组合在一起的javascript。如果我禁用 Omnifaces CRH 或从 pom 文件中删除 Richfaces,它会按预期工作。

我尝试使用 Richfaces 5,但在那里我得到了类似的行为(尽管 getter 被调用的次数更少)。我没有尝试过最新的 Omnifaces 版本,因为它与 OWB 1.6 不兼容。

另一个有趣的地方可能是,如果我用 JSTL 包围数据表<c:if>,它就可以工作。但我不想这样做,显然......

我在这里做错了吗?这些版本的 Omnifaces CRH 和 Richfaces 之间是否存在兼容性问题?

编辑:从数据表的 getter 调用堆栈

TestBean1.getStrings() line: 33 
TestBean1$$OwbNormalScopeProxy0.getStrings() line: not available    
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]  
NativeMethodAccessorImpl.invoke(Object, Object[]) line: not available   
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: not available   
Method.invoke(Object, Object...) line: not available    
BeanELResolver.getValue(ELContext, Object, Object) line: 87 
DemuxCompositeELResolver._getValue(int, ELResolver[], ELContext, Object, Object) line: 176  
DemuxCompositeELResolver.getValue(ELContext, Object, Object) line: 203  
AstValue.getValue(EvaluationContext) line: 183  
ValueExpressionImpl.getValue(ELContext) line: 185   
WrappedValueExpression.getValue(ELContext) line: 70 
TagValueExpression.getValue(ELContext) line: 109    
ComponentStateHelper.eval(Serializable, Object) line: 194   
ComponentStateHelper.eval(Serializable) line: 182   
HtmlDataTable(UIData).getValue() line: 732  
HtmlDataTable(UIData).getDataModel() line: 1822 
HtmlDataTable(UIData).setRowIndexWithoutRowStatePreserved(int) line: 484    
HtmlDataTable(UIData).setRowIndex(int) line: 473    
HtmlDataTable(UIData).visitColumnsAndColumnFacets(VisitContext, VisitCallback, boolean) line: 2104  
HtmlDataTable(UIData).visitTree(VisitContext, VisitCallback) line: 1446 
HtmlPanelGroup(UIComponent).visitTree(VisitContext, VisitCallback) line: 1701   
HtmlForm(UIComponent).visitTree(VisitContext, VisitCallback) line: 1701 
HtmlForm(UIForm).visitTree(VisitContext, VisitCallback) line: 371   
HtmlBody(UIComponent).visitTree(VisitContext, VisitCallback) line: 1701 
UIViewRoot(UIComponent).visitTree(VisitContext, VisitCallback) line: 1701   
FaceletViewHandlingStrategy.locateComponentByClientId(FacesContext, String) line: 2082  
FaceletViewHandlingStrategy.reapplyDynamicRemove(FacesContext, ComponentStruct) line: 2174  
FaceletViewHandlingStrategy.reapplyDynamicActions(FacesContext) line: 2116  
FaceletViewHandlingStrategy.buildView(FacesContext, UIViewRoot) line: 966   
RenderResponsePhase.execute(FacesContext) line: 99  
RenderResponsePhase(Phase).doPhase(FacesContext, Lifecycle, ListIterator<PhaseListener>) line: 101  
LifecycleImpl.render(FacesContext) line: 219    
FacesServlet.service(ServletRequest, ServletResponse) line: 647 
ApplicationFilterChain.internalDoFilter(ServletRequest, ServletResponse) line: 305  
ApplicationFilterChain.doFilter(ServletRequest, ServletResponse) line: 210  
StandardWrapperValve.invoke(Request, Response) line: 222    
StandardContextValve.invoke(Request, Response) line: 123    
NonLoginAuthenticator(AuthenticatorBase).invoke(Request, Response) line: 472    
StandardHostValve.invoke(Request, Response) line: 171   
ErrorReportValve.invoke(Request, Response) line: 99 
AccessLogValve.invoke(Request, Response) line: 936  
StandardEngineValve.invoke(Request, Response) line: 118 
CoyoteAdapter.service(Request, Response) line: 407  
Http11Processor(AbstractHttp11Processor).process(SocketWrapper<S>) line: 1004   
Http11Protocol$Http11ConnectionHandler(AbstractProtocol$AbstractConnectionHandler).process(SocketWrapper<S>, SocketStatus) line: 589    
JIoEndpoint$SocketProcessor.run() line: 312 
ThreadPoolExecutor(ThreadPoolExecutor).runWorker(ThreadPoolExecutor$Worker) line: not available 
ThreadPoolExecutor$Worker.run() line: not available 
TaskThread(Thread).run() line: not available [local variables unavailable]  
4

1 回答 1

1

首先,getter 方法不应该执行业务逻辑:我应该如何以及何时从数据库中为 h:dataTable 加载模型。一旦您将 getter 方法修复为仅返回属性,就完全没有必要担心它被多次调用。

回到观察到的行为,当代码执行没有和/或<h:dataTable>时,可能会在“意外”时间调用未渲染的 getter 方法。本身并没有这样做。至少,不是直接的。它只操作组件树中的组件资源。然而,它有一个 RichFaces 特定的hack,以便提取其本土资源库方法。尽管如此,它的源代码似乎并没有在任何地方触发。要确定真正的原因,只需在 getter 方法上放置一个调试断点,并探索负责调用的调用堆栈。UIComponent#visitTree()VisitHint#SKIP_UNRENDEREDVisitHint#SKIP_ITERATIONCombinedResourceHandlervisitTree()

至于明显的 JSTL<c:if>厌恶(“显然”这个词在这里有点太强了),这是值得阅读的:JSTL in JSF2 Facelets... 有意义吗?

也就是说,最新的 OmniFaces 1.x 版本是 1.8.3,而不是 1.8。另见下载部分。将 Mojarra 升级到最新版本(截至目前为 2.2.12)也是值得的。

于 2015-12-14T16:49:24.223 回答