JSF 2 Access on Facelet Files - jsf-2

I am starting to explore JSF 2 facelet and I would like to test this in a simple project.
I just have some query regarding the file structure in JSF 2. When I was using Spring,
I use to put all my pages under WEB-INF so that they wont be accessible to the browser.
I notice in JSF 2, you should put your *.xhtml outside of WEB-INF and allow access to them thru
the Faces Servlet.
Question, does this mean that all enterprise application that utilizes JSF always put
a security constraint in their web.xml?
<security-constraint>
<web-resource-collection>
<web-resource-name>XHTML files</web-resource-name>
<url-pattern>*.xhtml</url-pattern>
</web-resource-collection>
<auth-constraint />
</security-constraint>
Or they are using some sort of a filter, that traps all incoming request and then reject request
that has *.xhtml?
Is my understanding correct and if so which one is more apt to be used?
Thanks

A third alternative in JSF 2.x is to map the FacesServlet just straight on *.xhtml instead of *.jsf or whatever. This way you don't need to cobble with security constraints or filters to prevent endusers from directly accessing *.xhtml files. It has the only disadvantage that you cannot serve "plain vanilla" XHTML files without invoking the FacesServlet, but that would in turn already not make much sense, because such files should technically have the *.html extension.
Please note that this doesn't work in old JSF 1.x. The FacesServlet would run in an infinite loop invoking itself again and again.

Related

How to configure error-page in web.xml for ViewExpiredException? [duplicate]

I have some Facelets files like below.
WebContent
|-- index.xhtml
|-- register.xhtml
|-- templates
| |--userForm.xhtml
| `--banner.xhtml
:
Both pages are using templates from /templates directory. My /index.xhtml opens fine in browser. I get the generated HTML output. I have a link in /index.xhtml file to /register.xhtml file.
However, my /register.xhtml is not getting parsed and returns as plain XHTML / raw XML instead of its generated HTML output. All EL expressions in form of #{...} are displayed as-is instead of that their results are being printed. When I rightclick page in browser and do View page source, then I still see the original XHTML source code instead of the generated HTML output. For example, the <h:body> did not become a <body>. It looks like that the template is not being executed.
However, when I open the /register.xhtml like /faces/register.xhtml in browser's address bar, then it displays correctly. How is this caused and how can I solve it?
There are three main causes.
FacesServlet is not invoked.
XML namespace URIs are missing or wrong.
Multiple JSF implemenations have been loaded.
1. Make sure that URL matches FacesServlet mapping
The URL of the link (the URL as you see in browser's address bar) has to match the <url-pattern> of the FacesServlet as definied in web.xml in order to get all the JSF works to run. The FacesServlet is the one responsible for parsing the XHTML file, collecting submitted form values, performing conversion/validation, updating models, invoking actions and generating HTML output. If you don't invoke the FacesServlet by URL, then all you would get (and see via rightclick, View Source in browser) is indeed the raw XHTML source code.
If the <url-pattern> is for example *.jsf, then the link should point to /register.jsf and not /register.xhtml. If it's for example /faces/*, like you have, then the link should point to /faces/register.xhtml and not /register.xhtml. One way to avoid this confusion is to just change the <url-pattern> from /faces/* to *.xhtml. The below is thus the ideal mapping:
<servlet>
<servlet-name>facesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
If you can't change the <url-pattern> to *.xhtml for some reason, then you probably would also like to prevent endusers from directly accessing XHTML source code files by URL. In that case you can add a <security-constraint> on the <url-pattern> of *.xhtml with an empty <auth-constraint> in web.xml which prevents that:
<security-constraint>
<display-name>Restrict direct access to XHTML files</display-name>
<web-resource-collection>
<web-resource-name>XHTML files</web-resource-name>
<url-pattern>*.xhtml</url-pattern>
</web-resource-collection>
<auth-constraint />
</security-constraint>
JSF 2.3 which was introduced April 2017 has already solved all of above by automatically registering the FacesServlet on an URL pattern of *.xhtml during webapp's startup. The alternative is thus to simply upgrade to latest available JSF version which should be JSF 2.3 or higher. But ideally you should still explicitly register the FacesServlet on only one URL pattern of *.xhtml because having multiple possible URLs for exactly the same resource like /register.xhtml, /register.jsf, /register.faces and /faces/register.xhtml is bad for SEO.
See also:
Set default home page via <welcome-file> in JSF project
Opening JSF Facelets page shows "This XML file does not appear to have any style information associated with it."
Sometimes I see JSF URL is *.jsf, sometimes *.xhtml and sometimes /faces/*. Why?
JavaServer Faces 2.2 and HTML5 support, why is XHTML still being used
Which XHTML files do I need to put in /WEB-INF and which not?
Our servlets wiki - to learn the mandatory basics about servlets
2. Make sure that XML namespaces match JSF version
Since introduction of JSF 2.2, another probable cause is that XML namespaces don't match the JSF version. The xmlns.jcp.org like below is new since JSF 2.2 and does not work in older JSF versions. The symptoms are almost the same as if the FacesServlet is not invoked.
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
If you can't upgrade to JSF 2.2 or higher, then you need to use the old java.sun.com XML namespaces instead:
<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">
But ideally you should always use the latest version where available.
See also:
Which XML namespace to use with JSF 2.2 and up
JSF tags not executed
Warning: This page calls for XML namespace http://xmlns.jcp.org/jsf/XXX declared with prefix XXX but no taglibrary exists for that namespace
3. Multiple JSF implementations have been loaded
One more probable cause is that multiple JSF implementations have been loaded by your webapp, conflicting and corrupting each other. For example, when your webapp's runtime classpath is polluted with multiple different versioned JSF libraries, or in the specific Mojarra 2.x + Tomcat 8.x combination, when there's an unnecessary ConfigureListener entry in webapp's web.xml causing it to be loaded twice.
<!-- You MUST remove this one from web.xml! -->
<!-- This is actually a workaround for buggy GlassFish3 and Jetty servers. -->
<!-- When leaving this in and you're targeting Tomcat, you'll run into trouble. -->
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
When using Maven, make absolutely sure that you declare the dependencies the right way and that you understand dependency scopes. Importantingly, do not bundle dependencies in webapp when those are already provided by the target server.
See also:
Configuration of com.sun.faces.config.ConfigureListener
How to properly install and configure JSF libraries via Maven?
Make sure that you learn JSF the right way
JSF has a very steep learning curve for those unfamiliar with basic HTTP, HTML and Servlets. There are a lot of low quality resources on the Internet. Please ignore code snippet scraping sites maintained by amateurs with primary focus on advertisement income instead of on teaching, such as roseindia, tutorialspoint, javabeat, baeldung, etc. They are easily recognizable by disturbing advertising links/banners. Also please ignore resources dealing with jurassic JSF 1.x. They are easily recognizable by using JSP files instead of XHTML files. JSP as view technology was deprecated since JSF 2.0 at 2009 already.
To get started the right way, start at our JSF wiki page and order an authoritative book.
See also:
Java / Jakarta EE web development, where do I start and what skills do I need?
What is the need of JSF, when UI can be achieved with JavaScript libraries such as jQuery and AngularJS

Locating Javascript and other resources in modular JSF 2.2.x application

I have a modular JSF application. Facelets are stored in the module in META-INF/resources. I added a custom FaceletsResourceResolver (as demonstrated in this post How to create a modular JSF 2.0 application?) and all of this works well with .xhtml - Files. Now I am trying to add other resources in the same fashion and it's not working.
Let's say I have this structure in the module:
/META-INF/resources
/META-INF/resources/foo
/META-INF/resources/foo/bar.xhtml
/META-INF/resources/foo/bar.js
Now the application resolves /foo/bar.xhtml just fine. But attempts to fetch /foo/bar.js simply fail with 404. I tried using
<h:outputScript library="modulename" name="foo/bar.js" />
as well as direct reference
<script language="text/javascript" src="/context/foo/bar.js"/>
both to no avail. I feel I am missing something. Can someone help me?
PS: using Apache Tomcat 6 and Eclipse-Juno for development.
I'll ignore the fact that the mentioned versions in your question in its current form is confusing. JSF 2.2 (as you tagged) requires a minimum of Servlet 3.0. Tomcat 6 (as you mentioned) is a Servlet 2.5 container (and a quite old one either). This is not going to work together. Also noted should be that the FaceletsResourceResolver which you found there is only necessary when you're using Servlet 2.5 or are using a very early JBoss AS 6 version.
The library name does here not represent the JAR file name, but the subfolder where all of those resources commonly belong to. So when you have a
<h:outputScript library="modulename" name="foo/bar.js" />
then the following structure is expected in the JAR:
/META-INF/resources/modulename
/META-INF/resources/modulename/foo
/META-INF/resources/modulename/foo/bar.xhtml
/META-INF/resources/modulename/foo/bar.js
Alternatively, you can keep your original structure and use
<h:outputScript library="foo" name="bar.js" />
depending on the meaning of the actual value of foo.
See also:
Packaging Facelets files (templates, includes, composites) in a JAR

Calling a Jsf View id from a non JSF page

We are integrating new application with existing JSP application and trying to re use some of existing functionality.
I have a navigation rule in faces-navigation.xml like
<from-view-id>/WEB-INF/jsp/admin/login.xhtml</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/WEB-INF/jsp/admin/welcome.xhtml</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>admin</from-outcome>
<to-view-id>/WEB-INF/jsp/admin/dashaboard.xhtml</to-view-id>
</navigation-case>
</navigation-rule>
In my jsp I am trying to call this definition like
Admin login
When I click on "Admin Login", I get page not found exception.
Is there another way to call this view ?
Files in /WEB-INF are not publicly accessible (i.e. the enduser can't open any files in /WEB-INF directly by entering its bare URL in browser's address bar). They are only accessible by a servlet which does a RequestDispatcher#forward() on the file in /WEB-INF folder. The old webapp code setup was apparently using such a servlet, either homegrown or from a different MVC framework.
You should be moving those pages to outside the /WEB-INF folder. I would by the way also remove the misleading /jsp part from the path as those files are not JSP files at all. Given the .xhtml extension, you are instead actually using its successor Facelets.
By the way, navigation rules are obsolete since JSF 2.0 thanks to the new "implicit navigation" feature. Perhaps you was focusing too much at JSF 1.x targeted books/tutorials while learning JSF?

JSF 2.0 javax.faces.webapp.FacesServlet mappig

I started to use JSF 2.0 recently and I don't understand completely how I need to configure the javax.faces.webapp.FacesServlet to correctly handle resources.
For example, If I decided to create a web application with .xhtml files and .jsp files and I want both them to use jsf technology how am I supposed to configure the jsf servlet to handle both?
Sometimes I found example where the servlet url pattern is just /faces/*
thanks!
I suggest to use a suffix pattern as URL pattern like *.jsf. If a Facelets file (.xhtml) is present on the given view ID, then it will be served. Otherwise if a JSP file (.jsp) is present on the given view ID, then it will be served. This also gives you the room to gradually upgrade from JSP to Facelets without the need to change URLs, so that you can ultimately get rid of those legacy JSPs in an easy way.

In GWT I'd like to load the main page on www.domain.com and a dynamic page on www.domain.com/xyz - I just can't make it work

I have a question for which I'm sure there must a simple solution. I'm writing a small GWT application where I want to achieve this:
www.domain.com : should serve the welcome page
www.domain.com/xyz : should serve page xyz, where xyz is just
a key for an item in a database. If there is an
item associated with key xyz, I'll load that and show a page, otherwise I'll show a
404 error page.
I was trying to modify the web.xml file accordingly but I just couldn't
make it work. I could make it work with an url-pattern if the key in question is after another /, for example: www.domain.com/search/xyz. However,
I'd like to have the key xyz directly following the root / (http://www.domain.com/xyz).
Something like
<servlet-mapping>
<servlet-name>main</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
doesn't seem to work as I don't know how to address the main page
index.html (as it's not an actual servlet) which will then load my
main GWT module.
I could make it work with a bad work around (see below): Redirecting a
404 exception to index.html and then doing the look up in the main
entry point, but I'm sure that's not the best practice, also for SEO
purposes.
Can anyone give me a hint on how to configure the web.xml with GWT for
my purpose?
Thanks a lot.
Mike
Work-around via 404:
Web.xml:
<web-app>
<error-page>
<exception-type>404</exception-type>
<location>/index.html</location>
</error-page>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
index.html --> Main Entry point --> onModuleLoad():
String path = Window.Location.getPath();
if (path == null || path.length() == 0 || path.equalsIgnoreCase("/")
|| path.equalsIgnoreCase("/index.html")) {
... // load main page
} else {
lookup(path.substring(1)); // that's key xyz
You don't!
GWT is meant fro creating single-page webapplications (RIA) that don't do hyperlinking to all sorts of loose pages. You essentially bootstrap the GWT app when loading it in the initial page and dynamically generate content in the app by using widgets, handling events and requesting server data through either GWT-RPC or simple HTTP requests (for instance when accessing a REST service on some other domain).
I introduced a colleague to GWT two weeks ago and let him dig around for a while when he got stuck on how to "link to a new page?". I explained GWT is not geared towards page-based navigational websites, but for building applications that use the browser as their runtime.
Hope this can shed a little light on your problem :)
Regards,
-- Jeroen
Jeroen is right. You don't.
However, if you want to force it, you can. Essentially every new url will have to have an HTML page that contains the GWT widget. Thus, causing the GWT widget to then have to reload itself on every new url. Then it would configure its UI accordingly based upon the URL. You can use a REST framework such as Jersey or Restlet to make things easier.
But, you only want to do this if you intend to distribute these new links or urls outside of your GWT app. However, GWT supports URL changes through anchors (not sure if this is the proper terminology), see the documentation on History for more information. Do not 'link' within your app to other URLs on your domain that contain the same GWT widget. Doing anything that will cause a page refresh and your GWT widget to reload would be very inefficient. Use ClickHandlers.
To conclude, don't do this. =)
How about instead of
www.domain.com/?key=A1B2C3
you use GWT's way of doing it:
www.domain.com/#key=A1B2C3
you can do this via the History mechanism.
Try UrlRewriteFilter: http://tuckey.org/urlrewrite/ it is a plain ol' Java EE filter, so it should work.

Resources