Use Facelets composition without any template - jsf-2

I watched the facelet template example and I could see the official tutorial, too, in both cases I can see the tuts make using <ui:composition template="/layout.xhtml"> with template support only; But is there a way not to include template if I want some a very simple facelet for example?
I tried to ignore the template attribute but then I have java.lang.StackOverflowError being thrown :( So the jsf 2.0 makes me to have templates to be included anyway...
So my question is... is there a way writing facelets without templates references?
Thanks

Yes, certainly. Just use the same XHTML composition as the master template /layout.xhtml itself, but then without any <ui:insert>. Instead, just put the desired content straight in there.
For example, /page.xhtml:
<!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>
<title>Some title</title>
</h:head>
<h:body>
<h1>Some heading</h1>
<p>Plain Facelets page without template!</p>
</h:body>
</html>
Open this directly as /page.xhtml in browser.

Related

JSF2 custom component issue

I have created a custom component which is used in xhtml view and it takes the attribute value and print it(like helloworld in below code), in JSF 2.1, using Netbeans8.1 and glassfish4.1. I have created a custom component by extending UIComponentBase overridden family and encode begin and custom component tag class by extending UIComponentELTag overriding componentType and rendererType methods. I have defined a tag library for it test.taglib.xml and registered it in web.xml.
Below is the view code.
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:body>
<h2>JSF Custom Input FacesComponent Example</h2>
<h:form>
<ui:composition
xmlns:custom="http://packt.net/cookbook/components"
xmlns:ui="http://java.sun.com/jsf/facelets">
<custom:testInput helloworld="test component!!"/>
</ui:composition>
</h:form>
</h:body>
</html>
The problem is - it is only displaying the html value and not displaying the custom component attribute value.
The same example works for when I tried for jsp view (with tld file).
I tried using custom component tag directly or as child of ui:composition but no luck.
I put a break point in the custom component and tried to debug, but control is not even going to the break point.
Since control is not going to break point, so does it means my custom component is not getting registered by the servlet? If so them please tell me what I am doing wrong.
I am new to JSF so please tell me if I am missing any point.
I tried the same with JSF2.2.7 but that also exhibits the same problem.
I cannot find the root cause but got my problem solved. I tried the same code with JSF 2.2.7 jar but I used annotations instead.
As mentioned in the problem that it didn't worked with 2.2.7, was because I was using old fashion code, like creating taglib and updating it in deployment descriptor.

Define group of taglib definitions xmlns

Most of my .xhtml facelets starts with something like this
<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"
xmlns:p="http://primefaces.org/ui"
xmlns:of="http://omnifaces.org/functions"
xmlns:comps="http://java.sun.com/jsf/composite/comps">
Is it possible to define this as a group in a common place and have all the files point to it?
so that when I decide to start using a new library, it can be easily accessed in any file.

Are XML namespaces supposed to be effective in a template client?

This is no major issue, but I just want to upgrade my understanding of the specifications.
I started using ui:composition as the root element of my JSF pages. When my template starts using tag libraries that are NOT in the client, I get warning messages on the rendered page.
So the Netbeans wizard gives me this:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
template="./template.xhtml">
<ui:define name="content">
<h1>A simple page</h1>
<p>
Hi there.
</p>
</ui:define>
And the rendered page shows:
Warning: This page calls for XML namespace declared with prefix p but no taglibrary exists for that namespace.
Warning: This page calls for XML namespace declared with prefix h1 but no taglibrary exists for that namespace.
So I add the xmlns declarations and everything is fine, but is this the way that it is supposed to work? If not is the discontinuity with the JSF specification, XML specification, or just the Mojarra implementation, or none of the above?
but is this the way that it is supposed to work?
Yes. JSF (and in particular Facelets) leverages plain old XML. Both the template client and the template definition are XML documents, so both need to specify their elements' namespaces in accordance with the XML specification. Put differently, the XML parser doesn't know that the document it parses will later be interpreted as a template client definition by JSF, but parses it as "just another document".
It's similar to normal Java code. If you want to use any classes, you must declare by importing their library. XML namespace is just something you need to include to declare the libraries of tags that you are going to use. It's absolutely normal that XML namespace is effective in template clients.
Besides, if you use <ui:composition> as your root element, you should not use normal HTML tags. Instead, you should change them to the equivalent JSF tags. If you need to use HTML tags, use <html> as your root element and the warning will be gone.

Spring tags don't work in JSF xhtml files

I am new to JSF and Spring. I am trying to use Spring Security with JSF. I have designed an XHTML page as below:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:spring="http://www.springframework.org/tags"
xmlns:form="http://www.springframework.org/tags/form">
<head>
<title>JSF Test</title>
</head>
<body>
<f:view>
<p>This page should be authenticated!</p>
Logout
<div>Test: <spring:url value="/j_spring_security_logout" htmlEscape="true" /></div>
</f:view>
</body>
</html>
I am trying to add a logout link using the spring:url tag. However, firstly this gives an error that href attribute cannot contain <. To troubleshoot this I removed the a tag and used the div to test whether I am able to get the desired value from the spring:url tag. I found that the spring:url tag is not being parsed and appears as it is in the source of the generated page.
Instead of using XHTML page if I use JSP page with taglibs instead of xmlns, everything works fine. I am not able to understand why it is not working with XHTML files.
My Faces Servlet is mapped to .jsf and springSecurityFilterChain is mapped to /* (without the space in between). I tried mapping springSecurityFilterChain to *.jsf and that too doesn't help.
Facelets is a XML based view technology. Nesting tags as an attribute of another tag is invalid XML. All <spring:url> effectively does is prepending the context path, you could also do it yourself:
Logout
The HTML escaping is not relevant since you are not passing any parameters through it.

JSP equivalent of ASP.NET MVC's partial views?

What is the JSP equivalent of ASP.NET MVC's partial views?
I'd like to separate some complicated view logic out of a page into a separate page that only handles that logic. How can I render that page as part of my page?
There isn't. JSP is not a fullworthy equivalent of ASP.NET MVC. It's more an equivalent to classic ASP. The Java equivalent of ASP.NET MVC is JSF 2.0 on Facelets.
However, your requirement sounds more like as if you need a simple include page. In JSP you can use the <jsp:include> for this. But it offers nothing more with regard to templating (Facelets is superior in this) and also nothing with regard to component based MVC (there you have JSF for).
Basic example:
main.jsp
<!DOCTYPE html>
<html lang="en">
<head>
<title>Title</title>
</head>
<body>
<h1>Parent page</h1>
<jsp:include page="include.jsp" />
</body>
</html>
include.jsp
<h2>Include page</h2>
Generated HTML result:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Title</title>
</head>
<body>
<h1>Parent page</h1>
<h2>Include page</h2>
</body>
</html>
See also:
What is the Java alternative to ASP.NET/PHP?
Analogues of Java and .NET technologies/frameworks
What is the difference between JSF, JSP and Servlet?

Resources