1. 做爲spring MVC入門,先以xml配置的方式爲例。首先須要配置web工程的web.xml文件。java
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <!--配置監聽器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--配置springIoC--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:beans.xml</param-value> </context-param> <!--配置字符編碼過濾器--> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceRequestEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--配置處理器分發器--> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!--配置分發攔截,/表示全部(除了jsp)--> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
討論一下這個文件的配置內容:web
2. 配置須要用到的beans.xml(配置spring IoC)spring
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <aop:aspectj-autoproxy/> <context:component-scan base-package="com.wise.tiger"> <!--過濾掉@Controller註解--> <context:exclude filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> </beans>
3. 配置須要用到的spring-mvc.xml(配置spring mvc)express
<?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:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven/> <context:component-scan base-package="com.wise.tiger.controller"/> <!--配置視圖解析器--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
這裏的配置也比較簡單:api
4. 開發一個簡單的Controllerspring-mvc
package com.wise.tiger.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; //註解@Controller表示它是一個控制器 @Controller public class FirstController { //映射URI請求爲/list.do @RequestMapping("/list.do") public ModelAndView list() { //模型和視圖 var ret = new ModelAndView(); ret.addObject("books", "這是從service中查詢到的圖書列表信息"); //視圖名稱 ret.setViewName("list"); return ret; } }
首先註解@Controller是一個控制器,Spring MVC掃描的時候就會把它做爲控制器加載進來。而後@RequestMapping("/list.do")指定了對應的請求的URI,Spring MVC在初始化的時候就會將這些信息解析,存放起來,因而便有了HandlerMapping。當發生請求時,springmvc就會去使用這些信息去找到對應的控制器提供服務。方法定義返回ModelAndView,在方法中,視圖名稱爲list,從配置的視圖解析器中會尋找/WEB-INF/list.jsp做爲最後的響應。服務器
5. 附gradle依賴架構
dependencies { compile group: 'cglib', name: 'cglib', version: '3.2.12' compile group: 'org.springframework', name: 'spring-aop', version: '5.1.5.RELEASE' compile group: 'org.springframework', name: 'spring-context', version: '5.1.7.RELEASE' compile group: 'org.springframework', name: 'spring-webmvc', version: '5.1.5.RELEASE' testCompile group: 'org.springframework', name: 'spring-test', version: '5.1.4.RELEASE' compile group: 'org.springframework', name: 'spring-jdbc', version: '5.1.5.RELEASE' compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2' compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2' compile group: 'org.aspectj', name: 'aspectjweaver', version: '1.9.3' compile group: 'org.aspectj', name: 'aspectjrt', version: '1.9.3' providedCompile group: 'javax.servlet.jsp', name: 'javax.servlet.jsp-api', version: '2.3.3' providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1' compile group: 'javax.servlet', name: 'jstl', version: '1.2' }