5

i have a jsf application and i want hide the url and keep just the name of application in URL while swiching between pages .

thats the url that i have :

> http://localhost:8080/PlanificationDrapageWeb/faces/admin/adminHome.xhtml
> http://localhost:8080/PlanificationDrapageWeb/faces/cuisson/Home.xhtml

and thats what i want always have :

> http://localhost:8080/PlanificationDrapageWeb/

how can i get this result ??

marouanoviche
  • 253
  • 4
  • 11
  • 28

2 Answers2

4

As MaVRoSCy said you can use Prettyfaces to rewrite your URLs. Their docs are very useful and very clear. Here are steps to follow (without Maven dependencies approach):
1) Download latest jar depending on your JSF version and put it in your project classpath.
2) Add following to web.xml

<filter>
    <filter-name>Pretty Filter</filter-name>
    <filter-class>com.ocpsoft.pretty.PrettyFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>Pretty Filter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>

3) Create under WEB-INF a file: pretty-config.xml that will define your prettyfaces mappings, like this one:

<pretty-config xmlns="http://ocpsoft.com/prettyfaces/3.3.0" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xsi:schemaLocation="http://ocpsoft.com/prettyfaces/3.3.0
                                    http://ocpsoft.com/xml/ns/prettyfaces/ocpsoft-pretty-faces-3.3.0.xsd">

<url-mapping id="accueil"> 
    <pattern value="/" /> 
       <view-id value="/path-to-yourpage.xhtml" />             
</url-mapping>

<url-mapping id="error"> 
    <pattern value="/" /> 
    <view-id value="/tpath-to-yourpage2.xhtml" />   
</url-mapping>
</pretty-config>

4) Now when defining outcome in your managed beans, you should return pretty:idOfURLMapping. For example: pretty:accueil will redirect to the first defined page above and normally it would display http://localhost:8080/PlanificationDrapageWeb/ as URL.
Finally, notice that you should use this only if it's a functional requirement. Otherwise I would use URLs without extensions as BalusC mentioned (his method or if you want advanced Prettyfaces functionalities).
EDIT
It seems that Prettyfaces does not work for this situation. Sorry for your time wasting.
Now I would suggest another possible solution, since BalusC's answer was deleted.
1) You create a new managed bean of session scope, let's call it: PageManagedBean:

public class PageManagedBean {
  private String includedPage = "/pages/accueil.xhtml";
  //Setters and getters
}

2) Create a master layout page (Facelets templating):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:ui="http://java.sun.com/jsf/facelets"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:f="http://java.sun.com/jsf/core"
  xmlns:p="http://primefaces.org/ui">
<h:head>        

<ui:insert name="head"></ui:insert> 
</h:head>
<h:body>  

<div class="pagewidth">
<ui:include src="shared/header.xhtml"/>
<!-- Content -->
<div class="page_content">
    <div class="page_content_inner">
        <div class="container">                
            <ui:include id="pageLivre" src="#{pageManagedBean.includedPage}"/>                  
        </div> 

    </div>
 </div>
 <div class="page_content_footer"/>
 <ui:include src="shared/footer.xhtml"/>
</div>
</h:body>

Now when you want to change page, you just change PageManagedBean.includedPage value.

Laabidi Raissi
  • 3,263
  • 1
  • 22
  • 28
  • i got this error when using your solution Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: java.lang.IllegalArgumentException: java.lang.ClassNotFoundException: com.ocpsoft.pretty.faces.config.PrettyConfigListener. Please see server.log for more details. C:\Users\hp\Documents\NetBeansProjects\PlanificationDrapage\nbproject\build-impl.xml:294: The module has not been deployed. – marouanoviche Jun 21 '13 at 12:03
  • @LaabidiRaissi i found your solution interesting but the problem is that the id isn't hide do you have a solution about it – Yagami Light Oct 09 '16 at 18:24
2

Try using prettyFaces.

PrettyFaces is an OpenSource URL-rewriting library with enhanced support for JavaServer Faces – JSF 1.1, 1.2 and 2.0 – enabling creation of bookmark-able, pretty URLs.

See also this UrlRewriteFilter with Glassfish a couple ways more on how to do this.

  1. Front your GlassFish instance with Apache and use mod_rewrite
  2. Use Tuckey's Url Rewrite Filter
Community
  • 1
  • 1
MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125