springmvc 对ajax 支持,SpringMVC—对Ajax的处理(含 JSON 类型)(3)

五、服务器端的 SpringMVC 如何返回 JSON 类型的字符串。

请求:

fcfa46ced77ecdd4efda4537b441286a.png

$("#testJson8").click(function () {

$.ajax({

url: "testReturnJsonValue",

type: "post",

success: function (result) {

console.log(result);

}

});

});

fcfa46ced77ecdd4efda4537b441286a.png

1.返回单个对象

handler 方法:

fcfa46ced77ecdd4efda4537b441286a.png

@ResponseBody

@RequestMapping("/testReturnJsonValue")public Person testReturnJsonValue() {

Person person = new Person();

person.setName("lily");

person.setAge(23);    return person;

}

fcfa46ced77ecdd4efda4537b441286a.png

在浏览器控制台正常打印了 Person 对象。

注意:这里没有指定 dataType。

2.返回多个对象

handler 方法:

fcfa46ced77ecdd4efda4537b441286a.png

@ResponseBody

@RequestMapping("/testReturnJsonValue")public List testReturnJsonValue() {

List personList = new ArrayList<>();

Person person = new Person();

person.setName("lily");

person.setAge(23);

Person person2 = new Person();

person2.setName("lucy");

person2.setAge(33);

personList.add(person);

personList.add(person2);    return personList;

}

fcfa46ced77ecdd4efda4537b441286a.png

在浏览器控制条正常打印了 Person 数组。

3.返回 Map

fcfa46ced77ecdd4efda4537b441286a.png

@ResponseBody

@RequestMapping("/testReturnJsonValue")public Map testReturnJsonValue() {

Map map = new HashMap<>();

Person person = new Person();

person.setName("lily");

person.setAge(23);

Person person2 = new Person();

person2.setName("lucy");

person2.setAge(33);

map.put("1", person);

map.put("2", person2);    return map;

}

fcfa46ced77ecdd4efda4537b441286a.png

浏览器控制台输出:

fc5271c25dca4a19a2789bb2cfdc18f3.jpg

4.在实际生产环境下的 Ajax 返回值。

封装一个返回值类型:

fcfa46ced77ecdd4efda4537b441286a.png

public class AjaxResult implements Serializable {

public static final String RESULT_CODE_0000 = "0000";

public static final String RESULT_CODE_0001 = "0001";

private String code;

private String message;

private Object data;

public AjaxResult() {

}

public String getCode() {        return this.code;

}

public void setCode(String code) {        this.code = code;

}

public String getMessage() {        return this.message;

}

public void setMessage(String message) {        this.message = message;

}

public Object getData() {        return this.data;

}

public void setData(Object data) {        this.data = data;

}

}

fcfa46ced77ecdd4efda4537b441286a.png

实际使用:

fcfa46ced77ecdd4efda4537b441286a.png

@ResponseBody

@RequestMapping("/testReturnJsonValue")

public AjaxResult testReturnJsonValue() {

AjaxResult ajaxResult = new AjaxResult();

try {

Map map = new HashMap<>();

Person person = new Person();

person.setName("lily");

person.setAge(23);

Person person2 = new Person();

person2.setName("lucy");

person2.setAge(33);

map.put("1", person);

map.put("2", person2);

ajaxResult.setData(map);

ajaxResult.setMessage("success!");

ajaxResult.setCode(AjaxResult.RESULT_CODE_0000);

} catch(Exception e) {

e.printStackTrace();

ajaxResult.setMessage("fail!");

ajaxResult.setCode(AjaxResult.RESULT_CODE_0001);

}    return ajaxResult;

}

fcfa46ced77ecdd4efda4537b441286a.png

87633de24e6640a4b0b1086536a89c3a.jpg

六、Request Payload

(1)出现的条件:

contentType: \'application/json;charset=utf-8\'

type:post

(2)具体请参看

(3)建议尽量不要手动的去处理此种情况,能选用别的方式避免就尽量避免。

七、总结

主要介绍了SpringMVC 对 Ajax 的支持,对与 Ajax 数据如何组织,重点介绍了对表单的支持。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值