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>
来源:https://stackoverflow.com/questions/12120648/how-to-use-jsf-extension-in-urls