0

I am a beginner programming Java and I am doing a project using primefaces. I want to include another XHTML page in an XHTML page. The include page is in /WEB-INF/facelets/include.xhtml (It has some data from a Managed Bean)

In my "page.xhtml", at first, I put this line inside <ui:define name="content">:

<ui:include src="WEB-INF/facelets/include.xhtml" /> 

But, it does not work.

Later, I tried to do this inside <ui:define name="content">

<ui:include src="WEB-INF/facelets/include.xhtml">
    <ui:param name="fullName" value="#{identityInformationBean.fullName}" />
</ui:include>

And in the "include.xhtml":

<h:outputText
    rendered="#{fullName!=null}"
    value="#{fullName}" />

But, it does not work too. Nevertheless, if I do this:

On "page.xhtml"

<ui:include src="WEB-INF/facelets/include.xhtml">
    <ui:param name="fullName" value="Helen" />
</ui:include>

The "include.xhtml" receives the information correctly.

I'd tried to registering the include file as a tagfile, as suggest here How to include another XHTML in XHTML using JSF 2.0 Facelets? But, it does not work.

Any idea to solve this problem? Thanks!

This is a piece of code from "include.xhtml":

<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

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

    <h:outputText
        rendered="#{identityInformationBean.fullName!=null}"
        value="#{identityInformationBean.fullName}" />
        
</ui:composition>

This is a piece of code from "page.xhtml":

<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

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

    <ui:define name="content">
    
        <h2>
            <h:outputText value="Identity Information"/>
        </h2>
        
    </ui:define>

</ui:composition>
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Elena
  • 1
  • 2
  • 1
    It's very difficult to infer and understand what exactly you mean with "does not work". It seems that you're talking about displaying a specific bean property in the include file? In other words, the include action works perfectly fine (i.e. when you add some random text such as "test" to the include file, then it appears there where you expect it to appear), but the variables are just not resolved the way you expect? Please be less ambiguous and more specific in describing the problem. "It does not work" is the worst problem description you can think of. – BalusC Aug 26 '20 at 08:56
  • @BalusC thanks for your answer. Well, my include file (include.xhtml) contains some beans properties and, when I tried to include "include.xhtml" in my page.xhtml (by the ways that I mentioned) this properties not appears. – Elena Aug 26 '20 at 09:13

1 Answers1

0

I'm not sure why you need ui:param. Think of the include file as just saving you the trouble of typing that included code into all the pages that might use it. You don't need to pass it a parameter.

What about using a single line of code: <ui:include src="WEB-INF/facelets/include.xhtml"/> And the include file would have <h:outputText value="#{identityInformationBean.fullName}"/>

Tom T
  • 263
  • 1
  • 2
  • 11
  • Yes, I included this line of code in "page.xhtml" and in the "include.xhtml": and when I load the page, this parameter and others that I have did not appear. – Elena Aug 26 '20 at 12:06
  • Perhaps you should put a breakpoint in your bean at getFullName. Maybe it is null and you don't know it. – Tom T Aug 26 '20 at 14:22
  • Thanks @Tom T...But, if I copy the code from "include.xhtml" and paste it in "page.xhtml" the parameters appear. But, I need those parameters in "include.xhtml" because I want to reuse them on other pages – Elena Aug 26 '20 at 15:35
  • Is page.xhtml your main page, or do you display it when the user does something? – Tom T Aug 26 '20 at 16:46
  • I display it when user search something. – Elena Aug 27 '20 at 06:10
  • Normally, in Primefaces, you need to update the form once you've called your backing bean to find data. So if your main form has ", and your user presses a button to search, the search button should update the form. Example: `` – Tom T Aug 27 '20 at 13:42
  • Thanks Tom! Yes, I have an update in my search button. I am looking for a solution to solve my problem...Thanks again! – Elena Aug 27 '20 at 14:38
  • This is a very simple Primefaces task, so I don't know what your problem is without knowing what your pages look like. Is page.xhtml your main page? Where is the tag, and what id does it have? And what does the code for your button look like? – Tom T Aug 27 '20 at 16:10
  • Well, I have 3 pages in my project with different and when the user presses a button to search, those pages show their information, but they have a with a who show the same bean properties. That's why I am trying to have an "include.xhtml" with the data that they have in common. – Elena Aug 28 '20 at 06:42
  • To test, I extracted the repeating code from one of the pages (example: "page.xhtml") to include.xhtml, and I tried to include it as you said, with and when I run "page.xhtml" the plain texts are displayed, example, "Full name:" but the #{identityInformationBean.fullName} is not displayed. – Elena Aug 28 '20 at 06:42
  • Ah, I see. What you can try is to put ` ` around your include. Then, your search button should have `update="userInfo"` – Tom T Aug 28 '20 at 17:16
  • Solved! :) Thanks Tom for your help! Finally, I register the include file in my ".taglib.xml" and then on "page.xhtml" I used it as below: Inside "include.xhtml" bean properties are called like: – Elena Aug 31 '20 at 11:21