【问题标题】:how to Hide JSF URL after the application name?如何在应用程序名称后隐藏 JSF URL?
【发布时间】:2013-06-21 10:09:12
【问题描述】:

我有一个 jsf 应用程序,我想隐藏 url 并在页面之间切换时只在 URL 中保留应用程序的名称。

这就是我的网址:

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

这就是我一直想要的:

> http://localhost:8080/PlanificationDrapageWeb/

我怎样才能得到这个结果??

【问题讨论】:

    标签: jsf jakarta-ee


    【解决方案1】:

    正如 MaVRoSCy 所说,您可以使用 Prettyfaces 重写您的 URL。他们的文档非常有用且非常清晰。以下是要遵循的步骤(没有 Maven 依赖项方法):
    1) 根据您的 JSF 版本下载最新的 jar 并将其放入您的项目类路径中。
    2) 关注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) 在WEB-INF 下创建一个文件:pretty-config.xml,它将定义您的漂亮面孔映射,如下所示:

    <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) 现在,当在托管 bean 中定义 outcome 时,您应该返回 pretty:idOfURLMapping。例如:pretty:accueil 将重定向到上面定义的第一个页面,通常它会将http://localhost:8080/PlanificationDrapageWeb/ 显示为 URL。
    最后,请注意,仅当它是功能要求时才应使用它。否则,我会使用 BalusC 提到的不带扩展名的 URL(他的方法,或者如果您想要高级 Prettyfaces 功能)。
    编辑
    Prettyfaces 似乎不适用于这种情况。很抱歉浪费您的时间。
    现在我建议另一种可能的解决方案,因为 BalusC 的答案已被删除。
    1)您创建一个新的会话范围的托管bean,我们称之为:PageManagedBean

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

    2) 创建一个主布局页面(Facelets 模板):

    <!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>
    

    现在,当您想要更改页面时,您只需更改 PageManagedBean.includedPage 的值。

    【讨论】:

    • 我在使用您的解决方案时遇到此错误部署期间发生错误:加载应用程序时出现异常:java.lang.IllegalStateException:ContainerBase.addChild:开始:org.apache.catalina.LifecycleException:java.lang .IllegalArgumentException:java.lang.ClassNotFoundException:com.ocpsoft.pretty.faces.config.PrettyConfigListener。有关详细信息,请参阅 server.log。 C:\Users\hp\Documents\NetBeansProjects\PlanificationDrapage\nbproject\build-impl.xml:294:模块尚未部署。
    • @LaabidiRaissi 我发现你的解决方案很有趣,但问题是 id 不隐藏你有解决方案吗
    【解决方案2】:

    尝试使用prettyFaces

    PrettyFaces 是一个开源 URL 重写库,具有增强的 支持 JavaServer Faces – JSF 1.1、1.2 和 2.0 – 启用 创建可添加书签的漂亮 URL。

    另请参阅UrlRewriteFilter with Glassfish,了解更多有关如何执行此操作的方法。

    1. 在您的 GlassFish 实例前面使用 Apache 并使用 mod_rewrite
    2. 使用Tuckey's Url Rewrite Filter

    【讨论】:

    • 我发现只是解决方案需要 maven 但我没有使用 maven .. 你有没有使用 maven 的教程吗??
    • 我们不能只在 web.xml 中添加一些东西并隐藏 url ??
    猜你喜欢
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-08
    • 1970-01-01
    • 2011-09-04
    相关资源
    最近更新 更多