在前文《從SpringBoot到SpringMVC(非註解方式)》之中,咱們遠離了 Spring Boot的開箱即用與自動配置的便利性後,迴歸到了淳樸的 Spring MVC開發時代,可是以非註解的方式來給出的,而本文則以註解方式再度講述一遍。html
注: 本文首發於 My Personal Blog:CodeSheep·程序羊,歡迎光臨 小站
一個典型的Spring MVC請求流程如圖所示,詳細分爲12個步驟:前端
整個過程清晰明瞭,下面咱們將結合實際實驗來理解這整個過程。
實驗環境以下:java
這裏我是用IDEA來搭建的基於Maven的SpringMVC項目,搭建過程再也不贅述,各類點擊而且下一步,最終建立好的項目架構以下:web
使用了SpringMVC,則全部的請求都應該交由SpingMVC來管理,即要將全部符合條件的請求攔截到SpringMVC的專有Servlet上。spring
爲此咱們須要在 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來進行處理服務器
從上一步的配置能夠看到,咱們定義的mvc-dispatcher Servlet依賴於配置文件 mvc-dispatcher.xml
,在本步驟中咱們須要在其中添加以下的配置架構
方式一:mvc
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />
方式二:app
<mvc:annotation-driven></mvc:annotation-driven>
因爲使用了註解的處理器映射器和處理器適配器,因此不須要在XML中配置任何信息,也不須要實現任何接口,只須要添加相應註解便可。
@Controller public 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。
因爲能力有限,如有錯誤或者不當之處,還請你們批評指正,一塊兒學習交流!