SpringMVC 注解式开发
SpringMVC的注解式开发是指,处理器是基于注解的类的开发。对于每一个定义的处理器,无需再配置文件中逐个注册,只需在代码中通过对类与方法的注解,便可完成注册。即注解替换是配置文件中对于处理器的注册部分。
一、第一个注解式开发程序
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>
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 }
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>
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>
二、处理器的请求映射规则的定义
(1)对请求URI的命名空间的定义
A、一个处理器定义多个处理方法
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、命名空间
(2)请求URI中通配符的应用
(3) 对请求提交方式的定义
@RequestMapping(value={"/my.do","hello.do"},method=RequestMethod.POST)
(4)对于请求中携带参数的定义