使用注解方式搭建SpringMVC开发环境

注解的处理器映射器

注解映射器

  • 在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;
	}
}

测试成功:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

code_mo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值