我有一个 jsf 应用程序,我想隐藏 url 并在页面之间切换时只保留 URL 中的应用程序名称。
这就是我拥有的网址:
> http://localhost:8080/PlanificationDrapageWeb/faces/admin/adminHome.xhtml
> http://localhost:8080/PlanificationDrapageWeb/faces/cuisson/Home.xhtml
这就是我一直想要的:
> http://localhost:8080/PlanificationDrapageWeb/
我怎样才能得到这个结果?
最佳答案
正如 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) 现在定义
outcome
在您的托管 bean 中,您应该返回 pretty:idOfURLMapping
.例如:pretty:accueil
将重定向到上面定义的第一个页面,通常它会显示 http://localhost:8080/PlanificationDrapageWeb/
作为网址。最后,请注意,只有在功能需求时才应该使用它。否则我会使用 BalusC 提到的没有扩展名的 URL(他的方法或者如果你想要高级的 Prettyfaces 功能)。
编辑
Prettyfaces 似乎不适用于这种情况。很抱歉浪费您的时间。
现在我建议另一种可能的解决方案,因为 BalusC 的回答已被删除。
1) 您创建了一个 session 范围的新托管 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 值。
关于jsf - 如何在应用程序名称后隐藏 JSF URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17232638/