設置網站默認頁面



在網站開發過程中,我們通常會有這樣的需求:網站訪客輸入域名後,在瀏覽器中顯示默認的頁面,無需在後面輸入默認頁面的名稱,雖然輸入默認頁面的名稱也能正確訪問,但是不符合人們的習慣。(你通過htt://www.hao123.com能訪問hao123的導航主頁面,同樣你也可以通過htt://www.hao123.com/index.html來訪問hao123的導航主頁面,2013-08-15測試通過,但你通常不會這麼幹)。

目前各種Web服務器都可以配置相關網站的默認頁面,IIS通過配置網站的默認文檔,tomcat可以通過web.xml來設置。但是,這種配置通常配置網站根目錄下的頁面,而不會配置包含路徑的頁面。例如:www.hao123.com映射到tomcat下面的hao123 項目,在hao123 項目目錄下有一個index.hmtl的頁面,會在web.xml中配置

<welcome-file-list>
       <welcome-file>index.html</welcome-file>
    </welcome-file-list>

    如果我們的主頁面包含在某個目錄裏面,怎麼辦?例如index.jsp包含在Main文件夾下。或許有人會說在web.xml裏面配上路徑不就行了:

    <welcome-file-list>
        <welcome-file>Main/index.jsp</welcome-file>
    </welcome-file-list>

    我沒見過有誰這樣配過。至少到目前爲止沒遇到過,故不再此評價這種方式的好壞。

下面來分享一下我所遇到的解決方式。

1. 通常見到的是,web.xml配置成最上面那種,然後在index.html裏面寫上

    <head>
        <META HTTP-EQUIV="Refresh" CONTENT="0;URL=Main/index.jsp">
    </head>

通過頁面重定向來訪問我們所需的頁面。 

2. struts可以配置struts.xmlroot namespace,也可以配置default namespace,

    <!—default namespace
    <package name="default" extends="struts-default">
        <default-action-ref name="index"/>
        <action name="index" >
            <result type="redirectAction">
	        <param name="namespace">/Main</param>
	        <param name="actionName">index</param>
	    </result>
        </action>		
    </package> 
    -->

    <!—- root namespace-->
    <package name="root" namespace="/" extends="struts-default">
	<default-action-ref name="index"/>
	<action name="index" >
	    <result type="redirectAction">
		<param name="namespace">/Main</param>
		<param name="actionName">index</param>
	    </result>
	</action>		
    </package>
	
    <package name="Main" namespace="/Main" extends="struts-default">
	<action name="index" class="com.struts.action.login">
	    <result>index.jsp</result>
	</action>
    </package>

關與二者,struts官方文檔描述:

Default namespace 匹配所有的namespace,如果一個action在其他namespace中沒找到,那麼default namespace將被搜索進行匹配。

The default namespace is "" - an empty string. The default namespace is used as a "catch-all" namespace. If an action configuration is not found in a specified namespace, the default namespace is also be searched. The local/global strategy allows an application to have global action configurations outside of the action element "extends" hierarchy.

Root namespace匹配網站根目錄下的請求。

A root namespace ("/") is also supported. The root is the namespace when a request directly under the context path is received. As with other namespaces, it will fall back to the default ("") namespace if a local action is not found.

因此,直接訪問網站域名時,這兩個namespace都會被搜索到。

 

歡迎有不同見解的人士提出寶貴意見。




發佈了40 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章