How to use .jsf extension in URLs? - jsf

I'm developing a JSF 2 web application. For prestige purpouses I would like that every URL ends with .jsf extension. Now it ends with .xhtml. If I change it directly to .jsf in web browser address bar, then a HTTP 500 error is shown.
How can I set it to .jsf?

The URL pattern of JSF pages is specified by <servlet-mapping> of the FacesServlet in web.xml. As you mentioned that .xhtml works fine, you have apparently configured it as follows:
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
You need to change the <url-pattern> accordingly to get the desired virtual URL extension.
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
That's all you need to change in order to achieve the concrete functional requirement, really.
However, this puts a security problem open. The enduser can now see the raw Facelets file source code when changing the extension in the URL back from .jsf to .xhtml. You can prevent this by adding the following security constraint to web.xml:
<security-constraint>
<display-name>Restrict access to Facelets source code.</display-name>
<web-resource-collection>
<web-resource-name>Facelets</web-resource-name>
<url-pattern>*.xhtml</url-pattern>
</web-resource-collection>
<auth-constraint/>
</security-constraint>

<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>

you can add this code in your web.xml, and you can run your pages ends with xhtml, jsf or faces
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

Related

Separating .xhtml in subfolders

I'm using JSF for my project and I want to separate .xhtml files in subfolders.
My current directory structure is as follows:
webapp/
resources/
WEB-INF/ (beans.xml, web.xml)
faces/ (test.xhtml)
login.xhtml
register.xhtml
...
But when I try to access [PROJECT]/faces/test.xhtml it never works. I get a 404 Not Found error with the following description: "/test.xhtml Not Found in ExternalContext as a Resource."
The stackOverflow question: Organizing .xhtml files in subfolders has exactly the same problem and still hasn't received any helpful answers regarding this problem.
As an alternative I can also ofcourse use container managed authentication, but I refuse to do so, because it should be possible to just separate views in different subfolders, right?
web.xml:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Project</display-name>
<welcome-file-list>
<welcome-file>timeline.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
Okayy I solved my problem by removing the following line of code:
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
I don't know why that was a problem though.

Render xhtml page by URL

how to render .xhtml pages by it's URL using JSF? It's something like using JSP and Servlet. Where you can map URL to every doGet() method and it will display page by it's name.
Thanx.
You don't have to map each xhtml to an url. That is done by JSF automatically when you set up your web.xml file:
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
You must place all your xhtml on your WEB-INF folder and you can access them with the URL:
http://localhost:port/WebAppContext/faces/page.xhtml

IntelliJ project errors "/hello.xhtml Not Found in ExternalContext as a Resource"

My web.xml contains
<welcome-file-list>
<welcome-file>faces/hello.xhtml</welcome-file>
</welcome-file-list>
<!-- JSF mapping -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
Directory structure is
-web
---WEB-INF
faces-config.xml
web.xml
---hello.xhtml
I know that there are many questions like this on stackoverflow but they didn't help me. What am I doing wrong?
The problem was that IntelliJ Idea configured non-existed folder as web resources.
Project Structure -> Facets -> Web, then i changed web resources directory path ti actual web folder. That resolved the issue.

JSF 2.0 VIew Handler

I am migrating application to JSF 1.2 to JSF 2.0,I am facing issue when i open a new popup window in jsf 2.0,when i open a window jsf 2 create a new view for page1.jsf and another view for page1.xhtml.it creates two views for same page the only difference is of suffix.
when it create second view all my query param got lost which results a blank popup windows.
servlet mapping is web.xml is :
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
Change the Web.xml file
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<servlet>
<servlet-name>facesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
Details Migration of JSF2.0 follow this link....
Migrating from JSF 1.2 to JSF 2.0

java.lang.NullPointerException at com.sun.faces.application.view.FaceletViewHandlingStrategy.createView after JSF 1.2 to 2 migration

I am getting the below error after JSF 1.2 to JSF 2 migration.
java.lang.NullPointerException
at com.sun.faces.application.view.FaceletViewHandlingStrategy.createView(FaceletViewHandlingStrategy.java:804)
at c
How is this caused and how can I solve it? I tried with 2.0.11 which allow me to login and getting different error, where as if i try with 2.2 got the below error. Attached web.xml
http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd">
<display-name>sa-web</display-name>
<welcome-file-list>
<welcome-file>/faces/pages/home.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>javax.faces.FACELETS_LIBRARIES</param-name>
<param-value>/WEB-INF/facelet-taglib/facelets-ui.taglib.xml;/WEB-INF/facelet-taglib/shale_validation.taglib.xml;/WEB-INF/facelet-taglib/tomahawk.taglib.xml;/WEB-INF/facelet-taglib/saui.taglib.xml;/WEB-INF/facelet-taglib/radien_security.taglib.xml
</param-value>
</context-param>
<context-param>
<param-name>javax.faces.CONFIG_FILES</param-name>
<param-value>/WEB-INF/faces-config-global.xml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.jsp</param-value>
</context-param>
<context-param>
<param-name>facelets.VIEW_MAPPINGS</param-name>
<param-value>/pages/*;/templates/*;/public/*</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>facelets.DEBUG</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>
org.apache.shale.view.VIEW_CONTROLLER_MAPPER
</param-name>
<param-value>
com.gmacfs.sa.web.framework.shale.ExtendedViewControllerMapper
</param-value>
</context-param>
<filter>
<filter-name>extensionsFilter</filter-name>
<filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter
</filter-class>
<init-param>
<description>Set the size limit for uploaded files.
Format: 10 - 10 bytes
10k - 10 KB
10m - 10 MB
1g - 1 GB
</description>
<param-name>uploadMaxFileSize</param-name>
<param-value>100m</param-value>
</init-param>
<init-param>
<description>Set the threshold size - files below this limit are
stored
in memory, files above this limit are stored on disk.
Format: 10 - 10 bytes
10k - 10 KB
10m - 10 MB
1g - 1 GB
</description>
<param-name>uploadThresholdSize</param-name>
<param-value>100k</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>extensionsFilter</filter-name>
<url-pattern>/faces/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>extensionsFilter</filter-name>
<url-pattern>*.jsf</url-pattern>
</filter-mapping>
<filter>
<display-name>AuthenticationFilter</display-name>
<filter-name>AuthenticationFilter</filter-name>
<filter-class>com.gmacfs.sa.web.framework.filters.AuthenticationFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>AuthenticationFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>com.gmacfs.sa.web.framework.listeners.ValidatorsInitializer
</listener-class>
</listener>
<servlet>
<servlet-name>StartupServlet</servlet-name>
<servlet-class>com.gmacfs.sa.framework.servlets.StartupServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>ResourceServlet</servlet-name>
<servlet-class>com.gmacfs.sa.framework.servlets.ResourceServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>com.gmacfs.sa.framework.servlets.LogoutServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ResourceServlet</servlet-name>
<url-pattern>/resource/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ResourceServlet</servlet-name>
<url-pattern>/nocache-resource/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/logout</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Logging Servlet</servlet-name>
<servlet-class>com.gmacfs.sa.framework.servlets.GMACLoggingServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Logging Servlet</servlet-name>
<url-pattern>/log4j/*</url-pattern>
</servlet-mapping>

Resources