3

I am trying to create an application using JSF and Primefaces. Unfortunately, I have an issue about page templates and could not manage to work it out properly.

I have a page template like this. What I want is to load Left Menu and the Content with ajax request without loading whole page. However, when I click the page2 link, everything gets broken like this.

I tried different combinations of how to place form, include, composition tags but, it's still the same. Only change is my server response according to place of form tag. When I checked the response it seems alright. It does not try to load whole page and nothing seems wrong to me:

<?xml version='1.0' encoding='UTF-8'?>
<partial-response id="j_id1"><changes><update id="javax.faces.ViewRoot"><![CDATA[<script id="innerLayout_s" type="text/javascript">$(function(){PrimeFaces.cw(  "Layout","widget_innerLayout",{id:"innerLayout",widgetVar:"widget_innerLayout",west:{paneSelector:'#left',size:"200",resizable:false,closable:false},center:{paneSelector:'#innerContent',size:"auto",resizable:false,closable:false}},"layout");})   ;</script><div id="innerLayout"><div id="left" class="ui-layout-unit ui-widget ui-widget-content ui-corner-all ui-layout-west"><div class="ui-layout-unit-header ui-widget-header ui-corner-all"><span class="ui-layout-unit-header-title">Left    Menu</span></div><div class="ui-layout-unit-content ui-widget-content">
<form id="leftForm" name="leftForm" method="post" action="/Magician/pages/page2.xhtml" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="leftForm" value="leftForm" />
Page2 Left Menu
</form></div></div><div id="innerContent" class="ui-layout-unit ui-widget ui-widget-content ui-corner-all ui-layout-center"><div class="ui-layout-unit-content ui-widget-content">
<form id="contentForm" name="contentForm" method="post" action="/Magician/pages/page2.xhtml" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="contentForm" value="contentForm" />
Page2 Content
</form></div></div></div>]]></update><update id="j_id1:javax.faces.ViewState:0"><![CDATA[2499963408064928377:-8018685353649439725]]></update></changes></partial-response>

However, when I look the console, there is an error like below:

Uncaught TypeError: Cannot read property '0' of null    primefaces.js.xhtml?ln=primefaces&v=5.0:2
PrimeFaces.ajax.Utils.updateElement                     primefaces.js.xhtml?ln=primefaces&v=5.0:2
PrimeFaces.ajax.ResponseProcessor.doUpdate              primefaces.js.xhtml?ln=primefaces&v=5.0:2
PrimeFaces.ajax.Response.handle                         primefaces.js.xhtml?ln=primefaces&v=5.0:2
p.success                                               primefaces.js.xhtml?ln=primefaces&v=5.0:2
i                                                       jquery.js.xhtml?ln=primefaces&v=5.0:25
cd.fireWith                                             jquery.js.xhtml?ln=primefaces&v=5.0:25
cg                                                      jquery.js.xhtml?ln=primefaces&v=5.0:25
i

Here are my codes:

default.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui">
<h:head>
    <title>My Application</title>
</h:head>
<h:body>

    <h:outputStylesheet name="css/style.css" />

    <p:layout id="fullPageLayout" fullPage="true">

        <p:layoutUnit id="header" position="north" size="80">
            <h:form id="headerForm">
                <ui:insert name="headerUI">
                    <ui:include src="header.xhtml" />
                </ui:insert>
            </h:form>
        </p:layoutUnit>

        <p:layoutUnit id="content" position="center">
            <ui:insert name="contentUI" />
        </p:layoutUnit>

    </p:layout>

</h:body>
</html>

header.xhtml:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui">

    <p:tabMenu id="tabMenu" activeIndex="#{templateBean.activeIndex}">
        <p:menuitem value="page1" action="#{templateBean.navigateToPage(0)}" update=":contentForm,tabMenu" />
        <p:menuitem value="page2" action="#{templateBean.navigateToPage(1)}" update=":contentForm,tabMenu" />
    </p:tabMenu>

</ui:composition>

content.xhtml:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui">

    <p:layout id="innerLayout">

        <p:layoutUnit id="left" position="west" size="200" header="Left Menu">
            <h:form id="leftForm">
                <ui:insert name="leftUI" />
            </h:form>
        </p:layoutUnit>

        <p:layoutUnit id="innerContent" position="center">
            <h:form id="contentForm">
                <ui:insert name="centerUI" />
            </h:form>
        </p:layoutUnit>

    </p:layout>

</ui:composition>

index.xhtml:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui"
    template="templates/default.xhtml">

    <ui:define name="contentUI">
        <ui:include src="#{templateBean.activePage}.xhtml" />
    </ui:define>

</ui:composition>

page1.xhtml:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui"
    template="templates/content.xhtml">
    <ui:define name="leftUI">

        <h:outputText value="Page1 Left Menu" />

    </ui:define>
    <ui:define name="centerUI">

        <h:outputText value="Page1 Content" />

    </ui:define>
</ui:composition>

page2.xhtml:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui"
    template="templates/content.xhtml">
    <ui:define name="leftUI">

        <h:outputText value="Page2 Left Menu" />

    </ui:define>
    <ui:define name="centerUI">

        <h:outputText value="Page2 Content" />

    </ui:define>
</ui:composition>

TemplateBean.java:

package com.myapplication.pagebeans;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("request")
public class TemplateBean {

    private int activeIndex;
    private String activePage = "page1";

    public String navigateToPage(int index) {
        activeIndex = index;
        switch (index) {
            case 0:
                activePage = "page1";
                break;
            case 1:
                activePage = "page2";
                break;
            default:
                break;
        }

        return activePage;
    }

    public int getActiveIndex() {
        return activeIndex;
    }

    public String getActivePage() {
        return activePage;
    }

}

Sorry for the long question and images (site does not let me to load images because of my reputation) but, I tried give information as much as I can. Thanks in advance.

I searched a lot and already look at different questions like below ones:

Community
  • 1
  • 1
Aykut Akin
  • 143
  • 1
  • 1
  • 7

0 Answers0