How to get bean name dynamically [duplicate] - jsf

I have a Facelet that might be used in different applications.
I don't to copy it, but reuse it. I need to pass the backing bean that will manage the view as a parameter, as some logic may vary according to the application where it is used in.
I don't want to use a composite component, but just include the Facelet and specify which bean will manage the view. How can I achieve this?
Let me give an example:
<ui:composition template="/resources/common/templates/template.xhtml"
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:c="http://java.sun.com/jsp/jstl/core" xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich" xmlns:fn="http://java.sun.com/jsp/jstl/functions">
<ui:define name="content">
<!-- somehow establish the backing bean that will manage formView.xhtml -->
<!-- f:set assign="ParameterBean" value="#{Bean}" / -->
<ui:include src="formView.xhtml" />
</ui:define>
</ui:composition>
formView.xhtml :
<ui:composition template="/resources/common/templates/template.xhtml"
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:c="http://java.sun.com/jsp/jstl/core" xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich" xmlns:fn="http://java.sun.com/jsp/jstl/functions">
<ui:define name="content">
<h:outputText value="#{ParameterBean.texto}" />
</ui:define>
</ui:composition>

You can use <ui:param> for that. It needs to be nested in the <ui:include>.
<ui:include src="formView.xhtml">
<ui:param name="ParameterBean" value="#{Bean}" />
</ui:include>
Unrelated to the concrete problem, standard Java Naming Conventions state that instance variable names must start with lower case. You should change your code in such way that respectively parameterBean and #{bean} will be used.

Because I would have found it helpful yesterday, when I was looking for this, here is a simple version of how to do this, without the extraneous template, defines and namespaces:
File1.xhtml (the root tag doesn't matter)
<ui:include src="File2.xhtml">
<ui:param name="person" value="#{whatever_value_you_want_to_pass}" />
</ui:include>
File2.xhtml
<ui:composition ... xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets" ... >
<h:outputLabel value="#{person.name}" />
</ui:composition>
You can also nest further in the same manner.
File1.xhtml
<ui:include src="File2.xhtml">
<ui:param name="person" value="#{whatever_value_you_want_to_pass}" />
</ui:include>
File2.xhtml
<ui:composition ... xmlns:ui="http://java.sun.com/jsf/facelets" ... >
<ui:include src="File3.xhtml">
<ui:param name="name" value="#{person.name}" />
</ui:include>
</ui:composition>
File3.xhtml
<ui:composition ... xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets" ... >
<h:outputLabel value="#{name.length}" />
</ui:composition>

Related

Avoid naming container / region on nested composite component children with include

I'm having problems rerendering the relevant parts of my view without having to resort to #form or the like.
I have a composite component like this:
wrapper.xhtml:
<!DOCTYPE composition 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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:rich="http://richfaces.org/rich"
xmlns:a4j="http://richfaces.org/a4j">
<cc:interface componentType="compositeComponent">
<cc:attribute required="false" name="foo" />
</cc:interface>
<cc:implementation>
<a4j:region>
<h:panelGroup id="content" layout="block" styleClass="my-wrapper">
<h1>TITLE</h1>
<c:forEach items="#{element.children}" var="element">
<ui:include src="element.xhtml">
<ui:param name="level" value="0" />
</ui:include>
</c:forEach>
</h:panelGroup>
</a4j:region>
</cc:implementation>
</ui:composition>
and element.xhtml:
<!DOCTYPE composition 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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:rich="http://richfaces.org/rich"
xmlns:a4j="http://richfaces.org/a4j">
<h:outputLabel styleClass="level-#{level}">
#{element.label}
<a4j:ajax event="click"
listener="#{controller.updateElements(element)}"
execute="#this" render="#region" />
</h:outputLabel>
<c:if test="#{element.someTest}">
<c:forEach items="#{element.children}" var="element">
<ui:include src="element.xhtml">
<ui:param name="level" value="#{level + 1}" />
</ui:include>
</c:forEach>
</c:if>
</ui:composition>
Now the problem is, although I'm setting an <a4j:region> around the wrapper, and setting the render expression in the children to #region, it still only renders the single clicked upon child on rerender.
I searched the web and SO but couldn't find a reason for this, does the include implicitly create a namespace or naming container (which would be treated as a region?)? Couldn't find anything like this, but if so, how would I avoid this? I want the entire wrapper (and only that, not #form or #all or the like) to be rerendered upon clicking on a child.
What am I missing?
#region is only used for execution. The <a4j:region> doesn't generate any HTML so you cannot use it for rendering.

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.

How to include common content into multiple level template page

I am trying include a common page into a template but all I get is a blank page without error.
common.xhtml actually has the content that indicate in the template.xhtml. It seems the template.xhtml doesn't recognize the two level include.
template.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:f="http://java.sun.com/jsf/core"
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:c="http://java.sun.com/jstl/core"
xmlns:ub="http://jboss.com/products/seam/ub-taglib"
xmlns:rich="http://richfaces.ajax4jsf.org/rich">
<head>
<ui:insert name="css" />
<ui:insert name="header" />
</head>
<body>
<ui:insert name="body" />
</body>
</html>
custom.xhtml
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:s="http://jboss.com/products/seam/taglib"
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:rich="http://richfaces.ajax4jsf.org/rich"
xmlns:a4j="https://ajax4jsf.dev.java.net/ajax"
xmlns:c="http://java.sun.com/jstl/core"
template="template.xhtml">
<ui:define name="css">
<link rel="stylesheet" type="text/css" href="/custom.css/>
</ui:define>
<ui:include src="common.xhtml" />
</ui:composition>
common.xhtml
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:s="http://jboss.com/products/seam/taglib"
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:rich="http://richfaces.ajax4jsf.org/rich"
xmlns:a4j="https://ajax4jsf.dev.java.net/ajax"
xmlns:c="http://java.sun.com/jstl/core"
template="template.xhtml">
<ui:define name="header">
<h1>header</h1>
</ui:define>
<ui:define name="body">
<table><tr><td>Table</td></tr></table>
</ui:define>
</ui:composition>
This indeed won't work. The <ui:define> is supposed to be used in a template client (i.e. a page with <ui:composition template="...">), not in an include file (i.e. a page with <ui:composition> without template). You can however just "extend" from existing master templates.
Remove from custom.xhtml:
<ui:include src="common.xhtml" />
Change in common.xhtml
template="custom.xhtml"
And open common.xhtml instead of custom.xhtml in browser.
See also:
How to include another XHTML in XHTML using JSF 2.0 Facelets?
Unrelated to the concrete problem, to prevent the enduser form being able to open custom.xhtml or template.xhtml directly in browser, it's recommended to move them into the /WEB-INF folder. Further, are you aware of the <h:head> and <h:outputStylesheet> components? I suggest to make use of them. Also, having a <h1> to ultimately end up in <head> makes no sense. Perhaps you meant the <ui:insert name="header"> to be inside <body>? Further, you could easily put that <h1> in the template so that you don't need to repeat them in every template client.
/WEB-INF/templates/template.xhtml
<html ...>
<h:head>
</h:head>
<h:body>
<ui:insert name="header" />
<ui:insert name="body" />
</body>
</html>
/WEB-INF/templates/custom.xhtml (CSS file is placed in /resources folder)
<ui:composition ... template="/WEB-INF/templates/template.xhtml">
<ui:define name="header">
<h1><ui:insert name="custom-header" /></h1>
</ui:define>
<ui:define name="body">
<h:outputStylesheet name="custom.css" target="head" />
<ui:insert name="custom-body" />
</ui:define>
</ui:composition>
/page.xhtml
<ui:composition ... template="/WEB-INF/templates/custom.xhtml">
<ui:define name="custom-header">
header
</ui:define>
<ui:define name="custom-body">
<table><tr><td>Table</td></tr></table>
</ui:define>
</ui:composition>
See also:
Which XHTML files do I need to put in /WEB-INF and which not?
How to reference CSS / JS / image resource in Facelets template?

Facelets Parameter has no value when passed to another page

I have a case like this where I include one file in another. but the page is more complex..
I sometimes get #{param2} with no value while #{param1} with a value. I even named the param some random name just to make sure there's no other param anywhere else with the same name that could be overwriting it.
Why could this happen, a ui:param who's value is overwritten by blank somehow?
In the first xhtml we include another file:
<ui:include src="/myFile.xhtml">
<ui:param name="param1" value="stringvalue1" />
<ui:param name="param2" value="stringvalue2" />
</ui:include>
myFile.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:s="http://jboss.org/seam/faces"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:p="http://primefaces.org/ui">
#{param1}
#{param2}
</ui:composition>

Problem using include in Facelets

I have problems including a facelet template. I wanted to split some content up, so that I can reuse it somewhere else.
So I changed this code:
<!DOCTYPE html>
<ui:composition 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"
template="/layout/template.xhtml">
<ui:define name="head">
<title>Title</title>
</ui:define>
<ui:define name="header">
<h3>Header</h3>
</ui:define>
<ui:define name="content">
<table><tr><td>table</td></tr></table>
</ui:define>
</ui:composition>
To this:
<!DOCTYPE html>
<ui:composition 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"
template="/layout/template.xhtml">
<ui:define name="head">
<title>Title</title>
</ui:define>
<ui:include src="/admin/admin_generic.xhtml"/>
</ui:composition>
And inside admin-generic.xhtml I wrapped the code in a ui:composition.
<ui:composition 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">
<ui:define name="header">
<h3>Header</h3>
</ui:define>
<ui:define name="content">
<table><tr><td>table</td></tr></table>
</ui:define>
</ui:composition>
But nothing is shown. I just get a blank page, with no errors. Is it wrong using ui:composition? I have tried with ui:component but that didn't help either.
Update: According to my Facelets Essentials Guide, it says:
The ui:include tag can be used to include another Facelets file into your
document. It simply includes whatever source file you specify. You can
include any Facelets file that has ui:component or ui:composition tags
(which trim the content outside themselves) or simply a fragment of
XHTML or XML.
Is that what is going on? Is the content outside of the include trimmed away? How can I just include the page, without the content outside being trimmed?
The <ui:define> has to be placed in an <ui:composition> or <ui:decorate> with a template containing the appropriate <ui:insert> tags. You've moved it to an <ui:composition> without a template. No template means no content.
Technically, to achieve your requirement, you have to replace the <ui:include> by <ui:insert>.
<!DOCTYPE html>
<ui:composition
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="template.xhtml">
<ui:define name="head">
<title>Title</title>
</ui:define>
<ui:insert />
</ui:composition>
And declare the above page (I assume it as somepage.xhtml) as template in admin_generic.xhtml.
<!DOCTYPE html>
<ui:composition
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="somepage.xhtml">
<ui:define name="header">
<h1>Header</h1>
</ui:define>
<ui:define name="content">
<table><tr><td>table</td></tr></table>
</ui:define>
</ui:composition>
Note that you have to open admin_generic.xhtml in the browser instead. If your intent is to open somepage.xhtml in browser, then the <ui:define> really has to stay in somepage.xhtml. You can however replace the body of <ui:define> by a simple <ui:include>.
<!DOCTYPE html>
<ui:composition
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
template="template.xhtml">
<ui:define name="head">
<title>Title</title>
</ui:define>
<ui:define name="header">
<h1>Header</h1>
</ui:define>
<ui:define name="content">
<ui:include src="admin_generic.xhtml" />
</ui:define>
</ui:composition>
It allows for <ui:composition>, so you don't necessarily need to put the <table> to root.
<!DOCTYPE html>
<ui:composition
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets">
<table><tr><td>table</td></tr></table>
</ui:composition>
I solved this by removing the <ui:composition> and the <ui:define> and just adding the namespace directly in the <table> like this:
<table class="adminform" xmlns="http://www.w3.org/1999/xhtml"
xmlns:s="http://jboss.com/products/seam/taglib"
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:a="http://richfaces.org/a4j">
So now my page looks like this:
<ui:define name="content">
<ui:include src="/admin/admin_generic.xhtml" />
</ui:define>

Resources