JSF的URL擴展,重寫工具 PrettyFaces

注意:路徑配置,好比 /xxx/user 配置爲 /xxx/user/ ,可能引起 shiro 控制出錯.引起其餘一些未知的BUG.這個東西...仍是有不少BUG的.通常狀況,最後不要以"/"結束. html

PrettyFaces是一個JSF1.2和JSF2.0的擴展,是一個開源基於過濾器(Filter)的Servlets擴展.用來建立便於書籤收藏、漂亮的網址。 PrettyFaces優雅的解決了這個問題,包括諸如功能:網頁裝載行動,無縫的跟faces的導航整合,動態視圖的ID分配和管理參數分析,無需配 置,兼容其餘JSF框架。PrettyFaces優雅地解決了「RESTful URL」 問題,包括這樣一些特性如: express

  1. URL Rewriting — Replace ugly URLs: 「/faces/page.jsf」 with pretty, SEO-friendly, human URLS: 「/optimized/page/」; The client browser never sees your original URLs, even in the source-HTML
  2. Dynamic view IDs — Now with dynamic view ID mapping for URLs (through El method expressions,) you can show different views based on run-time conditions, effectively de-coupling the URL from the JSF view.
  3. Page-load actions — Call one or more action-methods, at one or more phases, before rendering a view.
  4. Managed parameter parsing — HTTP parameters parsed from URLs are stored in managed beans, simply get a reference to your parameter bean for easy access from other Java classes.
  5. Navigation by ID — Define your page IDs in one place, reference them easily in action-methods and components, using standard JSF navigation techniques.
  6. Integrated validation — For URL and query-parameters, able to re-use existing JSF validator objects.
  7. Easy to configure — Requires absolutely minimal configuration; may be enabled using XML or annotations.
該項目主頁: http://ocpsoft.org/prettyfaces/

Get Started, it’s easy! (3 steps)

1. Include PrettyFaces in your Project

You may either use Maven (recommended) to include PrettyFaces in your project, or you can download the distribution ZIP file directly. app

If you are upgrading from PrettyFaces 3.x, note that all functionality is backwards compatible, but you may wish to review the migration guide to learn about new features made available in the latest releases. 框架

POM.XML jsp

<!-- for JSF 2.x -->
<dependency>
    <groupId>org.ocpsoft.rewrite</groupId>
    <artifactId>rewrite-servlet</artifactId>
    <version>2.0.12.Final</version>
</dependency>
<dependency>
    <groupId>org.ocpsoft.rewrite</groupId>
    <artifactId>rewrite-config-prettyfaces</artifactId>
    <version>2.0.12.Final</version>
</dependency>

2. Create pretty-config.xml

This is where you will create URL mappings between external URL patterns and your internal view files, Servlets, and other resources. Be sure to replace the mapping below with a mapping that matches your application. ide

/WEB-INF/pretty-config.xml ui

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

	<url-mapping id="login">
		<pattern value="/login" />
		<view-id value="/legacy/user/login.jsp" />
	</url-mapping>

</pretty-config>

Add mappings to your configuration

This part is all up to you! You’ll need to figure out which URLs in your application you want to 「prettify」 – so check out the documentation for details on how to fully utilize the PrettyFaces URL-mapping system. url

Additionally, be sure to check out the Rewrite examples or reference manual to learn more about Rewrite, and the powerful options it offers beyond the PrettyFaces configuration module. spa


<url-mapping id="view-user">
	<pattern value="/user/#{username}" />
	<view-id value="/user/view.xhtml" />
</url-mapping>

<!-- Maps "/user/#{username}" to the URL "/user/view.xhtml?username=value" -->

3. Run your application!

You should now be ready to go! PrettyFaces is installed and your first URL-mapping has been created. If everything has been done correctly, you should see PrettyFaces log a message confirming that setup was successful.
相關文章
相關標籤/搜索