aspose file tools*
The moose likes JSF and the fly likes How to change /faces/* url pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "How to change /faces/* url pattern" Watch "How to change /faces/* url pattern" New topic
Author

How to change /faces/* url pattern

Fawad Ali
Ranch Hand

Joined: Dec 28, 2009
Posts: 112
Hello All,
I have jsp page named first.jsp. I want its url to be /first only instead of /faces/first.jsp without the .jsp extenssion. Is there any way of doing it in JSF.


Regards, Fawad Ali.
Software Engineer, Stafona Inc. - My Blog
Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 160

Fawad Ali wrote:Hello All,
I have jsp page named first.jsp. I want its url to be /first only instead of /faces/first.jsp without the .jsp extenssion. Is there any way of doing it in JSF.

To remove the 'faces' part of the url, I think you could change

to

in your web.xml file. I'm not sure if this is a good idea though. As for hiding the extension, I'm not sure how you would accomplish that.


"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16134
    
  21

What makes JSF URLS be JSF URLs is that the are directed to the FacesServlet. They are directed there because of the URL mapping statement. URLs that match the mapping pattern get routed to the FacesServlet because that's how J2EE URL routing works. It's that simple.

So if you really want to mess with people, you could just as easily have made a mapping pattern like "*.aspx" or "/uglyfaces/*".


Customer surveys are for companies who didn't pay proper attention to begin with.
Fawad Ali
Ranch Hand

Joined: Dec 28, 2009
Posts: 112
Thanks guys for your replies. Well I found a soultion for this. In web.xml I specified this URL pattern.

<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<session-config>

which removes the faces in start and converts the .jsp extenssion to .faces. Then I made a filter so that each request is first passed from that filter which is

<filter>
<filter-name>URLFilter</filter-name>
<filter-class>com.soft.autohaul.Filter.URLFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>URLFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

which means any request will first pass through the URLFilter class which as follows


import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class URLFilter implements Filter {

public void destroy() {
// TODO Auto-generated method stub

}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest httpServletRequest = ((HttpServletRequest)request);
String dispatchURL =((HttpServletRequest)request).getServletPath()+".faces";
try{

httpServletRequest.getRequestDispatcher(dispatchURL).forward(request, response);
}catch(Exception e){
}

}


public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub

}

}

Si if I want to visit "first.jsp". I will write only "first" in browser without the extenssion. The request will go to filter which will add the ".faces" extenssion to it. After that the request will be dispatched to faces Servlet.
Hope it helps others.

Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 160

This looks like a very clever solution. Well done!
naveen gupta
Ranch Hand

Joined: Apr 12, 2006
Posts: 129
Just Curious, what is the reason you don't want to type the extension?

And i think there should be some easy way to do it right?

Let me do some digging
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16134
    
  21

Naveen, you're awakened a zombie. This thread was last updated over a year ago.

But a lot of people don't like URLs with filename-type extensions on them. That's one of the things that PrettyFaces is designed for.
naveen gupta
Ranch Hand

Joined: Apr 12, 2006
Posts: 129
oops..............

i might have searched for something in the forum and found this thread interesting and ...................
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to change /faces/* url pattern