微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SpringMVC框架02——基于注解的方式

上一篇已经实现了SpringMVC框架对请求的处理,但这种方式并不是我们常用的一种选择,因为第一个我们自定义的Controller和我们的配置文件spring-servlet.xml的关联性比较强,第二个自定义的Controller里面只有一个handleRequest方法,如果要处理的请求非常多,那怎么办??那得一个个用if——else语句去判断,所以不够灵活,基于注解的方式就会更加的灵活,然后基于注解的方式,依赖依然是这些依赖

在这里插入图片描述


配置文件spring-servlet.xml,注解的方式首先开启扫描,加上context和mvc的schema,保存一下

在这里插入图片描述

配置详情如下:

<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
		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-4.3.xsd">
	
	<!-- 开启扫描 -->
	<context:component-scan base-package="com.sxt.controller"></context:component-scan>
	
	<!-- 开启springmvc注解的方式 -->
	<mvc:annotation-driven></mvc:annotation-driven>
</beans>

Controller控制器:

在这里插入图片描述

可以实现多个请求

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述


这样一来我们后面再去做开发的时候spring-servlet.xml的配置文件基本上不用动了,后面如果需要去添加一个新的模块,就只需要在controller里面添加一个controller就可以了,再去添加对应的增删该查的方法,每一个方法去处理一个请求就可以了,注解方式使用就会比上一篇的那种方式简单很多!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。