SpringMVC常用注解:@Controller注解的详细使用方法
2018-01-28 16:09
519 查看
本文欢迎转载,转载前请联系作者,经允许后方可转载。转载后请注明出处,谢谢! http://blog.csdn.net/colton_null 作者:喝酒不骑马 Colton_Null from CSDN
这么做有个复杂的地方在于,一个自定义的Controller类智能处理一个单一请求。而在采用@Contoller注解的方式,可以使接口的定义更加简单,将@Controller标记在某个类上,配合@RequestMapping注解,可以在一个类中定义多个接口,这样使用起来更加灵活。
被@Controller标记的类实际上就是个SpringMVC Controller对象,它是一个控制器类,而@Contoller注解在org.springframework.stereotype包下。其中被@RequestMapping标记的方法会被分发处理器扫描识别,将不同的请求分发到对应的接口上。
首先,要在Spring配置文件的头文件中引入spring-context。
其次,使用
例如
base-package是值是包的路径。意思就是,启动了报扫描功能,将com.myz.controller这个包下以及子包下的所有类扫描一遍,将标记有@Controller、@Service、@repository、@Component等注解的类注入到IOC容器中,作为Spring的Bean来管理。
这样,Spring就能找到Controller类,通过@RequestMapping注解处理对应的请求。
TestController即为一个自定类。在该类的前面标记@Controller,该类就成了一个控制器类。
在浏览器地址栏中输入http://localhost:8080/hello,即对hello()方法进行调用,前台返回hello.jsp界面
● 处理request body的常用注解:@RequestParam, @RequestBody
● 处理request uri的常用注解:@PathVariable
● 处理request header的常用注解:@RequestHeader, @CookieValue
● 处理attribute的常用注解:@SessionAttributes, @ModelAttribute
有关上述注解,会在接下来的博客中详细介绍。
本文内容参考:《Spring + Mybatis企业应用实战》.疯狂软件 著
引言
Spring从2.5版本之后开始引入注解。开发人员可以使用丰富的注解例如@Controller、@RestController、@RequestMapping等。@Controller注解用来做什么的?
以前在编写Controller方法的时候,需要开发者自定义一个Controller类实现Controller接口,实现handleRequest方法返回ModelAndView。并且需要在Spring配置文件中配置Handle,将某个接口与自定义Controller类做映射。这么做有个复杂的地方在于,一个自定义的Controller类智能处理一个单一请求。而在采用@Contoller注解的方式,可以使接口的定义更加简单,将@Controller标记在某个类上,配合@RequestMapping注解,可以在一个类中定义多个接口,这样使用起来更加灵活。
被@Controller标记的类实际上就是个SpringMVC Controller对象,它是一个控制器类,而@Contoller注解在org.springframework.stereotype包下。其中被@RequestMapping标记的方法会被分发处理器扫描识别,将不同的请求分发到对应的接口上。
Spring如何通过注解注入?
不过大家仔细想一下,为什么打上注解了就能实现接口功能了呢?换句话说,Spring怎么样找到开发者自定义的Controller把对应的请求分发到对应的方法上呢?首先,要在Spring配置文件的头文件中引入spring-context。
其次,使用
<context:component-scan>元素启动“包扫描”功能。
例如
<context:component-scan base-package="com.myz.controller"/>
base-package是值是包的路径。意思就是,启动了报扫描功能,将com.myz.controller这个包下以及子包下的所有类扫描一遍,将标记有@Controller、@Service、@repository、@Component等注解的类注入到IOC容器中,作为Spring的Bean来管理。
这样,Spring就能找到Controller类,通过@RequestMapping注解处理对应的请求。
@Controller使用样例
@Controller public class TestController { private static final Log logger = LogFactory.getLog(TestController.class); @RequestMapping(value = "/hello") public ModelAndView hello () { logger.info("hello() 方法被调用"); ModelAndView mv = new ModelAndView(); mv.addObject("message", "Hello, Ma Yuzhe!"); mv.setViewName("/WEB-INF/views/hello.jsp"); return mv; } }
TestController即为一个自定类。在该类的前面标记@Controller,该类就成了一个控制器类。
在浏览器地址栏中输入http://localhost:8080/hello,即对hello()方法进行调用,前台返回hello.jsp界面
用于参数绑定的常用注解
在SpringMVC中用于参数绑定的注解还有很多,它们都在org.springframework.web.bind.annotation包下。● 处理request body的常用注解:@RequestParam, @RequestBody
● 处理request uri的常用注解:@PathVariable
● 处理request header的常用注解:@RequestHeader, @CookieValue
● 处理attribute的常用注解:@SessionAttributes, @ModelAttribute
有关上述注解,会在接下来的博客中详细介绍。
本文内容参考:《Spring + Mybatis企业应用实战》.疯狂软件 著
相关文章推荐
- SpringMVC的controller方法中注解方式传List参数使用@RequestBody
- SpringMVC使用注解完成 URL 和 Controller & 方法之间的映射细节
- SpringMVC使用注解完成 URL 和 Controller & 方法之间的映射细节
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(六): 一些常用的数据库 注解
- SpringMVC常用注解實例詳解1:@Controller,@RequestMapping,@RequestParam,@PathVariable
- SpringMVC拦截器中通过反射得到Controller方法注解时ClassCastException解决方案
- SpringMVC常用注解實例詳解1:@Controller,@RequestMapping,@RequestParam,@PathVariable
- Google常用搜索技巧及"index of"详细使用方法
- springmvc常用注解之@Controller和@RequestMapping
- springmvc常用注解之@Controller和@RequestMapping
- SpringMVC中使用@ResponseBody注解返回值,Ajax取得中文乱码解决方法
- 超详细SpringMVC——使用 Spring 2.5 基于注解驱动的 Spring MVC
- 注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(六): 一些常用的数据库 注解
- springmvc常用注解之@Controller和@RequestMapping
- 【3】springmvc常用注解之@Controller和@RequestMapping
- 转载:注解的力量 -----Spring 2.5 JPA hibernate 使用方法的点滴整理(六): 一些常用的数据库 注解
- SpringMVC常用注解實例詳解1:@Controller,@RequestMapping,@RequestParam,@PathVariable
- SSM框架——SpringMVC中使用@ResponseBody注解返回值,Ajax取得中文乱码解决方法
- 【SpringMVC整合MyBatis】RequestMapping注解与controller方法返回值
- @requestmapping--springmvc注解的使用变量和全局部*替代变量方法