zoukankan      html  css  js  c++  java
  • SpringMVC项目配置欢迎页面为index.html

    一、问题

    在web.xml中添加如下配置无效

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

    访问http://localhost/KingWeixin/ 无作用

    二、解决问题

    2.1、问题分析

    1.默认tomcat容器的默认页面。 
    
    /index.html 
    
    这种方式适合访问静态的页面(也包括JSP)或者说是没有任何参数的页面。
    
    2.spirng mvc 默认index controller 方式 
    如果在tomcat容器没有配置默认页面,怎spring mvc 会主动去寻找/index的controller,如果有则会调用,没有则会显示404页面。 
    @RequestMapping(value=”/index”) 
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response){ 
    return new ModelAndView(“index”); 
    }
    
    3.spirng mvc 配置根节点访问“/”方式 
    这种方法比较极端,就是配置一个名为“/”的controller,就是输入完网址之后就会调用。这种方法是前面两种方法都没有配置的时候。 
    @RequestMapping(value=”/”) public ModelAndView index(HttpServletRequest request, HttpServletResponse response){ return new ModelAndView(“index”); }
    
    三种方法的级别高低:1>>3>>2;因为tomcat的容器级别比spring要高,以上3钟配置都存在的情况,优先使用tomcat。因为配置了”/”的controller,所以会先匹配到相关的controller,而不会先寻找/index controller.
    
    注意,即使web.xml没有添加,tomcat也会自动默认去寻找在webroot目录下面的index文件,如果要使用后面两种方法,则要保证webroot下面没有index相关的文件。
    
    综合经验,第三种方法最方便 使用方法例如:
    
    @RequestMapping("/")
    public ModelAndView index(ModelAndView modelAndView, HttpServletRequest request, String openId) {
        return new ModelAndView("redirect:/toLogin.do");
    }
    
    @RequestMapping("/toLogin.do")
    public ModelAndView toLogin(ModelAndView modelAndView,Model model, HttpServletRequest request) {
        modelAndView.setViewName("index");
        return modelAndView;
    }

    Spring MVC中默认HTML为静态资源,所以我们可以通过设置我们静态资源映射的index.html为项目默认访问的页面

    2.2: Spring 设置静态资源映射和目录(切记index.html要放到html目录下)

    <mvc:resources mapping="/img/**" location="/img/" />
    <mvc:resources mapping="/js/**" location="/js/" />
    <mvc:resources mapping="/css/**" location="/css/" />
    <mvc:resources mapping="/html/**" location="/html/" />
    <mvc:resources mapping="/tinymce/**" location="/tinymce/" />
    <mvc:resources mapping="/upload/**" location="/upload/" />
    <mvc:resources mapping="/assset/**" location="/assset/" />
    <mvc:resources mapping="/data/**" location="/data/" />
    <mvc:resources mapping="/images/**" location="/images/" />
    <mvc:resources mapping="/media/**" location="/media/" />

    2.3:创建一个跳转到index对象的Controller

    /**
     * 
     */
    package com.king.weixin.controller;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    /**
     * @author kingstudy@vip.qq.com
     * @version 创建时间:2018年6月13日 下午10:28:26
     * @ClassName IndexController
     * @Description Spring MVC 跳转到首页
     */
    @Controller
    public class IndexController {
    
        @RequestMapping(value="/") 
        public ModelAndView GoToIndex(HttpServletRequest request, HttpServletResponse response){ 
            return new ModelAndView("index"); 
        }
    
    }

    2.3:测试localhost/项目名称-配置OK

  • 相关阅读:
    *** mixed implicit and normal rules: deprecated syntax
    cold boot and warm boot.
    git打补丁命令
    LSB和MSB
    __attribute__((weak)) ------ 关于弱符号的用法
    键盘和鼠标无法热插拔问题
    yocto编译加速及单独编译内核与uboot
    V4L2学习教程
    linux错误码
    linux内核面试常见试题
  • 原文地址:https://www.cnblogs.com/wxjnew/p/9180763.html
Copyright © 2011-2022 走看看