URLRewrite 實現URL地址偽靜態化
1.首先在http://tuckey.org/urlrewrite/#download下載urlrewirtefilter
2.解壓所下載的文件,把urlrewrite-2.6.0.jar復制到項目的WebRoot/WEB-INF/lib/目錄下
3.把urlrewrite.xml復制到項目的WebRoot/WEB-INF/目錄下
4.在web.xml文件中加入filter
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
<param-name>logLevel</param-name>
<param-value>WARN</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
<init-param>
<param-name>logLevel</param-name>
<param-value>WARN</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
5.配置urlrewrite.xml
1.普通url靜態化 例如:
要把http://localhost/prjtest/user/list.jsp轉換成http://localhost/prjtest/user/list.html這種是最簡單的,當一個servlet跳轉到list.jsp頁面列出user列表時,在urlrewrite.xml中這樣配置:
<rule>
<from>^/user/list.html</from>
<to
</rule>
當請求/user/list.html這個頁面時,實際上相當于請求/user/list.jsp頁面,在servlet的跳轉要這樣寫:response.sendRedirect("./user/list.html");<from>^/user/list.html</from>
<to
type="redirect"
>/user/list.jsp</to></rule>
2要把http://localhost/prjtest/user/view.jsp?cid=1&cname=admin轉換成http://localhost/prjtest/user/view/1_admin.html在urlrewrite.xml中這樣配置:
<rule>
<from>^/user/view/([0-9]+)_([a-z]+).html$</from>
<to
</rule>
<from>^/user/view/([0-9]+)_([a-z]+).html$</from>
<to
type="redirect"
>/user/view.jsp?cid=$1&cname=$2</to></rule>
6特別說明
為什么地址欄不變?
原因就在于瀏覽器顯示的是最后被給定的URL。當一個URL被提交后,在某一個組件返回一個相應給瀏覽器之 前,你的應用可能轉發請求多次。所有這些都發生在服務器端,瀏覽器并不知道發生了什么事。當一個Http相應被放回時,它并沒有包含地址信息,所以瀏覽器 僅僅顯示用來作為初始請求的地址。
要想讓地址欄也變成靜態化的URL,很簡單,將<to type="redirect">改成<to type="forward">即可
posted on 2009-03-05 09:38 草原上的駱駝 閱讀(940) 評論(0) 編輯 收藏 所屬分類: JAVA基礎知識