使用动态ui:include in ui:在NullPointerException中重复结果

时间:2012-03-07 14:25:13

标签: jsf facelets uirepeat uiinclude

我想使用<ui:repeat><p:dialog><ui:include>一起创建动态对话框窗口。但是当我在下面尝试时有一个例外。

main.xhtml

<p:outputPanel id="windowsPanel" layout="block" style="width:100%;">
    <p:outputPanel rendered="#{mainView.dynamicWindows ne null}">
        <ui:repeat var="item"  value="#{mainView.dynamicWindows}">
            <p:dialog binding="#{item.dialog}">
                <ui:include src="#{item.includedWindowPath}" />
            </p:dialog>
        </ui:repeat>
    </p:outputPanel>
</p:outputPanel>

MainView.java

@ManagedBean(name = "mainView")
@SessionScoped
public class MainView extends BaseView {

private static final long serialVersionUID = -6291834350102049312L;

private List<Window> dynamicWindows;

@PostConstruct
public void init() {
    fillWindows();
}

private void fillWindows() {
    dynamicWindows = new ArrayList<Window>();

    for (int i = 0; i < 3; i++) {
        Window window = new Window("Header " + i);
        window.getDialog().setId("_dynamicWindow" + i);
        window.getDialog().setWidgetVar("_dynamicWindowWidget" + i);
        dynamicWindows.add(window);
    }
}

// getters & setters

例外:

javax.servlet.ServletException: /main.xhtml @33,42 binding="#{item.dialog}": Target Unreachable, identifier 'item' resolved to null
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:422)
    org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
    org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:369)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
    org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:381)
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:168)
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
    org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

2 个答案:

答案 0 :(得分:6)

<ui:include>是在视图构建期间运行的标记处理程序,而<ui:repeat>是在视图渲染时运行的UI组件。标记处理程序生成JSF UI组件树。 JSF UI组件生成HTML。它们不会同步运行。

因此,当<ui:include>运行时,#{item}指定的<ui:repeat var>在范围内根本不可用,因为<ui:repeat>尚未运行时间。您需要一个迭代标记处理程序而不是迭代的JSF UI组件。 JSTL <c:forEach>就是这样的。按<ui:repeat>替换<c:forEach>可以解决此问题。然而,可能具有不希望的副作用。由于您的具体功能要求尚不完全清楚,因此很难预先说明。

另见:

答案 1 :(得分:0)

如果你说有限的情况(比如4-5例)动态选择&amp;对ui:repeat中的项目进行包含,则可能值得这样做,因为如果要迭代的列表更大,它将使您无法使用c:forEach构建大型组件树。这将有条件地从几个案例中渲染出一个包含xhtml的案例。这些情况不会在组件树中重复多次,每次都在列表中重复。希望这应该提供更好的性能和还可以避免将JSF与JSTL混合使用。

    <ui:repeat var="item"  value="#{mainView.dynamicWindows}">
         <h:panelGroup rendered="#{booleancase1}">
            <ui:include src="case1.xhtml" />
         </h:panelGroup>

         <h:panelGroup rendered="#{booleancase2}">
            <ui:include src="case2.xhtml" />
         </h:panelGroup>

         <h:panelGroup rendered="#{booleancase3}">
            <ui:include src="case3.xhtml" />
         </h:panelGroup>

         <h:panelGroup rendered="#{booleancase4}">
            <ui:include src="case4.xhtml" />
         </h:panelGroup>
    </ui:repeat>