zoukankan      html  css  js  c++  java
  • SpringMVC默认访问首页配置及web.xml配置

    以前在使用SpringMVC时一直没注意到在web.xml中默认首页直接转到controller的问题,这两天在进行配置时才发现没我想的那么简单,不过其实也不难,查的各种资料因为不合适傻乎乎的绕了点弯路,但也算是解决了,所以记录下来,留个备忘。

    之前访问主页地址为:http://xxxxxxx/xxxx/index.htm 但由于要求,需要去掉index.htm,直接访问地址就转到主页(index.htm是配置的controller地址)

    <!--web.xml默认配置-->
    <welcome-file-list>  
       <welcome-file>/index.html</welcome-file>  
    </welcome-file-list>
    • 1
    • 2
    • 3
    • 4
    //controller配置
    @RequestMapping({"/index.htm"})
    public ModelAndView  indexCore(HttpServletRequest request, HttpServletResponse response){
        ModelAndView mv = new JModelAndView("/core/index.html", request, response);
        return mv;  
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    开始想着这不简单嘛,,直接把web.xml的默认主页写为controller访问地址不就行了,所以就改成了这样:

    <!--web.xml默认配置-->
    <welcome-file-list>
       <welcome-file>/index.htm</welcome-file>  
    </welcome-file-list>
    • 1
    • 2
    • 3
    • 4

    访问后果然报了404。o(╯□╰)o 
    查资料才知道<welcome-file>标签内不能写有后缀名!!!不然就会当做普通的静态页面文件去访问,所以就不会找到相应的controller。(谁把controller访问地址写成/index.htm的,粗来我不打死你╭∩╮(︶︿︶)╭∩╮)

    所以修改如下:

    <!--web.xml默认配置-->
    <welcome-file-list>  
       <welcome-file>index</welcome-file>  
    </welcome-file-list>
    
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/index</url-pattern>
    </servlet-mapping>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    //controller配置
    @RequestMapping({"/index"})
    public ModelAndView  indexCore(HttpServletRequest request, HttpServletResponse response){
        ModelAndView mv = new JModelAndView("/core/index.html", request, response);
        return mv;  
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    然后直接输入http://xxxxxxx/xxxx 访问成功。

    不过还有一种简单直接粗暴的:

    <!--web.xml默认配置-->
    <welcome-file-list>  
       <welcome-file>/</welcome-file>  
    </welcome-file-list>
    • 1
    • 2
    • 3
    • 4
    //controller配置
    @RequestMapping({"/"})
    public ModelAndView  indexCore(HttpServletRequest request, HttpServletResponse response){
        ModelAndView mv = new JModelAndView("/core/index.html", request, response);
        return mv;  
    }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    直接都使用,这样也能达到目的。

  • 相关阅读:
    windows安全实验
    ping 命令的禁止 以及密码的攻破
    网络基础
    html 中间件
    js php BurpSuite v2.1
    网页标签,PHPstudy
    说说text_line_orientation算子的巧妙应用
    说说C#进行数字图像处理的方法
    微信张小龙产品30条
    说说几个常用的阈值分割算子
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13317640.html
Copyright © 2011-2022 走看看