`
Inmethetiger
  • 浏览: 108453 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

@Controller中的@RequestMapping参数

阅读更多
转载自:http://blog.sina.com.cn/s/blog_6d3c1ec601017q4l.html
下列参数一般都和@RequestMapping配合使用。

A、@CookieValue
org.springframework.web.bind.annotation.CookieValue
public @interface CookieValue
Annotation which indicates that a method parameter should be bound to an HTTP cookie. Supported for annotated handler methods in Servlet and Portlet environments.
这个注释表示一个方法参数绑定到一个HTTP cookie。支持Servlet和Portlet环境。
The method parameter may be declared as type Cookie or as cookie value type (String, int, etc).
这个方法的参数可声明为Cookie类型或String, int等。

A.1、@CookieValue的属性
String value
The name of the cookie to bind to.
绑定的cookie名称。
boolean required
Whether the header is required.
Default is true, leading to an exception being thrown in case the header is missing in the request. Switch this to false if you prefer a null in case of the missing header.
Head是否需要。默认是true,请求中头丢失将抛出一个异常。False,请求中头丢失将返回null。
Alternatively, provide a defaultValue, which implicitly sets this flag to false.
因此,提供一个defaultValue。
String defaultValue
The default value to use as a fallback. Supplying a default value implicitly sets required() to false.
当required为false,请求中头丢失将返回这个值。


B、@PathVariable

Annotation which indicates that a method parameter should be bound to a URI template variable. Supported for RequestMapping annotated handler methods in Servlet environments.
    这个参数指出方法的一个参数绑定到一个URI template变量。在Servlet环境中的被@RequestMapping注释的处理器方法。
B.1、@PathVariable的属性

value
The URI template variable to bind to.
绑定URI template变量。
举例说明
@Controller
public class HelloWorldController {    @RequestMapping("/helloWorld/{userId}")
public String helloWorld(ModelMap model,@PathVariable("userId") String userId) {
       model.addAttribute("attributeName", userId);
       return "helloWorld";
    }
}

当URI template变量和方法的参数名称一样时,可以省略value的定义,@PathVariable达到同样的效果。

C、@RequestBody
Annotation which indicates that a method parameter should be bound to the web request body. Supported for annotated handler methods in Servlet environments.
这个注释它指示一个方法的参数绑定到一个web请求的body。它支持Servlet环境中的注释处理器方法。
举例说明
@Controller
public class HelloWorldController {
    @RequestMapping("/hello.do")   
    public String helloWorld(Model model,@RequestBody String reqBody) {
       model.addAttribute("message", reqBody);
       return "helloWorld";
    }
}

这时这个参数reqBody的值是请求页面的form表单的所有值。

D、@ RequestHeader
Annotation which indicates that a method parameter should be bound to a web request header. Supported for annotated handler methods in Servlet and Portlet environments.
这个注释它指示一个方法的参数绑定到一个web请求的头信息。它支持Servlet和Portlet环境中的注释处理器方法。
D.1、@ RequestHeader的属性
String defaultValue
The default value to use as a fallback.
默认返回值。
Boolean required
Whether the header is required.
是否需要header。
String value
The name of the request header to bind to.
绑定的请求头名称。
举例说明
@Controller
public class HelloWorldController {
    @RequestMapping("/hello.do")   
    public String helloWorld(Model model,@RequestHeader("Accept") String info) {
       model.addAttribute("message", info);
       return "helloWorld";
    }
}

这时这个参数info将获得请求的Accept头信息。

E、@RequestParam
org.springframework.web.bind.annotation.RequestParam
Annotation which indicates that a method parameter should be bound to a web request parameter. Supported for annotated handler methods in Servlet and Portlet environments.
这个参数指出一个方法的参数应绑定到一个web请求的参数。支持Servlet和Portlet环境下注释处理器的方法。
E.1、@RequestParam的属性

E.1.1、value
The name of the request parameter to bind to.
绑定的请求参数的名称。
@RequestParam(value="abc")等同于@RequestParam("abc")
E.1.2、required
Whether the parameter is required.
是否需要参数。
Default is true, leading to an exception thrown in case of the parameter missing in the request. Switch this to false if you prefer a null in case of the parameter missing.
默认为true,若请求中没有参数会导致抛出一个异常。若设置为false,若请求中没有参数就会返回null。
Alternatively, provide a defaultValue, which implicitly sets this flag to false.
在required=false时,最好设置一个defaultValue默认值。
@RequestParam(value = "abc",required=false)

E.1.3、defaultValue
The default value to use as a fallback. Supplying a default value implicitly sets required() to false.
当required=false时,设定默认值。
举例说明
@Controller
@RequestMapping("/a")
public class HelloWorldController {
    @RequestMapping("/b")
    public String helloWorld(Model model,@RequestParam("a") String abc) {
       model.addAttribute("message", abc);
       return "helloWorld";
    }
}


F、@ResponseBody
Annotation which indicates that a method return value should be bound to the web response body. Supported for annotated handler methods in Servlet environments.
这个注释它指示一个方法的返回值应该绑定到一个web响应的body中。它支持Servlet环境中的注释处理器方法。
应用@ResponseBody将会跳过视图处理,而是调用合适HttpMessageConverter,将返回值写入输出流。

举例说明
@Controller
@RequestMapping("/a")
public class HelloWorldController {
    @RequestMapping("/b")
    @ResponseBody
    public String helloWorld() {
       return "helloWorld";
    }
}

或者这样定义
@Controller
public class HelloWorldController {
    @RequestMapping("/a/b")
    public @ResponseBody String helloWorld() {
       return "helloWorld";
    }
}


这时访问/a/b时,不是返回一个view名为helloWorld的视图,而是作出一个响应,其内容为helloWorld。
分享到:
评论

相关推荐

    springboot jpa mysql controller演示

    springboot jpa mysql controller 完整demo演示 ... //将数据放置到ModelAndView对象view中,第二个参数可以是任何java类型 view.addObject("name", name); //返回ModelAndView对象view return view; }

    springMVC技术概述

    常用注解:@Controller @RestController(Controller+ResponseBody) @Service @Transactional @Mapper @AutoWired @RequestMapping--路由 @RequestParam--参数绑定(不同名参数或Map<Object,String>) @...

    Spring注解 - 52注解 - 原稿笔记

    在火狐中显示可能会有问题,大家都是程序员,改个参数就好啦 注解包含: 拦截器 , 过滤器 , 序列化 , @After , @AfterReturning , @AfterThrowing , @annotation , @Around , @Aspect , @Autowired , @Bean , @Before ,...

    Spring2.5_基于注解驱动的SpringMVC

    @RequestMapping 注解中除了 params 属性外,还有一个常用的属性是 method,它可以让 Controller 方法处理特定 HTTP 请求方式的请求,如让一个方法处理 HTTP GET 请求,而另一个方法处理 HTTP POST 请求,如下所示:...

    SpringMVC项目:ALL

    JavaWeb框架——SpringMVC学习过程前端控制器、视图解析器@Controller@RequestMapping(value、method、params)获取请求参数域对象共享数据视图前缀RESTFul文件上传下载拦截器Interceptor异常处理Excepton

    Spring 4 + REST Web Service + JSON Example with Tomcat

    Web服务方法参数包含@RequestParam,该属性具有defaultValue属性 ,该值将为请求中不可用的请求参数分配默认值。 要设置环境,我们可以使用Spring Boot快速启动。WebApplicationInitializer用于替换web.xml设置。...

    SpringBoot视频教程 快速上手

    第一节:@RequestMapping配置url映射 第二节:@Controller处理http请求 第三节:@RestController处理ajax请求 第四节:@PathVariable获取url参数 第五节:@RequestParam获取请求参数 第四章:SpringBoot之Spring ...

    MVC框架(含源码与jar包)

    比较全面的MVC源码分享。用户只要轻松使用几个注解就能完成...@Controller:表示控制器; @RequestMapping:映射url路径的控制器; @RequestParam:表示请求时传递来的参数; @ResponseBody:表示方法返回json数据。

    01.后台管理系统静态页面

    导入静态资源展示后台首页1 功能分析请求的url:/参数:无返回值:逻辑视图String2 Controller/** * 页面跳转controller * */ @Controller public class PageController { @RequestMapping("/") public ...

    carcar.sql

    @RequestMapping(value = "/hello",method = RequestMethod.GET) public String hello(Model model) { model.addAttribute("name", "Dear"); return "hello"; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 2....

    baweiMVC.jar—web层框架

    1、通过@Controller注解为控制器类注入实例(DI,依赖注入); 2、通过@RequestMapping,建立url请求路径与对应处理类及方法的映射关系; 3、通过@RequestParam,为url对应处理方法的参数变量赋值; 4、通过@...

    乐优商城.xmind

    通过 @PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过@PathVariable(“xxx“) 绑定到操作方法的入参中。 select * from tb_category where id in (select ...

    spring-base64-url-decoder:添加 MVC 参数注释和 HandlerMethodArgumentResolver 以启用解码 Base64 编码的 URL 参数

    < dependency> < groupId>de.is24.spring</ groupId> < artifactId>base64-url-decoder</ artifactId> < version>1.0</ version> 用法根据此示例,在 MVC 控制器中使用@DecodedUri注释来注释您的参数: @...

    rest-doclet:启用从Spring MVC REST服务源代码自动生成文档的功能-mvc source code

    用@ RequestParam @PathVariable , @ RequestParam , @ RequestBody注释的参数。 它还记录了以下定制注释: @JsonRequestExample @JsonResponseExample @PossibleResponseStatuses 这些批注在中声明。 使用...

    SpringMvcGuide:一个用来记录SpringMVC细节的项目

    SpringMVC的相关用法主要内容@RequestMapping注解中相关参数的意义参考RequestMappingController类.Controller中方法的参数可以定义的类型统计普通常用的基本参数没有写, 介绍了一些稍微冷门但是还有点用的, 参考...

    springboot学习思维笔记.xmind

    web项目设置在Servlet的context parameter中 事件Application Event 自定义事件,集成ApplicationEvent 定义事件监听器,实现ApplicationListener 使用容器发布事件 Spring高级话题 Spring Aware ...

    annotation-reflect-test

    1、扫描包下的controller注解类 2、扫描controller类型下RequestMapping注解的方法 3、获取方法的请求参数类,并获取参数类中的非静态参数

    SpringMVC学习笔记整合搭建框架

    SpringMVC第一天 框架课程 ...5、设置sql语句中的参数(使用preparedStatement) 6、通过statement执行sql并获取结果 7、对sql执行结果进行解析处理 8、释放资源(resultSet、preparedstatement、connection)

    基于JavaScript的数据可视化实验室后台管理系统源码+项目说明(期末大作业).zip

    一个code200,404来告诉具体状态,用一个const类存不同状态对应的code值,用resultgenerator来按照controller的逻辑修改result的参数(通过setter), 最后result的值和信息通过@RequestMapping指定的路径在访问这个...

Global site tag (gtag.js) - Google Analytics