URLRewrite 實(shí)現(xiàn)URL地址偽靜態(tài)化
1.首先在http://tuckey.org/urlrewrite/#download下載urlrewirtefilter
2.解壓所下載的文件,把urlrewrite-2.6.0.jar復(fù)制到項(xiàng)目的WebRoot/WEB-INF/lib/目錄下
3.把urlrewrite.xml復(fù)制到項(xiàng)目的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靜態(tài)化 例如:
要把http://localhost/prjtest/user/list.jsp轉(zhuǎn)換成http://localhost/prjtest/user/list.html這種是最簡單的,當(dāng)一個(gè)servlet跳轉(zhuǎn)到list.jsp頁面列出user列表時(shí),在urlrewrite.xml中這樣配置:
<rule>
<from>^/user/list.html</from>
<to
</rule>
當(dāng)請求/user/list.html這個(gè)頁面時(shí),實(shí)際上相當(dāng)于請求/user/list.jsp頁面,在servlet的跳轉(zhuǎn)要這樣寫: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轉(zhuǎn)換成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。當(dāng)一個(gè)URL被提交后,在某一個(gè)組件返回一個(gè)相應(yīng)給瀏覽器之 前,你的應(yīng)用可能轉(zhuǎn)發(fā)請求多次。所有這些都發(fā)生在服務(wù)器端,瀏覽器并不知道發(fā)生了什么事。當(dāng)一個(gè)Http相應(yīng)被放回時(shí),它并沒有包含地址信息,所以瀏覽器 僅僅顯示用來作為初始請求的地址。
要想讓地址欄也變成靜態(tài)化的URL,很簡單,將<to type="redirect">改成<to type="forward">即可
posted on 2009-03-05 09:38 草原上的駱駝 閱讀(940) 評(píng)論(0) 編輯 收藏 所屬分類: JAVA基礎(chǔ)知識(shí)