<转载>springmvc(29):spring默认欢迎页设置

本文介绍如何在Spring MVC中配置欢迎页,实现直接展示静态网页或通过不同方式跳转到指定视图页面,并探讨了对根路径进行拦截的几种方法。
  1. 简单配置的方式,直接展示静态网页,不经过Controller。 
    web.xml 中什么没有配置任何有关欢迎页的信息!其实这时等效于如下配置:这个会由Web容器最先访问!

//-未指定欢迎页时,缺省等于如下配置。这个应该不同的Web服务器可以设置,但大多数都如此-。

<welcome-file-list>
 <welcome-file>index.html</welcome-file>
 <welcome-file>index.htm</welcome-file>
 <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
 
  • 1
  • 2
  • 3
  • 4
  • 5

在web.xml中什么都不写的时候,默认访问WebContent目录下的index页面,不管是index.html还是index.jsp页面。但是这个页面不经过Controller的处理,相当于静态的页面。

此外也可以通过如下的方式,指定首页要显示的视图页面:

<welcome-file-list>
       <welcome-file>/WEB-INF/jsp/hello.jsp</welcome-file>
    </welcome-file-list>
 
  • 1
  • 2
  • 3

以上方式,在浏览器中输入http://localhost:8080/工程名/,即可返回指定的页面。

2.在静态页面上进行跳转。核心 代码:<meta http-equiv="Refresh" content="0; URL=spring/"> 
项目目录下,有个index.html文件,加入如下内容进行跳转:

<html> 
<head>
  <meta http-equiv="Refresh" content="0; URL=spring/">
</head>
</html>
 
  • 1
  • 2
  • 3
  • 4
  • 5

跳转到的url全路径就相当于 http://localhost:8080/项目名/spring/。这个路径就会由mvc 的DispatcherServlet来处理。为什么呢,是因为web.xml中进行了如下url配置:

<servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/app/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/spring/*</url-pattern>
</servlet-mapping>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

此时在浏览器输入http://localhost:8080/项目名时候,浏览器的地址会自动的跳转到 http://localhost:8080/项目名/spring/,根据此规则,就会对应为MVC 路径路由中的 /。也就是HomeController。

@Controller
public class HomeController
{
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Model model)
    {        
        return "home";
    }
}
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

然后找到对应的home.jsp。

整个流程如此。其实缺省为什么设置这么一个spring路径。主要目的是为了提升spring对url的处理效率。spring/下的分支都交由spring来处理,其它的就可以交由web 服务器。

如果一切都交给spring处理,我们就要将 / 进行拦截。嗯,一定会由很多静态资源、或者其它动态jsp是另有用途,也会先被spring拦截,然后再当做另外来处理。可以是可以,但是效率上就会觉得多了一步。

但是,另一方面,我们在规划url时,可能会尽可能的减短,以方便用户的输入;同时,规划url时,才不会考虑spring的效率呢,也就是url设计先行。这个时候,通常不会有spring这个特定的路径;也就是spring要将就url的规划。也就是要对 / 进行拦截了。

3.配置servlet。 
之前的SpingMVC配置控制器的代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

    <!-- 为spring配置监听器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml,
                 <!-- /WEB-INF/hello-dao.xml,
                 /WEB-INF/hello-service.xml, -->
    </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 默认首页定义 --> 
    <welcome-file-list>
       <welcome-file>/WEB-INF/jsp/hello.jsp</welcome-file> 
    </welcome-file-list>
    <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 通过初始化参数声明配置文件位置 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/hello-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>


</web-app>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

此时浏览器中输入http://localhost:8080/Hello/即可进入到指定的静态首页。

问题的由来: 
welcome-file-list一般情况下只能使用静态网页,如果非要把他配置成SpringMVC的控制器URL就会报错

解决的方法: 
仔细看了一些资料,发现welcome-file-list可以转向到servlet,但是!!!前提是servlet不能有扩展名,否则就当成静态文件处理了,那么这样的话就尝试了定义个没有扩展名的SpringMVC控制器URL。修改配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

    <!-- 为spring配置监听器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml,
                 <!-- /WEB-INF/hello-dao.xml,
                 /WEB-INF/hello-service.xml, -->
    </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 指定welcome-file-list要转向的servlet -->
    <welcome-file-list>
       <welcome-file>index</welcome-file>  <!-- 注意 -->
    </welcome-file-list>
    <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 通过初始化参数声明配置文件位置 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/hello-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/index</url-pattern>     <!-- 注意 -->
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>


</web-app>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

注意:welcome-file-list配置的是没有 / 的 index,下面为SpringMVC控制器单独注册了一个 /index 的URL(这个有 “/”) 
Controller写法:

@Controller
public class Shouye {
    @RequestMapping(value = "/index") 
    public String index(){  
        System.out.print("333shouye");
            return "index";  
    }  


}
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

此时浏览器中输入http://localhost:8080/Hello/,即可进入到Shouye 返回的视图。

4.直接对根路径进行拦截 
必须在web.xml中加入如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
         http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">

    <!-- 为spring配置监听器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml,
                 <!-- /WEB-INF/hello-dao.xml,
                 /WEB-INF/hello-service.xml, -->
    </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 指定默认页面 -->
    <welcome-file-list>
       <welcome-file></welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>hello</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 通过初始化参数声明配置文件位置 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/hello-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>hello</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>


</web-app>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

此时,web服务器就知道,根路径不要web服务器来处理,而是由程序自身来处理。这时,index.html也就不起作用了。

然后,根路径就被 Shouye 这个Controller拦截了;因为其中配置了对”/”的映射。

@Controller
public class Shouye {
    @RequestMapping(value = "/") 
    public String index(){  
        System.out.print("333shouye");
            return "index";  
    }  


}
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

此时浏览器中输入http://localhost:8080/Hello/,即可进入到Shouye 返回的视图。

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://JAVA.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>springMVC</display-name> <welcome-file-list> <welcome-file>/WEB-INF/jsp/login.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-mybatis.xml</param-value> </context-param> <filter> <filter-name>encodingFilter</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <context-param> <param-name>webAppRootKey</param-name> <param-value>keshe_C12_09.root</param-value> </context-param> <listener> <listener-class> org.springframework.web.util.Log4jConfigListener </listener-class> </listener> </web-app>
07-16
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>agileEx</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>agileEx</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springMvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>agileEx</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <display-name>PpmFilter</display-name> <filter-name>PpmFilter</filter-name> <filter-class>com.dahuatech.core.interceptor.PpmFilter</filter-class> </filter> <filter-mapping> <filter-name>PpmFilter</filter-name> <url-pattern>/property/*</url-pattern> </filter-mapping> <filter> <display-name>TpmFilter</display-name> <filter-name>TpmFilter</filter-name> <filter-class>com.dahuatech.core.interceptor.TpmFilter</filter-class> </filter> <filter-mapping> <filter-name>TpmFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <display-name>dimission</display-name> <filter-name>dimission</filter-name> <filter-class>com.dahuatech.core.interceptor.DimissionFilter</filter-class> </filter> <filter-mapping> <filter-name>dimission</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <display-name>RestAuthFilter</display-name> <filter-name>RestAuthFilter</filter-name> <filter-class>com.dahuatech.core.interceptor.RestAuthFilter</filter-class> </filter> <filter-mapping> <filter-name>RestAuthFilter</filter-name> <url-pattern>/rest/*</url-pattern> </filter-mapping> </web-app>
最新发布
11-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值