前兩篇博客咱們講解了基於XML 的入門實例,以及SpringMVC運行的詳細流程。可是咱們發現基於 XML 的配置仍是比較麻煩的,並且,每一個 Handler 類只能有一個方法,在實際開發中確定是不可能這樣來進行開發的。那麼這篇博客咱們就講解實際開發中用的最多的基於註解配置的SpringMVC配置。html
本篇博客源碼下載連接:http://pan.baidu.com/s/1dESLgv3 密碼:vkuy前端
項目結構爲:java
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>SpringMVC_01</display-name> <!-- 配置前端控制器DispatcherServlet --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--springmvc.xml 是本身建立的SpringMVC全局配置文件,用contextConfigLocation做爲參數名來加載 若是不配置 contextConfigLocation,那麼默認加載的是/WEB-INF/servlet名稱-servlet.xml,在這裏也就是 springmvc-servlet.xml --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!--第一種配置:*.do,還能夠寫*.action等等,表示以.do結尾的或者以.action結尾的URL都由前端控制器DispatcherServlet來解析 第二種配置:/,全部訪問的 URL 都由DispatcherServlet來解析,可是這裏最好配置靜態文件不禁DispatcherServlet來解析 錯誤配置:/*,注意這裏是不能這樣配置的,應爲若是這樣寫,最後轉發到 jsp 頁面的時候,仍然會由DispatcherServlet進行解析, 而這時候會找不到對應的Handler,從而報錯!!! --> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--註解處理器映射器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean> <!--註解處理器適配器 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> <!--使用mvc:annotation-driven能夠代替上面的映射器和適配器 這裏面會默認加載不少參數綁定方法,好比json轉換解析器就默認加載,因此優先使用下面的配置 --> <!-- <mvc:annotation-driven></mvc:annotation-driven> --> <!--單個配置Handler --> <!-- <bean class="com.ys.controller.HelloController"></bean> --> <!--批量配置Handler,指定掃描的包全稱 --> <context:component-scan base-package="com.ys.controller"></context:component-scan> <!--配置視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 返回視圖頁面的前綴 --> <property name="prefix" value="/WEB-INF/view/"></property> <!-- 返回頁面的後綴 --> <property name="suffix" value=".jsp"></property> </bean> </beans>
package com.ys.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; //使用@Controller註解表示這個類是一個Handler @Controller public class HelloController { //@RequestMapping註解括號裏面的表示訪問的URL @RequestMapping("hello") public ModelAndView hello(){ ModelAndView modelView = new ModelAndView(); //相似於 request.setAttribute() modelView.addObject("name","張三"); //配置返回的視圖名,因爲咱們在springmvc.xml中配置了前綴和後綴,這裏直接寫視圖名就好 modelView.setViewName("index"); //modelView.setViewName("/WEB-INF/view/index.jsp"); return modelView; } }
注意@Controller註解和@RequestMapping註解的用法web
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> hello:${name} </body> </html>