• @Responsebody与@RequestBody


    预备知识:@RequestMapping
    RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
    @RequestMapping(value = "/aaa")//类级别,可以没有
    public class myController {
        @RequestMapping(value = "/bbb")//方法级别,必须有
        public String getMyName() {
            return "myReturn";
        }
    }
    对应的action就是:<form action="aaa/bbb">
    返回页面就是myReturn.jsp
    
    @Responsebody与@RequestBody
    @Responsebody表示该方法的返回结果直接写入HTTP response body中
    一般在异步获取数据时使用,在使用@RequestMapping后,返回值通常解析为跳转路径,
    加上@Responsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中。
    比如异步获取json数据,加上@Responsebody后,会直接返回json数据。
    @RequestBody将HTTP请求正文插入方法中,使用适合的HttpMessageConverter将请求体写入某个对象。
    function login() {//页面异步请求
        var mydata = '{"name":"' + $('#name').val() + '","id":"'
                + $('#id').val() + '","status":"' + $('#status').val() + '"}';
        $.ajax({
            type : 'POST',
            contentType : 'application/json',
            url : "${pageContext.request.contextPath}/person/login",
            processData : false,
            dataType : 'json',
            data : mydata,
            success : function(data) {
                alert("id: " + data.id + "
    name: " + data.name + "
    status: "
                        + data.status);
            },
            error : function() {
                alert('出错了!');
            }
        });
    };
        @RequestMapping(value = "person/login")
        @ResponseBody
        public Person login(@RequestBody Person person) {//将请求中的mydata写入Person对象中
            return person;//不会被解析为跳转路径,而是直接写入HTTP response body中
        }
    
    扩展:@PathVariable获取请求路径变量
    function profile() {
        var url = "${pageContext.request.contextPath}/person/profile/";
        var query = $('#id').val() + '/' + $('#name').val() + '/'
                + $('#status').val();
        url += query;
        $.get(url, function(data) {
            alert("id: " + data.id + "
    name: " + data.name + "
    status: "
                    + data.status);
        });
    }
        @RequestMapping(value = "person/profile/{id}/{name}/{status}")
        @ResponseBody
        public Person porfile(@PathVariable int id,@PathVariable String name,@PathVariable boolean status) {
            return new Person(id, name, status);
        }
      //@RequestMapping(value = "/person/profile/{id}/{name}/{status}")中的{id}/{name}/{status}与@PathVariable int id, @PathVariable String name,@PathVariable boolean status一一对应,按名匹配。
     
     
     
  • 相关阅读:
    POJ3320 Jessica's Reading Problem
    POJ3320 Jessica's Reading Problem
    CodeForces 813B The Golden Age
    CodeForces 813B The Golden Age
    An impassioned circulation of affection CodeForces
    An impassioned circulation of affection CodeForces
    Codeforces Round #444 (Div. 2) B. Cubes for Masha
    2013=7=21 进制转换
    2013=7=15
    2013=7=14
  • 原文地址:https://www.cnblogs.com/Jeremy2001/p/6807058.html
Copyright © 2020-2023  润新知