SpringMVC的註解式開發是指,處理器是基於註解的類的開發。對於每個定義的處理器,無需再配置文件中逐個註冊,只需在代碼中經過對類與方法的註解,即可完成註冊。即註解替換是配置文件中對於處理器的註冊部分。html
1、第一個註解式開發程序java
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 3 <%@page isELIgnored="false" %> 4 <html> 5 <head> 6 <title>welcome page</title> 7 </head> 8 9 <body> 10 ${message} 11 </body> 12 </html>
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
1 import javax.servlet.http.HttpServletRequest; 2 import javax.servlet.http.HttpServletResponse; 3 4 import org.springframework.stereotype.Controller; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.servlet.ModelAndView; 7 8 @Controller //表示當前類是一個處理器 9 public class MyController { 10 11 @RequestMapping("/my.do") 12 public ModelAndView handleRequest(HttpServletRequest request, 13 HttpServletResponse response) throws Exception { 14 // TODO Auto-generated method stub 15 ModelAndView mv= new ModelAndView(); 16 mv.addObject("message","hello SpringMVC World!"); 17 mv.setViewName("/WEB-INF/jsp/welcome.jsp"); 18 return mv; 19 } 20 21 }
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xmlns:mvc="http://www.springframework.org/schema/mvc" 8 xsi:schemaLocation="http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop.xsd 14 http://www.springframework.org/schema/tx 15 http://www.springframework.org/schema/tx/spring-tx.xsd 16 http://www.springframework.org/schema/mvc 17 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 18 <!-- 註冊組件掃描器 --> 19 <context:component-scan base-package="com.jmu.handlers"></context:component-scan> 20 </beans>
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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_2_5.xsd" id="WebApp_ID" version="2.5"> 3 <display-name>01-springmvc-primary</display-name> 4 <servlet> 5 <servlet-name>springMVC</servlet-name> 6 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 7 <init-param> 8 <param-name>contextConfigLocation</param-name> 9 <param-value>classpath:springMVC.xml</param-value> 10 </init-param> 11 <load-on-startup>1</load-on-startup> 12 </servlet> 13 <servlet-mapping> 14 <servlet-name>springMVC</servlet-name> 15 <url-pattern>*.do</url-pattern> 16 </servlet-mapping> 17 <welcome-file-list> 18 <welcome-file>index.jsp</welcome-file> 19 </welcome-file-list> 20 </web-app>
2、處理器的請求映射規則的定義web
(1)對請求URI的命名空間的定義spring
A、一個處理器定義多個處理方法spring-mvc
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
1 @Controller //表示當前類是一個處理器 2 public class MyController{ 3 4 @RequestMapping({"/my.do","hello.do"}) 5 public ModelAndView doFirst(HttpServletRequest request, 6 HttpServletResponse response) throws Exception { 7 // TODO Auto-generated method stub 8 ModelAndView mv= new ModelAndView(); 9 mv.addObject("message","執行doFirst()方法"); 10 mv.setViewName("/WEB-INF/jsp/welcome.jsp"); 11 return mv; 12 } 13 @RequestMapping("/second.do") 14 public ModelAndView Second(HttpServletRequest request, 15 HttpServletResponse response) throws Exception { 16 // TODO Auto-generated method stub 17 ModelAndView mv= new ModelAndView(); 18 mv.addObject("message","執行doSecond()方法"); 19 mv.setViewName("/WEB-INF/jsp/welcome.jsp"); 20 return mv; 21 } 22 23 }
B、命名空間mvc
(2)請求URI中通配符的應用app
(3) 對請求提交方式的定義jsp
@RequestMapping(value={"/my.do","hello.do"},method=RequestMethod.POST)
(4)對於請求中攜帶參數的定義ide