在项目中,我们通常会在web.xml
文件中配置一个DispatcherServlet
进行请求转发
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
http://localhost:8080/demo/service/model/{modelId}/json
{modelId}
在实际环境中替换为对应modelId
的值
当有如上请求时,Controller
的RequestMapping
注解的value
值为/service/model/{modelId}/json
修改DispatcherServlet
的<url-pattern>
的值为/service/*
,此时Controller
的RequestMapping
注解的value
值应该改为/model/{modelId}/json
dispatcherServlet
对象在进行请求转发时,会将请求中的url
与已知的url
进行匹配,以便确定对应的Controller
和Method
。
如何确定已知的url
呢?
dispatcherServlet
对象初始化时会读取默认的配置文件({servlet-name}-servlet.xml
),当dispatcherServlet
的<init-param>
元素指定了contextConfigLocation
参数,则会读取该参数中指定的配置文件而不再读取默认配置文件({servlet-name}-servlet.xml
),这些配置文件中会指定待扫描Controller
的包名,这些Controller
中指定的url
会被这个dispatcherServlet
对象进行请求转发时与接收的url
进行比较。没有被扫描到的Controller
,其指定的映射url
不会被当前dispatcherServlet
对象用于请求分发时的比较。
由于dispatcherServlet
对象进行请求分发与其指定的配置文件中可扫描到的Controller
有关,故不同的dispatcherServlet
对象中允许不同的Controller
拥有相同的映射url
(这些Controller
被不同的dispatcherServlet扫描
),而同一个dispatcherServlet
对象中不允许有相同的映射url
例子:
web.xml
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/LoginController/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>sysCore</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/sys-core.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>sysCore</servlet-name>
<url-pattern>/UserController/*</url-pattern>
</servlet-mapping>
spring-mvc.xml
<context:component-scan base-package="com.example.controller" />
sys-core.xml
<context:component-scan base-package="com.example.sysCore.controller" />
com.example.controller.LoginController.java
@Controller
//@RequestMapping("/LoginController")
public class LoginController extends BaseController {
@RequestMapping(value = "/intoLoginPage.web", method = {RequestMethod.GET})
public ModelAndView intoLoginPage(HttpServletRequest req, HttpServletResponse resp) {
log.info("<==============LoginController#intoLoginPage==============>");
ModelAndView mav = new ModelAndView();
mav.setViewName("/login");
return mav;
}
}
com.example.sysCore.controller.UserController.java
@Controller
//@RequestMapping("/UserController")
public class UserController extends BaseController {
@RequestMapping(value = "/intoLoginPage.web", method = {RequestMethod.GET})
public ModelAndView user() {
log.info("<==============UserController#user==============>");
ModelAndView mav = new ModelAndView();
mav.setViewName("/login");
return mav;
}
}
Request URL
http://localhost:8080/demo/LoginController/intoLoginPage.web
http://localhost:8080/demo/UserController/intoLoginPage.web