• 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

  • 相关阅读:
    数据驱动编程法
    23个设计模式的简明教程
    分享一篇文章C语言字节对齐问题(适用于C++)转载至http://blog.csdn.net/21aspnet/article/details/6729724
    关于C++类中访问权限的若干疑问(虚函数访问权限)
    利用C# 反射设计支持可扩展插件的应用程序
    隐藏控制台console application窗口
    Intellij IDEA社区版上新建项目或模块没有Spring Initializr选项解决办法
    mac jmeter 界面乱码
    windows 查看端口被占用进程
    php static 变量声明
  • 原文地址:https://www.cnblogs.com/wxjnew/p/9180763.html
Copyright © 2020-2023  润新知