springMVC中Dispatcher中的/和/*的区别

来源:互联网 发布:浙江大学行知学院 编辑:程序博客网 时间:2024/06/14 05:03

1. 首先 / 这个是表示默认的路径,及表示:当没有找到可以匹配的URL就用这个URL去匹配。

2. 在springmvc中可以配置多个DispatcherServlet,比如: 配置多个DispatcherServlet有/和/*,先匹配的是/*这个

3. 当配置相同的情况下,DispathcherServlet配置成/和/*的区别

< 一 > / :使用/配置路径,直接访问到jsp,不经springDispatcherServlet

DispatcherServlet with name 'dispatcherServlet' processing GET request for [/online/jspx/img.jsp] Looking up handler method for path /jspx/img.jsp Did not find handler method for [/jspx/img.jsp] No mapping found for HTTP request with URI [/online/jspx/img.jsp] in DispatcherServlet with name 'dispatcherServlet'
      < 二 > /*:配置/*路径,不能访问到多视图的jsp
DispatcherServlet with name 'dispatcherServlet' processing GET request for [/online/jsp/img.jsp]Looking up handler method for path /jsp/img.jspReturning handler method [public org.springframework.web.servlet.ModelAndView com.anyvape.admin.controller.ViewsController.viewJsp(java.lang.String,org.springframework.ui.Model)]Last-Modified value for [/online/jsp/img.jsp] is: -1Rendering view [org.springframework.web.servlet.view.JstlView: name 'img'; URL [/jspx/img.jsp]] in DispatcherServlet with name 'dispatcherServlet' 这里渲染了Added model object 'templateSign' of type [java.lang.String] to request in view with name 'img'
分析:当我在客户端调用URL:/user/list然后返回user.jsp视图,当配置的是/:DispathcherServlet拿到这个请求然后返回对应的controller,然后通过Dispather Type通过Forward转发到user.jsp视图,即就是请求user.jsp视图(/user/user.jsp),此时Dispather没有拦截/user/user.jsp,因为此时你配置的是默认的/,就顺利的交给ModleAndView去处理显示了。
当配置的是/*:DispathcherServlet拿到这个请求然后返回对应的controller,然后通过Dispather Type通过Forward转发到user.jsp视图,即就是请求user.jsp视图(/user/user.jsp),此时Dispather已经拦截/user/user.jsp,Dispatcher会把他当作Controller去匹配,没有匹配到就会报404错误。

结论:在配置视图的时候尽量用/这种方式。

参考:http://www.oschina.net/question/3808_144366
  http://book.51cto.com/art/201204/329156.htm


0 0
原创粉丝点击