一、前言
web應用開發時,地址欄輸入ip+port+appName,通常可以跳轉到登錄頁面。以下便介紹我所知道并且驗證過的三種跳轉方式。
二、準備工作
需要使用到兩個url的處理分別如下:
    @At("/index")    @Ok("redirect:/toLogin")    @Filters//表示該url不被過濾(使用一個空的過濾器)    public void init(){            }        @At("/toLogin")    @Ok("")//此處配置登錄頁面的地址路徑    @Filters    public void toLogin(){            }三、三種跳轉登錄頁面的方式
1、配置<welcome-file-list>為存在的index.jsp頁面路徑
<welcome-file-list>配置如下:
<welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
此處index.jsp在WebContent目錄下。并在index.jsp中添加如下代碼:
<%    request.sendRedirect("/index")%>2、配置<welcome-file-list>為一個url。如下所示:
<welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list>
此處:index.html為一個url路徑。此時需要在WebContent目錄下,添加一個空的index.html文件。 這樣的話,輸入ip+port+appName時,就會默認訪問/index.html這個路徑。從而實現向登錄頁面的跳轉。
注意:若使用struts2,則ur名字可能為index.action。此時需在WebContent目錄下新增:index.action空文件。 文件名與配置的url一致。
3、使用UriWriter實現不配置<welcome-file-list>以及不新增index.jsp或index.html等文件時,跳轉到登錄頁面的功能。【不推薦使用,因為這樣會使簡單的問題復雜化。】
步驟1:引入urlWriter的jar包
步驟2:在web.xml中添加如下配置:
<filter> <filter-name>UrlRewriteFilter</filter-name> <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class> </filter> <filter-mapping> <filter-name>UrlRewriteFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
步驟3:在WEB-INF下新增名為:urlrewrite.xml的文件(注意:文件名只能為urlrewrite.xml),內容如下:
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 3.2//EN" "http://tuckey.org/res/dtds/urlrewrite3.2.dtd"><urlrewrite> <rule> <from>^/$</from> <to>/index</to> </rule> </urlrewrite>
即:當路徑為"/"時,會默認以forward的形式轉化為執行"/index"。從而實現到/index的訪問,進而實現到登錄頁面的跳轉。
四、參考資料:
1、通過配置<welcome-file-list>實現主頁跳轉:http://blog.csdn.net/zzq900503/article/details/44460963
2、urlWriter使用:http://blog.csdn.net/lgg201/article/details/5329364
http://www.osblog.net/blog/507.html
==================================================================================================
以上僅為我個人初學nutz的一點小小的使用經驗,如有不正確和不恰當的地方,請大家多多指出,共同進步!
如有轉載,請指明出處。謝謝!
新聞熱點
疑難解答