Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing /faces/ from URL in JSF applications

I have made website using JSF 2.0

All links goes as below.

http://www.mywebsite.com/faces/index.xhtml

What I want is remove /faces/, so that final url would be as below.

http://www.mywebsite.com/index.xhtml

Note: I want to achieve this while passing parameters.

This question is related to my previous question

What I tried is changing filter criteria for my new filter

<filter>
    <filter-name>UrlRewrite</filter-name>
    <filter-class>com.lab.filter.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>UrlRewrite</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

TO

<filter>
    <filter-name>UrlRewrite</filter-name>
    <filter-class>com.lab.filter.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>UrlRewrite</filter-name>
    <url-pattern>/</url-pattern> // here made change from /* to /
</filter-mapping>

But it was giving errors. No web page is showing.

What I want to achieve from previous is mywebsite.com/faces/dr.rajesh to mywebsite.com/dr.rajesh


Full WEB-INF as below.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Production</param-value>
    </context-param>
    <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>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
    <filter>
        <filter-name>restrict</filter-name>
        <filter-class>com.lab.filter.MyFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>restrict</filter-name>
        <url-pattern>*.xhtml</url-pattern>
    </filter-mapping>
    <filter>
        <filter-name>MyFacesExtensionsFilter</filter-name>
        <filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>MyFacesExtensionsFilter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>
    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>none</param-value>
    </context-param>
    <context-param>
        <param-name>com.sun.faces.enableRestoreView11Compatibility</param-name>
        <param-value>true</param-value>
    </context-param>

    <filter>
        <filter-name>UrlRewrite</filter-name>
        <filter-class>com.lab.filter.UrlRewriteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>UrlRewrite</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
like image 529
user3728970 Avatar asked Dec 20 '14 21:12

user3728970


1 Answers

In your web.xml you just need the url pattern below:

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

and remove the other mapping to the Faces Servlet.

In modern JSF versions (>2.1), you don't need any entries in the web.xml at all since the are present 'on' the Faces Servlet via annotations and will be parsed.

like image 153
NerosE Avatar answered Oct 13 '22 15:10

NerosE