GeeCON Prague 2014*
The moose likes JSF and the fly likes Starting with JSF 2.0 - Templates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSF
Bookmark "Starting with JSF 2.0 - Templates" Watch "Starting with JSF 2.0 - Templates" New topic
Author

Starting with JSF 2.0 - Templates

Christopher Sharp
Ranch Hand

Joined: Dec 12, 2007
Posts: 154
So far so good with my (slow) progress in getting to grips with JSF 2.0.

Now I'm having problems with templates. I created the following file "main-template.xhtml" in a folder called "templates" below the WebContent folder in Eclipse:

And changed my "index.xhtml" file to the following:

But the "h" tags are not recognised by Eclipse. In case it was a problem with Eclipse, I closed it, reopened it, and ran "Validate" but the problem was not resolved. The applications also falis when I attempt to run it.

Obviously "index.xhtml" is not being able to find the "h" tag in the template file. As "main-template.xhtml" is in the subfolder "templates" the path should be "/templates/main-template.xhtml", so I'm wondering why it doesn't work. Does anybody have any ideas?

I looked at documentation at http://courses.coreservlets.com/Course-Materials/pdf/jsf/jsf2/JSF2-Facelets-Templating.pdf and http://www.mkyong.com/jsf2/jsf-2-templating-with-facelets-example/, amongst others.
Christopher Sharp
Ranch Hand

Joined: Dec 12, 2007
Posts: 154
If I add the line xmlns:h="http://java.sun.com/jsf/html" at the top of "index.xhtml", then as expected the "h"tags are not flagged as an error by Eclipse and the page is displayed, but on attempting to click one of the links I get the message:

javax.servlet.ServletException: javax.el.PropertyNotFoundException: /index.xhtml @15,68 action="#{login.option1}": Target Unreachable, identifier 'login' resolved to null

so obviously that quick fix didn't work. Something most be wrong with the paths somewhere, but I don't know here.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16092
    
  21

Do not confuse what your IDE wants with what the application wants. In production, the IDE won't be there. You can usually live with IDE errors (sometimes, in fact, you have little choice), but if the webapp isn't done right, it simply won't run right.

XHTML is an XML notation, and in XML, there is the concept of namespaces, where each namespace is identified by prefix tag (such as "h:" or "ui:") and mapped via the xmlns attribute to a URI. The URI provides a unique and unambiguous identifier for an XML tag schema. Kind of like a UUID/GUID, but in more human-readable terms.

The actual prefix itself isn't important, although there are conventions. If I wanted to, I could map the prefix "peterRabbit" to the JSF HTML core tagset and code things like this:


What is important is that the mapping be done properly. Like so:


The URI http://java.sun.com/jsf/html is compared against meta-information in the component library JARs (jsf-api.jar, for this one, I believe) in order to resolve the tagset schema.

That's basic JSF requirements. Without it, the webapp will fail. Getting Eclipse to not get annoyed with the http://java.sun.com/jsf/html URI requires configuring Eclipse itself to know where a copy of the associated schema can be found, and that's a question that should be asked in the Eclipse forum.


Customer surveys are for companies who didn't pay proper attention to begin with.
Christopher Sharp
Ranch Hand

Joined: Dec 12, 2007
Posts: 154
OK, many thanks, that clears up some questions. I'm aware that an IDE such as Eclipse may not resolve some links that are valid, and will run correctly on a server, but as you say, even if an IDE doesn't complain about a problem doesn't mean there isn't one, and ultimately what matters is how things run when deployed on a server.

In any case, I thought that if tags are defined in the namespace of a template file, that namespace will automatically be propagated to any file that makes use of the template.

I have posted a question on rollovers using Ajax in a new topic.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16092
    
  21

Christopher Sharp wrote:
In any case, I thought that if tags are defined in the namespace of a template file, that namespace will automatically be propagated to any file that makes use of the template.


Nope. The namespaces are static definitions of XML and don't have any knowledge of other XML files (.xhtml files). So no propagation. Each .xhtml file stands alone. That's XML at work, not JSF.
Christopher Sharp
Ranch Hand

Joined: Dec 12, 2007
Posts: 154
OK, many thanks, and that will save me spending any more time on this.
 
GeeCON Prague 2014
 
subject: Starting with JSF 2.0 - Templates