从 Spring Boot到 Spring MVC(注解方式)
Profile
概述
在前文《从SpringBoot到SpringMVC(非注解方式)》之中,我们远离了 Spring Boot的开箱即可使用与自动配置的便利性后,回归到了淳朴的 Spring MVC开发时代,但是以非注解的方式来给出的,而本文则以注解方式再度讲述一遍。
注: 本文首发于 My Personal Blog:CodeSheep·程序羊,欢迎光临 小站
Spring MVC架构模式
SpringMVC请求解决流程
一个典型的Spring MVC请求流程如图所示,详细分为12个步骤:
- 客户发起请求,由前台控制器DispatcherServlet解决
- 前台控制器通过解决器映射器查找hander,可以根据XML或者者注解去找
- 解决器映射器返回执行链
- 前台控制器请求解决器适配器来执行hander
- 解决器适配器来执行handler
- 解决业务完成后,会给解决器适配器返回ModeAndView对象,其中有视图名称,模型数据
- 解决器适配器将视图名称和模型数据返回到前台控制器
- 前台控制器通过视图解析器来对视图进行解析
- 视图解析器返回真正的视图给前台控制器
- 前台控制器通过返回的视图和数据进行渲染
- 返回渲染完成的视图
- 将最终的视图返回给客户,产生响应
整个过程清晰明了,下面我们将结合实际试验来了解这整个过程。
Spring MVC项目搭建
试验环境如下:
- IntelliJ IDEA 2018.1 (Ultimate Edition)
- SpringMVC 4.3.9.RELEASE
- Maven 3.3.9
这里我是用IDEA来搭建的基于Maven的SpringMVC项目,搭建过程不再赘述,各种点击并且下一步,最终创立好的项目架构如下:
基于Maven的SpringMVC项目
增加前台控制器配置
使用了SpringMVC,则所有的请求都应该交由SpingMVC来管理,即要将所有符合条件的请求阻拦到SpringMVC的专有Servlet上。
为此我们需要在 web.xml
中增加SpringMVC的前台控制器DispatcherServlet:
<!--springmvc前台控制器--> <servlet> <servlet-name>mvc-dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:mvc-dispatcher.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>mvc-dispatcher</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping>
该配置说明所有符合.action的url,都交由mvc-dispatcher这个Servlet来进行解决
编写Spring MVC核心XML配置文件
从上一步的配置可以看到,我们定义的mvc-dispatcher Servlet依赖于配置文件 mvc-dispatcher.xml
,在本步骤中我们需要在其中增加如下的配置
- 增加注解的解决器适配器和解决器映射器
方式一:
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /><bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />
方式二:
<mvc:annotation-driven></mvc:annotation-driven>
编写控制器
因为使用了注解的解决器映射器和解决器适配器,所以不需要在XML中配置任何信息,也不需要实现任何接口,只要要增加相应注解就可。
@Controllerpublic class TestController { private StudentService studentService = new StudentService(); @RequestMapping("/queryStudentsList") public ModelAndView handleRequest( ) throws Exception { List<Student> studentList = studentService.queryStudents(); ModelAndView modelAndView = new ModelAndView(); modelAndView.addObject("studentList",studentList); modelAndView.setViewName("/WEB-INF/views/studentList.jsp"); return modelAndView; }}class StudentService { public List<Student> queryStudents() { List<Student> studentList = new ArrayList<Student>(); Student hansonwang = new Student(); hansonwang.setName("hansonwang99"); hansonwang.setID("123456"); Student codesheep = new Student(); codesheep.setName("codesheep"); codesheep.setID("654321"); studentList.add(hansonwang); studentList.add(codesheep); return studentList; }}
为了让注解的解决器映射器和解决器适配器找到注解的Controllor,有两种配置方式:
方式一:在xml中公告Controllor对应的bean
<bean class="cn.codesheep.controller.TestController" />
方式二:使用扫描配置,对某一个包下的所有类进行扫描,找出所有使用@Controllor注解的Handler控制器类
<context:component-scan base-package="cn.codesheep.controller"></context:component-scan>
编写视图文件
这里的视图文件是一个jsp文件,路径为:/WEB-INF/views/studentList.jsp
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><html><head> <title>学生名单</title></head><body> <h3>学生列表</h3> <table width="300px;" border=1> <tr> <td>姓名</td> <td>学号</td> </tr> <c:forEach items="${studentList}" var="student" > <tr> <td>${student.name}</td> <td>${student.ID}</td> </tr> </c:forEach> </table></body></html>
试验测试
启动Tomcat服务器,而后浏览器输入:
http://localhost:8080/queryStudentsList.action
试验结果
数据渲染OK。
后 记
因为能力有限,若有错误或者者不当之处,还请大家批评指正,一起学习交流!
- My Personal Blog:CodeSheep 程序羊
- 我的半年技术博客之路
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » 从 Spring Boot到 Spring MVC(注解方式)