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

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.

Related

Grails: how to set language dynamically in gsp or using js

I want to set language value of a gsp page dynamically . Currently I am just doing it using basic hardcoded value . I did find something with JS Onload event described here.
But I wanted to find something that is GSP driven . Is there any way ?
My current code looks like <html lang="en">
I think maybe you are thinking of this in a more complex way than it actuall is.
In grails you have your layouts/main.gsp which is your sitemesh.
The tag <html lang='en' is declared at the very top of this
If you simply edit this page and add the following:
<g:set var="locale" value="${session?.'org.springframework.web.servlet.i18n.SessionLocaleResolver.LOCALE'?:java.util.Locale.UK}"/>
<html lang="${locale?.language?:'en'}" class="no-js">
Then when I visit my site:
localhost:8080/?lang=ja_JP view source shows:
<html lang="ja" class="no-js">
You need to do that for each sitemesh that requires to do this - having a read about this property it seems it doesn't do much for the browser but may help non human things such as search engines.

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.

<ui:include> tag in JSF 2.0 not working

I am pretty new to JSF and facelets programming, I have followed the instructions in this link How to include another XHTML in XHTML using JSF 2.0 Facelets? to use the <ui:include> tags but strangely i see that the <ui:include> is not working on the page. The tag is showing as is on the rendered xhtml page.(sreenshot attached). My guess it that the ui tag lib is not being picked up. but am not sure where to check.
My config: WAS 8.5 with stock apache myfaces JSF 2.0 implementation.
You need to make sure that the ui: XML namespace is declared in any parent element as follows:
<anyelement ... xmlns:ui="http://java.sun.com/jsf/facelets">
You also need to make sure that the FacesServlet is in webapp's web.xml being mapped on an URL pattern of *.xhtml, given that you attempted to open it directly on /login.xhtml.
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
The FacesServlet is namely the one responsible for among others parsing that XHTML document and producing the HTML output based on it.

JSF ui:include on mojarra gives "One or more resources have the target of 'head', but no 'head' component has been defined within the view."

I'm loading achild *.xhtml file in my jsf page with ui:include. When I load this
<html>
<head/>
<body>
Testing
</body>
</html>
I get this error
One or more resources have the target of 'head', but no 'head' component has been defined within the view.
However - when I modify it to this:
<html>
<!-- <head/> -->
<body>
Testing
</body>
</html>
The error goes away. (The child *.xhtml file comes from a static content team so it can't have jsf directives in it).
What is the root cause? How can this be avoided?
Versions:
Mojarra 2.0
Java 7.0
Tomcat 7.0
Windows 7.0
It's talking about the <h:head>. Do the same for the body, which should be <h:body>.
By the way, the generated HTML output must be syntactically valid. You usually don't put <html> in an include file, but only in the parent file or the master template.
See also:
How to include another XHTML in XHTML using JSF 2.0 Facelets?
When to use <ui:include>, tag files, composite components and/or custom components?
What's the difference between <h:head> and <head> in Java Facelets?
How to programmatically add JS and CSS resources to <h:head>?

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.

Resources