我遇到了JSF 2.0的HtmlDataTable问题。在我的网页上,我有一个h:dataTable和一些其他内容,只有在用户登录后才能呈现。
从数据库加载HtmlDataTable的内容。虽然在用户未登录时不呈现h:dataTable,但仍会评估内容。
以下是网页的代码:
<h:panelGroup rendered="#{userBean.loggedIn}">
<h:dataTable value="#{xxxBean.allXxx}"
var="c">
<h:column>
<h:outputText value="#{c.name}"/>
</h:column>
</h:dataTable>
<!-- some other content -->
</h:panelGroup>
在getAllXxx()方法中,我正在记录方法的调用。但是如果没有渲染h:dataTable(以及所有其他内容),仍会调用getAllXxx()方法。
我尝试使用c:if而不是h:panelGroup。这可行,但随后我在登录过程中遇到问题,所以这不是合适的解决方案。
有谁知道如何解决这个问题?提前谢谢。
答案 0 :(得分:1)
使用以下SSCCE无法在Tomcat 7.0.5上的Mojarra 2.0.3上重现您的问题。
test.xhtml
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>SO question 4774516</title>
</h:head>
<h:body>
<h:panelGroup rendered="#{param.show}">
<h:dataTable value="#{bean.list}" var="item">
<h:column>#{item}</h:column>
</h:dataTable>
</h:panelGroup>
</h:body>
</html>
com.example.Bean
package com.example;
import java.util.Arrays;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean
@RequestScoped
public class Bean {
private List<String> list = Arrays.asList("one", "two", "three");
public List<String> getList() {
System.out.println("getList() called");
return list;
}
}
打开http://localhost:8080/playground/test.jsf不显示任何标准输出行。打开http://localhost:8080/playground/test.jsf?show=true会显示它们。
您的问题是由其他原因造成的。要么它是您的JSF实现中的错误,要么只是误解了该过程。例如,它可以实际上是一个回发请求,其中在应用请求值阶段期间调用了getter,并且#{userBean.loggedIn}
的结果仅在调用动作阶段期间被更改。或者吸气剂被其他东西调用。