How to remove the file extension in jsf

975 views
Skip to first unread message

Vasanth Kumar R

unread,
May 7, 2016, 1:22:56 AM5/7/16
to UrlRewrite
Hi All,

I want to remove and also rewrite the Url of my web application.I am doing it in java.
I am getting error in my web.xml page.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE urlrewrite
    PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"

  <display-name>Url rewrite</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</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>
  <context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
  </context-param>
  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
  </context-param>
  <listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
  </listener>
  
  <filter>
    <filter-name>UrlRewriteFilter</filter-name>
    <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>


/*This part I am getting the error*/
<rule match-type="wildcard">
       <from>/world.xhtml</from>
       <to>/world</to>
    </rule>
/*This part I am getting the error*/

</web-app>


Ujjwal Singh

unread,
Oct 20, 2016, 11:05:10 AM10/20/16
to UrlRewrite
Don't you need to move this in a seperate file named 'urlrewrite.xml' instaed of web.xml

<rule match-type="wildcard">
    <from>/world.xhtml</from>
    <to>/world</to>
</rule>

move above 4 lines to 'urlrewrite.xml' and place in same location as web.xml

zappulle

unread,
Oct 27, 2016, 12:06:27 AM10/27/16
to UrlRewrite

So after much experimentation I found how to do remove the JSP extension from all pages.  The rule below translats any page that has 1 or more characters in the page URI and fowards it to [page name].jsp

Its important to not fire the rule if its not a JSP page off the context root for my site so I exclude files in folders css, img, css, product-img

<?xml version="1.0" encoding="utf-8"?>


<!DOCTYPE urlrewrite
    PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"
    "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">
   
<urlrewrite>
    <rule match-type="regex">
         <condition type="request-url" operator="notequal">^.*\.jsp$</condition>
        
        <condition type="request-url" operator="notequal">/css/.*</condition>
        <condition type="request-url" operator="notequal">/img/.*</condition>
        <condition type="request-url" operator="notequal">/js/.*</condition>
        <condition type="request-url" operator="notequal">/product-img/.*</condition>
       
        <from>/.+</from>
        <to type="forward">%{request-uri}.jsp</to>
    </rule>
</urlrewrite>



On Monday, October 17, 2016 at 12:27:49 PM UTC-4, zappulle wrote:
Does anyone have an example of the rule(s) to tidy up a JSP page URL in a generic manner?  I'd like to remove the .jsp file extensions from all pages without having to specify the page name in each rule.  i.e. from /Login.jsp to /Login for all jsp pages.
Reply all
Reply to author
Forward
0 new messages