问题
I have got a problem with the HtmlDataTable of JSF 2.0. On my web page, i have got a h:dataTable and some other content, which should only be rendered if the user is logged in.
The content of the HtmlDataTable is loaded from a database. Although the h:dataTable is not rendered when the user is not logged in, the content is still evaluated.
Here is the code of the web page:
<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>
In the getAllXxx() method I am logging the call of the method. But also if the h:dataTable (and all the other content) is not rendered, the getAllXxx() method is still called.
I tried to use c:if instead of the h:panelGroup. That would work, but then I get issues during the login-process, so this is no suitable solution.
Does anyone know how to fix this? Thanks in advance.
回答1:
Can't reproduce your problem on Mojarra 2.0.3 on Tomcat 7.0.5 with the following SSCCE.
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;
}
}
Opening http://localhost:8080/playground/test.jsf doesn't show any stdout lines. Opening http://localhost:8080/playground/test.jsf?show=true shows them.
Your problem is caused by something else. Either it's a bug in your JSF implementation or you just misinterpreted the procedure. It can for example actually be a postback request wherein the getter is been called during apply request values phase and the outcome of #{userBean.loggedIn}
is only changed during invoke action phase. Or the getter is called by something else.
来源:https://stackoverflow.com/questions/4774516/although-rendered-false-content-of-a-hdatatable-is-always-evaluated