@RequestMapping详解
来源:互联网 发布:数控车床编程论文 编辑:程序博客网 时间:2024/06/05 15:00
一、@RequestMapping
1、value
@RequestMapping(value = "/foos")public String getFoosBySimplePath() { return "Get some Foos";}
URL:http://localhost:8080/foos
2、method
@RequestMapping(value = "/foos", method = RequestMethod.GET)public String postFoos() { return "Post some Foos";}
URL:http://localhost:8080/foos
3、指定http请求头
指定request中必须包含某些指定的header值,才能让该方法处理请求。
@RequestMapping(value = "/foos", headers = "key=val")public String getFoosWithHeader() { return "Get some Foos with Header";}
二、@RequestParam
@RequestParam String inputStr等价于request.getParameter(“inputStr”)
value属性约定传递过来的值必须为value指定的,否则异常
//inputStr必须为hello@RequestParam(value="hello") String inputStr
三、@Responsebody
1、注释在方法上
在使用@RequestMapping后,返回值通常解析为跳转路径,加上@responsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中
2、@RequestBody
将HTTP请求正文转换为适合的HttpMessageConverter对象
四、@RequestHeader
@RequestHeader 注解,可以把Request请求header部分的值绑定到方法的参数上。
@RequestHeader("Host") String host
可以获取host值内容
URL:http://localhost:8080/
得到localhost:8080
五、@CookieValue
@CookieValue 可以把Request header中关于cookie的值绑定到方法的参数上
//JSESSIONID=415A4AC178C59DACE0B2C9CA727CDD84 @RequestMapping("/displayHeaderInfo.do") public void displayHeaderInfo(@CookieValue("JSESSIONID") String cookie) { //... }
六、@ModelAttribute
该注解有两个用法,一个是用于方法上,一个是用于参数上;
用于方法上时: 通常用来在处理@RequestMapping之前,为请求绑定需要从后台查询的model;
用于参数上时: 用来通过名称对应,把相应名称的值绑定到注解的参数bean上;要绑定的值来源于:
A) @SessionAttributes 启用的attribute 对象上;
B) @ModelAttribute 用于方法上时指定的model对象;
C) 上述两种情况都没有时,new一个需要绑定的bean对象,然后把request中按名称对应的方式把值绑定到bean中。
1、用到方法上@ModelAttribute
// Add one attribute// The return value of the method is added to the model under the name "account"// You can customize the name via @ModelAttribute("myAccount")@ModelAttributepublic Account addAccount(@RequestParam String number) { return accountManager.findAccount(number);}// Add multiple attributes@ModelAttributepublic void populateModel(@RequestParam String number, Model model) { model.addAttribute(accountManager.findAccount(number)); // add more ...}
添加一个属性将该方法的返回值添加到model中名称为account
可以通过@ModelAttribute(“myAccount”)自定义名称
2、用在参数上的@ModelAttribute示例代码:
“`
@RequestMapping(value=”/owners/{ownerId}/pets/{petId}/edit”, method = RequestMethod.POST)
public String processSubmit(@ModelAttribute Pet pet) {
}
“`、
首先查询 @SessionAttributes有无绑定的Pet对象,若没有则查询@ModelAttribute方法层面上是否绑定了Pet对象,若没有则将URI template中的值按对应的名称绑定到Pet对象的各属性上
通过@ModelAttribute(“account”)将model名为account属性存储
七、@SessionAttributes
八、@RequestAttribute
九、@InitBinder
十、@JsonView
十一、拦截器
常见场景
1. 日志记录:记录请求信息的日志,以便进行信息监控、信息统计、计算PV(Page View)等。
2. 权限检查:如登录检测,进入处理器检测检测是否登录,如果没有直接返回到登录页面;
3. 性能监控:有时候系统在某段时间莫名其妙的慢,可以通过拦截器在进入处理器之前记录开始时间,在处理完后记录结束时间,从而得到该请求的处理时间(如果有反向代理,如apache可以自动记录);
4. 通用行为:读取cookie得到用户信息并将用户对象放入请求,从而方便后续流程使用,还有如提取Locale、Theme信息等,只要是多个处理器都需要的即可使用拦截器实现。
5. OpenSessionInView:如Hibernate,在进入处理器打开Session,在完成后关闭Session
servlet配置
<mvc:interceptors> <mvc:interceptor> <!--配置拦截目录/**或者不填则为所有controller--> <mvc:mapping path="/**"/> <bean class="Model.TimeBasedAccessInterceptor"/> </mvc:interceptor> </mvc:interceptors>
继承HandlerInterceptorAdapter实现interceptor
public class TimeBasedAccessInterceptor extends HandlerInterceptorAdapter { private int openingTime; private int closingTime; public void setOpeningTime(int openingTime) { this.openingTime = openingTime; } public void setClosingTime(int closingTime) { this.closingTime = closingTime; } /** 执行处理之后调用*/ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("===========HandlerInterceptor1 postHandle"); super.postHandle(request, response, handler, modelAndView); } /** 完成请求时调用*/ @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("===========HandlerInterceptor1 afterCompletion"); super.afterCompletion(request, response, handler, ex); } /** 执行处理之前调用 返回值为true继续,false终端执行*/ public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {// Calendar cal = Calendar.getInstance();// int hour = cal.get(HOUR_OF_DAY);// if (openingTime <= hour && hour < closingTime) {// System.out.println(openingTime+"开放");// return true;// }// System.out.println("完成"); System.out.println("===========HandlerInterceptor1 preHandle"); return true; }}
- 详解@RequestMapping
- @RequestMapping详解
- @RequestMapping详解
- @RequestMapping详解
- [@Controller]2 详解@RequestMapping
- [@Controller]2 详解@RequestMapping
- [@Controller]2 详解@RequestMapping
- [@Controller]2 详解@RequestMapping
- @RequestMapping 用法详解
- [@Controller]2 详解@RequestMapping
- SpringMVC RequestMapping 参数详解
- Spring Mvc @RequestMapping 详解
- SpringMVC RequestMapping注解详解
- spring @RequestMapping参数详解
- @RequestMapping的用法详解
- @RequestMapping 用法详解
- @RequestMapping 用法详解
- @RequestMapping 参数详解
- Android 解决输入法键盘遮盖布局问题,程序员不得不用的方案,快捷!
- 原生JS自己构建一个0-1之间的随机小数
- 手机零件采购
- android输入框在软键盘的上面
- Java线程池
- @RequestMapping详解
- 超详细React Native实现微信好友/朋友圈分享功能-Android/iOS双平台通用
- C#MD5加密算法的实例
- Typora使用手册和Markdown区别
- 实例分析 JavaScript 作用域
- ThreadLocal源码分析(JDK8)
- 锁(二) 信号量 读写信号量 互斥体
- 5个强大的Java分布式缓存框架推荐
- Linux命令之文件与用户权限