# 使用注释编写SpringMvc程序
在实际的开发过程中,会创建很多的Controller来满足业务的需求,这样造成的问题是,必须在springamvc.xml配置文件中配置大量的bean,导致程序看起来极其的臃肿,可以通过注解来解决该问题
1:注册扫描器 在配置文件 springmvc.xml中
# 下方所注册的扫描器会扫描 com.doaoao.下的所有包和类
# 还得注解驱动
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 注解驱动 --> <mvc:annotation-driven/> <!-- 组件扫描器,不扫描无法知道该注解的位置 --> <!-- 指定一个包名,系统会扫描该包下的内容,需要注解的类的位置 --> <context:component-scan base-package="com.doaoao.*" />
<!-- 视图解释类 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/jsp/" /> <property name="suffix" value=".jsp" /> </bean> </beans>2:定义处理器 创建一个 Controller 类
package com.doaoao.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; // 使用注解告诉SpringMvc当前类是一个controller @Controller() public class TestController{ // 浏览器中访问的请求 @RequestMapping("/test/test1.do") public ModelAndView handleRequest(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) throws Exception { ModelAndView mv = new ModelAndView(); mv.addObject("hello", "hello first spring mvc"); mv.setViewName("first"); return mv; } }# 补充
@Controller() // @ Controller表示当前类为一个Controller @RequestMapping("/test") //类名上的注解,命名空间namespace public class TestController02 { @RequestMapping("/test1.do") public ModelAndView test1(HttpServletRequest request, HttpServletResponse response) throws Exception{ ModelAndView mv = new ModelAndView(); mv.addObject("hello", "test1"); mv.setViewName("test1"); return mv; } // 当有多个请求可以匹配该方法时,可以写上一个String类型的数组,都指向该方法 @RequestMapping({"/test2.do", "/hello.do"}) public ModelAndView test2(HttpServletRequest request, HttpServletResponse response) throws Exception{ ModelAndView mv = new ModelAndView(); mv.addObject("hello", "test2"); mv.setViewName("test2"); return mv; } }...
# RequestMapping中常用的属性
// 表示请求中只要以test开头的,都会被该方法处理
@RequestMapping("/test*.do")// 表示请求中只要以test结尾的,都会被该方法处理
@RequestMapping("/*test.do")// 表示请求中只能有两级路径,第一级必须是doaoao
@RequestMapping("/doaoao/*/test.do")// 表示在资源test.do的前面,必须以doaoao开头,而其它级的路径包含几级,各级的名称均随意
@RequestMapping("/doaoao/**/test.do")
# 设置请求提交的方式
// @RequestMapping中含有一个属性method,该属性可以设置请求的提交方式
@RequestMapping(value="/test.do",method = RequestMethod.POST) 上方的注解表示,只有当/test.do的提交方式为post时才能执行当前的方法,对于其它的请求不会处理,如果没有写明method,则提交方式均可 RequestMethod为一个枚举类型,该方法中写明了大部分的提交方式(以下为该方法源码) public enum RequestMethod { GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, TRACE; private RequestMethod() { } }
# 请求中携带参数
在RequestMapping中含有一个属性 params,这个属性我们可以指定请求中必须携带的参数和不携带的参数
// 请求中必须携带name和age
@RequestMapping(value="/test.do",params={"name","age"})// 请求中不携带name,携带age
@RequestMapping(value="/test.do",params={"!name","age"})// 请求中必须携带name,且name的值为doaoao和age,age的值为18
@RequestMapping(value="/test.do",params={"name=doaoao","age=18"})// 请求中必须携带name,且name的值不为doaoao
@RequestMapping(value="/test.do",params={"name != doaoao"})...
本笔记参考自:小猴子老师教程 http://www.monkey1024.com