zoukankan      html  css  js  c++  java
  • spring默认欢迎页设置

    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>

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

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

    <welcome-file-list>
           <welcome-file>/WEB-INF/jsp/hello.jsp</welcome-file>
        </welcome-file-list>

    以上方式,在浏览器中输入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>

    跳转到的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>

    此时在浏览器输入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";
        }
    }

    然后找到对应的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>

    此时浏览器中输入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>

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

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

    此时浏览器中输入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>

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

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

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

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

  • 相关阅读:
    java接口变量问题
    FileInputStream与BufferedInputStream的对比
    eclipse使用javaFX写一个HelloWorkld
    Windows 安装Java与配置环境变量
    asp.net core处理中文的指南
    修改release management client对应的服务器的地址
    在server2012安装tfs遇到的问题:KB2919355
    release management客户端无法连接到release management server的问题解决
    如何升级PowerShell
    VS2010下调试.NET源码
  • 原文地址:https://www.cnblogs.com/lixuwu/p/5676101.html
Copyright © 2011-2022 走看看