小结:Spring MVC 的 XML 的经典配置方式

在 Spring MVC 的基于 XML 的经典配置方式,如何让某个请求 URL 映射到 HelloController 的具体方法 ,是通过配置 HandlerMapping 来完成的

方式一:使用 BeanNameUrlHandlerMapping(推荐 XML 配置方式)

1. 修改 HelloController,为它指定一个 bean 名称(URL 映射路径)

java 复制代码
// HelloController.java
public class HelloController implements Controller {
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mv = new ModelAndView("hello");
        mv.addObject("msg", "Hello XML Controller");
        return mv;
    }
}

1.1 在 spring-servlet.xml 中配置 Controller bean 和 URL 映射

xml 复制代码
<!-- spring-servlet.xml -->
<context:component-scan base-package="com.example"/>

<!-- 配置 Controller bean -->
<bean id="/hello.htm" class="com.example.HelloController"/>

<!-- 配置 HandlerMapping -->
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

<!-- 配置 ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>

🔍 效果说明:

  • 当访问:http://localhost:8080/your-app/hello.htm
  • 就会调用 HelloControllerhandleRequest 方法。
  • BeanNameUrlHandlerMapping 会将 bean 的 id 视为 URL 路径进行匹配。

方式二:使用 SimpleUrlHandlerMapping(更灵活的 URL 映射)

你也可以使用更显式的 URL 映射方式:

xml 复制代码
<bean id="helloController" class="com.example.HelloController"/>

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="mappings">
        <props>
            <prop key="/hello.htm">helloController</prop>
        </props>
    </property>
</bean>

这种方式允许你将多个 URL 映射到不同的 Controller bean,结构更清晰。


方式三:使用注解(虽然你用的是 XML,但可以混合使用)

如果你希望使用 @RequestMapping 注解,也可以启用注解驱动(即使你用的是 XML 配置):

xml 复制代码
<mvc:annotation-driven />
<context:component-scan base-package="com.example"/>

然后修改 Controller:

java 复制代码
@Controller
public class HelloController {

    @RequestMapping("/hello")
    public String sayHello(Model model) {
        model.addAttribute("msg", "Hello XML + Annotation Controller");
        return "hello";  // 映射到 /WEB-INF/jsp/hello.jsp
    }
}

这样你就可以通过 /hello 访问这个方法。


✅ 五、总结

方式 配置方式 特点
BeanNameUrlHandlerMapping XML bean 的 id 就是 URL
SimpleUrlHandlerMapping XML 更灵活,支持多个 URL 到 bean 的映射
注解 @RequestMapping + `` XML + 注解 更现代,更简洁,推荐使用

🧩 示例:完整 spring-servlet.xml

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 扫描 Controller -->
    <context:component-scan base-package="com.example"/>

    <!-- 启用注解驱动(可选) -->
    <mvc:annotation-driven />

    <!-- 显式配置 Controller 和 URL 映射 -->
    <bean id="/hello.htm" class="com.example.HelloController"/>

    <!-- 使用 BeanNameUrlHandlerMapping -->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

    <!-- 或者使用 SimpleUrlHandlerMapping -->
    <!--
    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/hello.htm">helloController</prop>
            </props>
        </property>
    </bean>
    -->

    <!-- ViewResolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

在 Spring MVC 的经典 XML 配置方式 中,如果你使用的是 传统的 Controller 接口 (即 org.springframework.web.servlet.mvc.Controller),那么:


不能直接为 HelloController 添加多个方法并分别映射到不同的 URL。

原因:

你当前的 HelloController 是这样定义的:

java 复制代码
public class HelloController implements Controller {
    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) {
        ...
    }
}
  • 它实现了 Spring MVC 的 Controller 接口,该接口 只有一个方法:handleRequest()
  • Spring MVC 会把整个类当作一个控制器,所有的请求都只会调用 handleRequest() 方法
  • 所以,即使你添加了多个方法,也无法通过 URL 映射到不同的方法上

✅ 如何实现一个 Controller 多个方法?

✅ 方法一:使用 MultiActionController(Spring 3.x 及以前支持)

这是 Spring 提供的一个旧版控制器类,允许你在同一个控制器中有多个处理方法。

示例:
java 复制代码
public class HelloController extends MultiActionController {

    public ModelAndView sayHello(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mv = new ModelAndView("hello");
        mv.addObject("msg", "Hello from sayHello");
        return mv;
    }

    public ModelAndView sayHi(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mv = new ModelAndView("hello");
        mv.addObject("msg", "Hello from sayHi");
        return mv;
    }
}
XML 配置 URL 映射:
xml 复制代码
<bean name="/hello/*.htm" class="com.example.HelloController">
    <property name="methodNameResolver">
        <bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
            <property name="mappings">
                <props>
                    <prop key="/hello/sayHello.htm">sayHello</prop>
                    <prop key="/hello/sayHi.htm">sayHi</prop>
                </props>
            </property>
        </bean>
    </property>
</bean>
访问地址:
  • /hello/sayHello.htmsayHello() 方法
  • /hello/sayHi.htmsayHi() 方法

⚠️ 注意:MultiActionController 在 Spring 3.2 以后已被弃用(不推荐使用),建议使用基于注解的方式。


✅ 方法二:使用注解驱动(推荐)

如果你希望一个 Controller 有多个方法,并且每个方法映射不同的 URL,最推荐的方式是使用:

  • @Controller
  • @RequestMapping
示例:
java 复制代码
@Controller
public class HelloController {

    @RequestMapping("/hello")
    public String sayHello(Model model) {
        model.addAttribute("msg", "Hello from sayHello");
        return "hello";
    }

    @RequestMapping("/hi")
    public String sayHi(Model model) {
        model.addAttribute("msg", "Hello from sayHi");
        return "hello";
    }
}
XML 中启用注解支持:
xml 复制代码
<context:component-scan base-package="com.example"/>
<mvc:annotation-driven />
访问地址:
  • /your-app/hellosayHello()
  • /your-app/hisayHi()

✅ 方法三:混合使用 XML 和多个 Controller

如果你坚持使用 XML 配置方式,但又想支持多个方法,可以:

  • 创建多个 Controller 类(每个类实现 Controller 接口)
  • 每个类对应一个 URL
  • 在 XML 中分别为它们配置 URL 映射
示例:
java 复制代码
// HelloController.java
public class HelloController implements Controller {
    public ModelAndView handleRequest(...) {
        ...
    }
}

// HiController.java
public class HiController implements Controller {
    public ModelAndView handleRequest(...) {
        ...
    }
}
XML 配置:
xml 复制代码
<bean id="/hello.htm" class="com.example.HelloController"/>
<bean id="/hi.htm" class="com.example.HiController"/>

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
相关推荐
萧瑟余晖6 小时前
Java深入解析篇十七之Spring Security
java·开发语言·spring
前网易架构师-高司机10 小时前
带标注的羽毛球运动员,裁判,羽毛球识别数据集,识别率84.4%,2879张图,支持yolo,coco json,voc xml,文末有模型训练代码
xml·yolo·json·数据集·羽毛球·裁判
Flittly12 小时前
【雕虫大技】Agent 动态 Skill 供应链安全加固(二):执行隔离沙箱实战
java·spring boot·spring
BUG指挥官1 天前
Sa-Token和Spring Security对比
java·后端·spring
geminigoth1 天前
Spring AI Alibaba 入门开发一(备份)
java·人工智能·spring
xbgRS1 天前
spring整合mybaits
java·spring·mybatis
ruleslol2 天前
静态依赖注入 VS 动态依赖查询
spring
墨雨晨曦882 天前
Spring AI总结
java·人工智能·spring
用户3126874877202 天前
Spring Boot 异常处理到底怎么玩的?从 DispatcherServlet 到全局兜底的全链路拆解
spring
不才不才不不才2 天前
Spring 源码系列(17): HandlerMapping 与 HandlerAdapter 两大体系
java·后端·spring