File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to change /faces/* url pattern

 
Fawad Ali
Ranch Hand
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Riaan Nel
Ranch Hand
Posts: 160
IntelliJ IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 17645
39
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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/*".
 
Fawad Ali
Ranch Hand
Posts: 116
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 160
IntelliJ IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This looks like a very clever solution. Well done!
 
naveen gupta
Ranch Hand
Posts: 129
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17645
39
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic