注解的处理器映射器
注解映射器
- 在spring3.1之前使用
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping注解映射器。 - 在spring3.1之后使用
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping注解映射器。
RequestMappingHandlerMapping
- 注解式处理器映射器,对类中标记@ResquestMapping的方法进行映射,根据ResquestMapping定义的url匹配
ResquestMapping标记的方法,匹配成功返回HandlerMethod对象给前端控制器,HandlerMethod对象中封装url对应的方法Method。 - 从spring3.1版本开始,废除了DefaultAnnotationHandlerMapping的使用,推荐使RequestMappingHandlerMapping完成注解式处理器映射。
注解适配器
- 在spring3.1之前使用
org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter注解适配器。 - 在spring3.1之后使用
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter注解适配器。
RequestMappingHandlerAdapter
- 注解式处理器适配器,对标记@ResquestMapping的方法进行适配。
- 从spring3.1版本开始,废除了AnnotationMethodHandlerAdapter的使用,推荐使RequestMappingHandlerAdapter完成注解式处理器适配。
使用注解开发的步骤:
- 创建web工程
- 导入 jar 包
- 在 web.xml 中配置 dispatcherServlet-servlet .xml
- 配置注解映射器和适配器
- 编写注解处理器
- 在spring容器中加载处理器
- 编写视图
- 配置视图解析器
- 部署调试
创建Web工程和导入jar包过程省略,可以参考这篇教程
配置web.xml如下:
<display-name>springMVC01_1017</display-name> <!-- 工程名字 -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name> <!-- 核心处理器的名字 -->
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- 核心处理的类路径 -->
</servlet>
<servlet-mapping> <!-- servlet的映射器 -->
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
配置dispatcherServlet-servlet .xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 采用注解开发 mvc:annotation-driven这个配置替代了两个小弟的bean的配置:处理映射器, 处理适配器 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 开启全包扫描,把我们自己写的controller注册进容器 -->
<context:component-scan base-package="net.neuedu"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
以上就完成SpringMVC环境的搭建。
那么,在我们就可以使用注解进行开发了;
编写一个HelloController测试类测试环境
package net.neuedu.springmvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloController {
@RequestMapping("hello.do") //hello.do就是请求的url
public ModelAndView hello()
{
System.out.println("hello()");
ModelAndView mav=new ModelAndView();
mav.setViewName("abc");// /WEB-INF/abc.jsp
return mav;
}
}
测试成功: