Spring常见Bug总结

一.404页面
@Controller
@RequestMapping("/")
public class UserController {

    @RequestMapping("/")
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/index");
        return mv;
    }

}

    有时候我们这样配置,直接跳转不到index页面,会出现404页面,原因如下:

    1.如果我们的web.xml配置如下

<servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>

  我们的controller层就应该这样写

@Controller
@RequestMapping("/")
public class UserController {

    @RequestMapping("/index.do")
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/index");
        return mv;
    }

}

   访问地址应该是这样

    

    2..如果controller这样写也可以直接访问

@RequestMapping("/index.html")
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/index");
        return mv;
    }

    3.如果我们想像开头那样写,直接8080就可以访问主页,那我们web.xml拦截的路径就只能这样配置

@Controller
@RequestMapping("/")
public class UserController {

    @RequestMapping("/")
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/index");
        return mv;
    }

}
<servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
二.500报错

一般都是各个jar包的版本不相容所引起的,所以最好的解决方法是找份配套的jar包。


。。。持续更新中。。。

猜你喜欢

转载自blog.csdn.net/Sirius_hly/article/details/80765251