Problem including another XHTML page in an XHTML page - jsf

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>

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}"/>

Related

Nested form concept in JSF? [duplicate]

This question already has answers here:
How to use <h:form> in JSF page? Single form? Multiple forms? Nested forms?
(2 answers)
Closed 6 years ago.
As per HTML standard nested form not allowed and JSF follow same thing but if we have design like below
MainPage.xhtml
<ui:composition template="../templates/home.xhtml">
<ui:define name="content">
<h:form>
<rich:panel>
..........................................
..........................................
</rich:panel>
<rich:panel>
<ui:insert name="createLinkTemplate">
<ui:include src="../pages/page.xhtml" />
</ui:insert>
</rich:panel>
<h:form>
</ui:define>
</ui:composition>
Content inside page.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich"
xmlns:fn="http://java.sun.com/jsp/jstl/functions">
<h:head>
</h:head>
<h:body>
<h:form id="formID">
<rich:panel>
..........................
............................
............................
</rich:panel>
</h:form>
</h:body>
</html>
is this come under the nested case ?
home-template.xhtml content
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich"
xmlns:c="http://java.sun.com/jsp/jstl/core">
...................................................
...................................................
...................................................
<rich:panel styleClass="tabs-main" id="tabsMain">
<h:form id="contentform">
<ui:insert name="content"/>
</h:form>
</rich:panel>
</h:body>
</html>
Along with that one more question First i am alnding to Main.xhtml page from here i am going to page.xhtml and when from there i am coming back all the value which i write in textbox or select from drop down gone and i got a empty form .
Forms are nested when they are nested in the resulting page - the shape of templates and includes has nothing to do with this.
Browser has no way of knowing where <form> tags come from. All it sees is that there are two form tags, one placed in the other.

jsf viewscoped bean - set values for every page(tabs)

I am sorry, this might sound really basic but if anyone could explain how to set explicit values for every tabs in the same browser window using the JSF viewscoped bean.
Please guide me to some article or working samples.
Kindly help, thanks in advance
Update:
auth-page.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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:f="http://java.sun.com/jsf/core"
xmlns:qumola="http://java.sun.com/jsf/composite/qumola"
xmlns:p="http://primefaces.org/ui">
<style type="text/css"/>
<h:panelGrid id="newGrid">
<ui:param name="labelResourceName"
value="#{userMenuState.selectedLeftMenuItem.menuLabel}" />
<h:form id="someForm">
<h:panelGrid id="topBannerId">
<font>#{msg[labelResourceName]}</font> <font
class="titleTextBar"> </font>
<font class="titleTextBar">
#{activityEditorManagedBean.selectedActivityBean.title}</font>
</h:panelGrid>
</h:form>
</h:panelGrid>
</ui:composition>
The value "activityEditorManagedBean.selectedActivityBean.title" in the above code is a viewscoped bean variable value but it has the same value in every tab it is opened in.

JSF Javascript failure - myfaces not defined

I'm working on a web solution using myfaces and primefaces. We have several pages in our solution, and some of the get this mystical error myfaces not defined because there is a javascript file that is not included on the page, namely the jsf.js file:
<script type="text/javascript" src="/driwkraft-hig/javax.faces.resource/jsf.js.xhtml?ln=javax.faces&stage=Development">
The error occurs when clicking on certain commandlinks on the page, where the generated javascript has been set to onClick="myfaces.oam.submitForm...
Now, I understand that this is something that the JSF framework adds to the page based on some mystical criteria which I fail to understand. There are pages that appear to be equal with regards to what elements are used, and it succeeds on one and fails on the other.
I have tried to create a test-file where I copied content of one of the failing pages and then removed parts until it would work to figure out what was the source of the problem, but this did not give me anything. My next hunch is that it might be some configuration-error somewhere. But I am completely and utterly blank as to where to start.
I understand I should probable add some code or xml in to this question, but I cannot post the entire solution so I think it is best to do so upon request. Would the web.xml be useful? How about the faces-config.xml?
Any guidance and thoughts are much appreciated!
Edit - adding template
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<ui:insert name="metadata" />
<h:head>
....
<title>
<ui:insert name="title">
....
</ui:insert>
</title>
</h:head>
<h:body>
....
<ui:insert name="content">
....
</ui:insert>
...
</h:body>
</html>
Edit - adding template client
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:dp="http://java.sun.com/jsf/composite/dapsys">
<ui:composition template="/templates/default.xhtml">
<ui:define name="title">TITLE</ui:define>
<ui:define name="metadata">
<f:metadata>
<f:viewParam name="customerId" value="#{CustomerController.customerId}">
</f:viewParam>
<f:viewParam name="edit" value="#{CustomerController.edit}">
</f:viewParam>
<f:viewParam name="activeTab" value="#{CustomerController.activeTab}">
</f:viewParam>
</f:metadata>
</ui:define>
<ui:define name="content">
...
</ui:define>
</ui:composition>
</html>
It's auto-included if there's a <h:head> in the template which automatically renders CSS/JS dependencies from UIViewRoot#getComponentResources() which are added via #ResourceDependency annotation on the JSF component.
Those problem symptoms suggests that there's no <h:head> in the template, but a plain <head>. Therefore JSF is unable to auto-include CSS/JS resource dependencies which in turn causes this JS error.
To fix this problem, just make sure that there's a <h:head> instead of <head> in the template.
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head> <!-- Look here. -->
<title>Blah</title>
</h:head>
<h:body>
<h1>Blah</h1>
<p>Blah blah.</p>
</h:body>
</html>

templating JSF 2.0 Primefaces

So I have:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:body>
<ui:composition template="template.xhtml">
<ui:define name="content">
<h:outputText value="Test!!!" />
</ui:define>
</ui:composition>
</h:body>
</html>
as my main page on my website and on template.xhtml:
<div id="content">
<h:panelGroup layout="block" styleClass="centercss">
<ui:insert name="content" />
</h:panelGroup>
</div>
...in the middle of the footer and the header views.
Now if I try to change the template="template.xhtml" to template.jsf it does not appear anywhere... the way it is right now I get my 'content' page perfectly in the middle of header and footer on the eclipse preview, but on the browser there's no content at all.
Im using primefaces3.1.1 and I have javax.faces-2.1.14 + jsf-api and jsf-impl, so I think its primefaces 3 and JSF 2.
What is the problem here ?
I think you are using facelets (templating) in a wrong way. You should'nt have html and body tags on your main page. The page that will use the template must be defined in a <ui:composition> tag, and the template shall define the page as a whole (html, body, head tags, etc).
Example:
index.html
<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.org/ui"
template="template.xhtml">
<ui:define name="content">
<h:outputText value="Test!!!" />
</ui:define>
</ui:composition>
template.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:body>
<div id="content">
<h:panelGroup layout="block" style="background-color: red;">
<ui:insert name="content" />
</h:panelGroup>
</div>
</h:body>
</html>

Ajax update dataTable from commandButton located in included file

I have such situation situation:
in file form.xhtml i have form used for filter some tables, i gave button "filter" defined here.
in file tableOne.xhtml, tableTwo.html, .. i have dataTable with id "filterTable", i've also included form.xhtml into this page. all pages with dataTables includes form.xhtml and all dataTables on them have the same id "filterTable"
what i want to do is to update dataTable[#filterTable] on after filter button is pressed using ajax. button is created by <p:commandButton>. if form and datatable are defined in the same file, all i need is simply add update="filterTable" to my command bytton. but if i add it to button defined in form.xhtml i get "Cannot find component with identifier" error.
is it possible to dynamic update dataTables from outside xhtml files included on page? if it is, how can i do it?
Example:
test.xhtml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body>
<h:form id="dtForm">
<p:dataTable id="dataTable" />
</h:form>
<ui:include src="/include.xhtml" />
</h:body>
</html>
include.xhtml:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body>
<h:form>
<p:commandButton value="button" update=":dtForm" />
</h:form>
</h:body>
</html>

Resources