设置网站默认页面



在网站开发过程中,我们通常会有这样的需求:网站访客输入域名后,在浏览器中显示默认的页面,无需在后面输入默认页面的名称,虽然输入默认页面的名称也能正确访问,但是不符合人们的习惯。(你通过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都会被搜索到。

 

欢迎有不同见解的人士提出宝贵意见。




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值