SpringMVC之基于注解的MVC

牧云@^-^@ 提交于 2019-12-09 00:55:02

目录

一、基于注解的 SpringMVC

1.注解优点

2.配置流程

3.执行流程

4.@Controller注解

5.@RequestMapping注解

6.@RequestMapping注解参数

7.方法参数类型

8.方法返回值类型

9.搭建注解程序

二、源代码管理

DispatcherServlet

springmvc-config.xml

控制器注解


一、基于注解的 SpringMVC

1.注解优点

1.相比于传统控制器,使用简单对象和@Controller注解可以处理过个请求,而传统的只可以请求一个,所以基于注解的控制器可以节省代码
2.基于注解控制器的映射规则不用写在配置文件中,使用@RequestMapping进行映射即可!
3.取消了配置文件中的HandlerMapping直接在Controller中使用RequestMapping进行请求映射 

2.配置流程

如果需要使用注解,则需要开启组件扫描,组件扫描需要使用到context包,所以需要导入该包,而jar包有需要有context的描述文件,所以需要在配置文件中加入context jar包的模式文件
1.导入SpringMVC的开发包(测试版本4.1.6),如果是maven项目直接引入以下依赖即可

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.1.6.RELEASE</version>
</dependency>

2.配置文件中开启组件扫描context:component-scan和注解mvc功能支持mvc:annotation-driven

  • SpringMVC功能支持:<mvc:annotation-driven/>
  • 格式:<context:component-scan base-package="需要扫描的包路径"/>
  • 示例:<context:component-scan base-package="com.codecoord.controller"/>
  • 注意:包路径不要带类名、扫描的范围不要太广,哪里需要就指定到哪里
<?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:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans	   http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/jdbc     http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
		http://www.springframework.org/schema/jee 	   http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
		http://www.springframework.org/schema/tx	   http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop 	   http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
		http://www.springframework.org/schema/mvc 	   http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
		http://www.springframework.org/schema/util 	   http://www.springframework.org/schema/util/spring-util-4.1.xsd">
	
	<!-- 开启组件扫描 -->
	<context:component-scan base-package="com.codecoord.controller"/>
    <!-- 开启SpringMVC功能支持 -->
	<mvc:annotation-driven/>

</beans>

3.编写DispatcherServlet
4.创建注解控制器
5.编写视图处理器

3.执行流程

1.传统执行:DispatcherServlet->HandlerMapping->Controller->ModelAndView->ViewResolver
2.注解执行:DispatcherServlet->Controller->ModelAndView(多实现)->ViewResolver
3.在传统控制器中返回值只能使用ModelAndView,而在注解执行中可以有多重返回值

4.@Controller注解

1.注解所在包:org.springframework.stereotype.Controller

2.注解位置:在普通类的类名上指定

3.作用:作为控制器

4.注解示例: 

5.@RequestMapping注解

1.注解所在包:org.springframework.web.bind.annotation.RequestMapping
2.注解位置:在普通类的类名上、处理方法上指定

  • 类上:代表每一个方法都使用其处理
  • 方法上:调用指定方法处理,通常在方法上指定就足够了
  • 作用:作为请求入口,相当于传统Servlet中的servlet name
  • 注意事项:当指定了映射名之后,注意在类上和方法上的区别
    • 方法上:http://域名:端口/项目名/方法映射名
    • 类名上:http://域名:端口/项目名/类映射名/方法映射名
  • 注解示例-在类名上和方法上都有·映射-请求URL就是 http://域名:端口/项目名/to/main,如果只有方法上有就是 http://域名:端口/项目名/main

6.@RequestMapping注解参数

  • value:指定请求映射的方式,value有两种写法
    • value="/请求映射":具有多个属性时候不能省略
    • "/请求映射":只有value一个属性可以忽略
  • method:指定请求类型(GET、POST、DELETE、PUT),只有当符合指定类型的时候才会被调用
    • RequestMethod.PUT:RequestMethod是一个枚举,保存着请求类型
    • method=RequestMethod.PUT:指定响应请求类型为PUT
    • method={RequestMethod.PUT,RequestMethod.GET}:指定多个请求类型,用大括号括起,只有一个可以省略

7.方法参数类型

1.在注解控制器中,发现控制器类只是一个简单的java对象,在传统控制器参数中有HttpServletRequest等,可以方便获取值,而在注解控制器中更加简单,只需要在参数中指定需要的参数即可,例如request、response、session等对象
2.指定参数示例: 

3.可以在参数中指定的类型主要有以下: 

4.注意点:Model是SpringMVC类型而不是ServletAPI类型,每次处理请求都会创建Model对象到注入到各个组件中

8.方法返回值类型

1.在处理器中可以返回值,返回不同值代表不同请求的处理
2.可以返回的类型:

9.搭建注解程序

1.配置DispatcherServlet: 

2.配置springm-config.xml: 

3.创建控制器: 

4.在WEB-INF/jsp/创建main.jsp
5.访问控制器: 


二、源代码管理

1.DispatcherServlet

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
	http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
 
<servlet>
	<servlet-name>springmvc</servlet-name>
	<!-- 指定分发 -->
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<!-- 加载配置文件 -->
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:springmvc-config.xml</param-value>
	</init-param>
</servlet>
<servlet-mapping>
	<servlet-name>springmvc</servlet-name>
	<!-- 拦截所有请求 -->
	<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

2.springmvc-config.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:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans	   http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/jdbc     http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
		http://www.springframework.org/schema/jee 	   http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
		http://www.springframework.org/schema/tx	   http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop 	   http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
		http://www.springframework.org/schema/mvc 	   http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
		http://www.springframework.org/schema/util 	   http://www.springframework.org/schema/util/spring-util-4.1.xsd">
	
	<!-- 开启组件扫描 -->
	<context:component-scan base-package="com.codecoord.controller"/>
	<mvc:annotation-driven/>
	
	<!-- 视图处理器 -->
	<bean id="viewResolver2"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/"/>
		<property name="suffix" value=".jsp"/>
	</bean>
</beans>

3.控制器注解

package com.codecoord.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/** 使用注解扫描 */
@Controller
public class MyController {
	
	@RequestMapping(value="/main")
	public String toMain() {
		return "main";
	}
}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!