how to Hide JSF URL after the application name?

前端 未结 2 1969
旧时难觅i
旧时难觅i 2021-02-10 03:47

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 :



        
相关标签:
2条回答
  • 2021-02-10 04:10

    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.

    0 讨论(0)
  • 2021-02-10 04:15

    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
    0 讨论(0)
提交回复
热议问题