我正在运行Tomcat 7,JSF 2.2,OpenWebBeans 1.6,Omnifaces 1.8和Richfaces 3.我最近添加了Omnifaces CombinedResourceHandler,并注意到它会导致调用数据表的值方法,尽管该方法位于未渲染的块中,当提交页面上的表单时。
示例:
<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作为一个依赖项。我没有任何合并的javascripts。如果我禁用Omnifaces CRH或从pom文件中删除Richfaces,它会按预期工作。
我尝试使用Richfaces 5,但我在那里得到了类似的行为(尽管getter被调用次数较少)。我没有尝试过最新的Omnifaces版本,因为它与OWB 1.6不兼容。
Anoter兴趣点可能是如果我用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]
答案 0 :(得分:1)
首先,getter方法不应该执行业务逻辑:How and when should I load the model from database for h:dataTable。一旦你修改你的getter方法来单独返回属性,多次调用它的关注变得完全没必要。
回到观察到的行为,可以在&#34;意外&#34;处调用未渲染的<h:dataTable>
的getter方法。代码执行UIComponent#visitTree()
而没有VisitHint#SKIP_UNRENDERED
和/或VisitHint#SKIP_ITERATION
的时间。 CombinedResourceHandler
本身并没有这样做。至少,不是直接的。它仅操纵组件树中的组件资源。然而,它具有特定的RichFaces hack,以便提取其本土资源库方法。尽管如此,它的source code似乎无法在任何地方触发visitTree()
。要确定真正的原因,只需在getter方法上放置一个调试断点,并探索负责该调用的调用堆栈。
至于明显的JSTL <c:if>
厌恶(单词&#34;显然&#34;这里有点太强了),这是可读的食物:JSTL in JSF2 Facelets... makes sense?
也就是说,最新的OmniFaces 1.x版本是1.8.3,而不是1.8。另请参阅download部分。将Mojarra升级到最新版本也是值得的,截至目前的2.2.12。