一、@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")
@ModelAttribute
public Account addAccount(@RequestParam String number) {
return accountManager.findAccount(number);
}
// Add multiple attributes
@ModelAttribute
public 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;
}
}