先看一个简单的实例:java
@Controller @RequestMapping("/hello") public class anyTypeController{ @RequestMapping(method={RequestMethod.GET,RequestMethod.POST}) public String processWebRequest(){ return "hello"; } }
上述例子中简单了阐明了Controller以及RequestMapping的用法。含义是web
将这个class申明为一个bean,当web请求为/hello时,跳转到这个class进行处理。spring
Controller的原型分析:
既然上文说了,此处的不须要再配置文件中配置例如<bean id="xxx" class="com.xxx">的语句,可是咱们的Spring如何知道哪些class是bean文件,哪些class文件不是bean文件呢? 按照Spring教材上所讲,此处全部配置了@Controller 的class文件回经过java的反射机制进行读取,所以在这里Spring2.5官方的 org.springframework.web.servlet.mvc.annotation.defaultAnnotationHandlerMapping 这个类进行处理。Ps:这里想进一步学习的同窗能够查看源码进行分析。这个类的做用是:首先扫描Classpath获取注解了@Controller的对 象(扫面经过语句:<context:component-sacn/>进行扫描)。 以后经过反射机制进行绑定。
mvc
自定义用于基于注解的HandlerAdapter:app
上面咱们已经经过注解的方式配置好了bean,可是咱们bean中方法不仅是一个,咱们怎么知道每次访问的是Controller的哪个具体的方 法呢?具体方法是使用@RequestMapping()注解,而后利用 org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter 的反射机制,将Controller中的具体方法和请求方法名绑定起来。Ps:想了解更深的同窗请看Spring的源码。框架
@RequestMapping
函数
因为在网上搜集到的@RequestMapping的资料很不齐全,看完云里雾里,因而找了本书总结了一下。
学习
@RequestMapping能够被标注类型申明和方法申明这两种方式上。ui
申明在类型定义上,那么@RequestMapping() 括号里面的东西一般为请求的地址
spa
申明在方法定义上,那么它用来代表当面方法定义是一个Web请求处理方法。(Ps:这是书上讲的,我的理解的是加上具体的映射条件,使映射不会出错)
下来看个例子,详细说明:
@Controller @RequestMapping("/hello") public class MyController{ @RequestMapping(method=RequestMethod.GET) public String function1(..){..} @RequestMapping(method=RequestMethod.POST) public String function2(..){..} }
类型定义上标注的@RequestMapping("/hello")的意思是,全部提交到/hello的web请求都有MyController处理。 可是问题来了,假设这个bean里面有不少个函数,到底决定由哪个来处理呢?接下来会使用在方法定义上的@RequestMapping()进行进一步 的缩小范围。如次因此,全部GET方法请求都会被function1处理,POST请求被function2处理。除了method能够区分,咱们还有 param能够区分(由于一个bean里面也不单单只有两个函数。GET和POST并不能使用于全部的地方)。
params属性的两种表达式形式:
1.“参数名=参数值”,一般参数名相同,后面的参数值用来区分而且界定请求的处理范围。实例:
@Controller @RequestMapping("/hello") public class MyController{ @RequestMapping(params="locale=en",{method=RequestMethod.POST}) public String function1(..){..} @RequestMapping(params="locale=ch",method={RequestMethod.POST}) public String function2(..){..} }
这里,当请求为http://xxx/xxx/hello?locale=en 时,调用function1(..)函数
当请求是http://xxx/xxx/hello?locale=ch时,调用function2()函数
2.“paramter” 形式的表达式。 这里判断请求中时候存在某一个参数来决定当前请求交个哪一个方法处理。
@Controller @RequestMapping("/hello") public class MyController{ @RequestMapping(params="delete",{method=RequestMethod.POST}) public String function1(..){..} @RequestMapping(params="update",method={RequestMethod.POST}) public String function2(..){..} }
这里请求能够是http://xxxxxxx/hell?delete 就会访问function1
@RequestMapping还有一一种所有用于方法级别的绑定方式.例:注意,这里用的是value
@Controller public class MyController{ @RequestMapping("/hello1") public String function1(..){...} @RequestMapping(value="/hello2",{method=RequestMethod.POST}) public String function2(..){..} @RequestMapping(value="/hello3",method={RequestMethod.POST}) public String function3(..){..} }
上述仅仅讲了映射的相关知识。下来看若是带有参数的请求应该如何处理。
请求参数到方法参数的绑定
1.默认绑定行为
假设有以下请求:<a href="/hello?age=10?name=jack">
@Controller public class MyController{ @RequestMapping("/hello") pulbic String function1(int age ,String name){ System.out.println(age + name); return "success"; } }
在这里会发现,age 和name自动赋给了函数参数中的age和name。 这是觉得默认绑定行为是根据名称匹配原则进行的函数绑定。当请求中的参数名与方法中的参数名一致,相应的参数值将被绑定到对应的方法参数上(这里框架类保证了请求参数完成了类型转换)。
2.使用@RequestParam明确的指定绑定关系.一样是上面的例子.
@Controller public class MyController{ @RequestMapping("/hello") pulbic String function1(@RequestParam("ageOfJack") int age ,String name){ System.out.println(age + name); return "success"; } }
这时候一样是刚才的web请求,已经能够将age的值赋给了ageOfJack参数。默认状况下,若是@RequestParam请求的数据不存在,会抛 出异常。这是由于他的required属性决定的。他的默认值是true.假设将他设置为false,即该参数不存在时不会报错,对应方法将获取到该类型 的默认值。
pulbic String function1(@RequestParam("ageOfJack",required=false) int age ,String name)
3.添加自定义数据绑定规则。
Spring框架提供的数据绑定才用JavaBean规范的PropertyEditor机制进行数据转换。在基于注解的Controller中,能够用下面两种方式达到这一目的.
1.使用@InitBinder(针对一个Controller)
2.指定自定义的WebBindingInitialize。(这个是针对多个Controller)
这个感受用的比较少,留下记号,遇到再看。