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就可以了,再去添加对应的增删该查的方法,每一个方法去处理一个请求就可以了,注解方式使用就会比上一篇的那种方式简单很多!

猜你喜欢

转载自blog.csdn.net/weixin_44868863/article/details/89436546
今日推荐