spring-controller

spring-mvc入门:

http://www.cnblogs.com/fangjian0423/p/springMVC-introduction.html



1@RequestBody && @ResponseBody

http://www.jianshu.com/p/7097fea8ce3f

http://snowolf.iteye.com/blog/1628861

应用场景:

1、适用于非键值对的情形,content-Typeapplication/jsonapplication/xml等,但不是application/x-www-form-urlencoded

2、用于简化从request中读取数据和向response写入数据

3、需要配合HttpMessageConverter来使用,spring-config.xml里需要添加注解<mvc:annotation-driven  />

RequestBody依据request.hadercontent-Type来选择;ResponseBody依据request.header.accept值来选择。

4HttpMessageConverter配置在mvc-config里,即配置一个类型是org.springframework.web.servlet.mvc.method.annotation.

RequestMappingHandlerAdapterbean,指定属性messageConverters

5RequestBodyResponseBody可以不同时使用.

6Spring自带的、常用HttpMessageConverter有:StringHttpMessageConverterByteArrayHttpMessageConverterObjectToStringHttpMessageConverterResourceHttpMessageConverter

7、仅限于POST方式





2@RequestParam

http://blog.csdn.net/walkerjong/article/details/7946109

应用场景:

1、适用于键值对的情形,即content-Typeapplication/x-www-form-urlencoded

2、使用方式如下,GETPOST均可以:

    @RequestMapping(method = RequestMethod.GET)

    public String setupForm(@RequestParam(“parameterName”) int petId, ModelMap model) {

        ….

    }


3@RequestHeader && @CookieValue

http://blog.csdn.net/walkerjong/article/details/7946109

应用场景:

1、可以把Request请求header部分的值(cookie中值)绑定到方法的参数上。

2、使用方式如下,GETPOST均可:

@RequestMapping("/displayHeaderInfo.do")

public void displayHeaderInfo(@RequestHeader("Accept-Encoding") String encoding, @CookieValue("JSESSIONID") String cookie, 

                                        @RequestHeader("Keep-Alive") long keepAlive)  {

  ...

}


4@PathVariable 

http://blog.csdn.net/walkerjong/article/details/7946109

应用场景:

1、绑定url路径值到方法的参数上,POST、GET均可。

2、使用方式:

@Controller

@RequestMapping("/owners/{ownerId}")

public class RelativePathUriTemplateController {

  @RequestMapping("/pets/{petId}")

  public void findPet(@PathVariable String ownerId, @PathVariable String petId, Model model) {   

    // implementation omitted

  }

}

5@RequestMapping

http://blog.csdn.net/kobejayandy/article/details/12690041

1、@RequestMapping("/owners/{ownerId}”)等效于@RequestMapping(path="/owners/{ownerId}")

2、@RequestMapping("/owners”, params={"id=215","name!=abc”}, headers={“Host=localhost:8088”,”provider=1”}, 

consumes = {"text/plain", "application/json”}, produces = {"text/plain", "application/*"})

表示:urlPath为/owners 

&& url参数里包含id=215&name!=abc 

&& headers里包含Host=localhost:8080 & provider=1

&& headers里的Content-Type为text/plain 或者application/json

&& headers里的Accept为text/plain 或者 application/*

那么,执行当前RequestMapping指定的方法。

3、method若不指定,那么表示GET和POST均可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值