zoukankan      html  css  js  c++  java
  • WebMvc常用注解示例

    WebMvc常用注解示例

    • 注册模型属性: @ModelAttribute
    • 读取请求头: @RequestHeader
    • 读取 Cookie: @CookieValue
    • 校验参数: @Valid 、@Validated
    • 注解处理: @ExceptionHandler
    • 切面通知: @ControllerAdvice

    示例代码

    • HelloWorldController.java
    @Controller
    public class HelloWorldController {
        @RequestMapping("")
        public String index(@RequestParam(required = true) int value, Model model) {
            return "index";
        }
    }
    
    • HelloWorldControllerAdvice.java
    @ControllerAdvice(assignableTypes = HelloWorldController.class)
    public class HelloWorldControllerAdvice {
        @ModelAttribute("acceptLanguage")
        public String acceptLanguage(@RequestHeader("Accept-Language") String acceptLanguage){
            return acceptLanguage;
        }
        @ModelAttribute("jsessionId")
        public String jsessionId(@CookieValue("JSESSIONID") String jsessionId){
            return jsessionId;
        }
        @ModelAttribute("message")
        public String message(){
            return "Hello,World";
        }
        @ExceptionHandler(Throwable.class)
        public ResponseEntity<String> onException(Throwable throwable) {
            return ResponseEntity.ok(throwable.getMessage());
        }
    }
    
  • 相关阅读:
    SpringMvc与Struts2的对比
    Spring 通知
    Spring 之 AOP
    Spring 之 IOC
    Spring IOC 和 AOP概述
    JSON与JAVA数据的相互转换
    (4)-optXXX方法的使用
    (3)-JSONObject的过滤设置
    (2)-生成JSONObject的方法
    String 类型的相关转换
  • 原文地址:https://www.cnblogs.com/fjf3997/p/13041453.html
Copyright © 2011-2022 走看看