Spring DispatcherServlet 请求映射

在项目中,我们通常会在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的值

当有如上请求时,ControllerRequestMapping注解的value值为/service/model/{modelId}/json

修改DispatcherServlet<url-pattern>的值为/service/*,此时ControllerRequestMapping注解的value值应该改为/model/{modelId}/json

dispatcherServlet对象在进行请求转发时,会将请求中的url与已知的url进行匹配,以便确定对应的ControllerMethod

如何确定已知的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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值