@ResponseBody和@RequestBody使用

原出处:https://blog.csdn.net/psp0001060/article/details/70193950

本文主要介绍通过ajax提交表单后,@ResponseBody和@RequestBody的使用。

一、概念

@RequestBody

作用: 

   

注解用于将Controller的方法参数,根据HTTP Request Header的content-Type的内容,通过适当的HttpMessageConverter转换为JAVA类

使用时机:

扫描二维码关注公众号,回复: 3051306 查看本文章

A)POST方式提时, 根据request header Content-Type的值来判断:

  •     application/x-www-form-urlencoded, 可选(即非必须,因为这种情况的数据@RequestParam, @ModelAttribute也可以处理,当然@RequestBody也能处理);
  •     multipart/form-data, 不能处理(即使用@RequestBody不能处理这种格式的数据);
  •     其他格式, 必须(其他格式包括application/json, application/xml等。这些格式的数据,必须使用@RequestBody来处理);

B) PUT方式提交时, 根据request header Content-Type的值来判断:

  •     application/x-www-form-urlencoded, 必须;
  •     multipart/form-data, 不能处理;
  •     其他格式, 必须;

说明:request的body部分的数据编码格式由header部分的Content-Type指定;

@ResponseBody

作用: 

      该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。

使用时机:

      返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;

 

二、案例结构

三、关键代码

UserController:

 
  1. package com.edwin.user.controller;

  2.  
  3. import java.util.ArrayList;

  4. import java.util.Date;

  5. import java.util.List;

  6.  
  7. import org.springframework.stereotype.Controller;

  8. import org.springframework.web.bind.annotation.RequestBody;

  9. import org.springframework.web.bind.annotation.RequestMapping;

  10. import org.springframework.web.bind.annotation.RequestMethod;

  11. import org.springframework.web.bind.annotation.ResponseBody;

  12.  
  13. import com.edwin.user.vo.UserVo;

  14.  
  15. @Controller

  16. @RequestMapping("user")

  17. public class UserController {

  18.  
  19. private List<UserVo> userList =new ArrayList<UserVo>();

  20.  
  21. public UserController() {

  22. for (int i = 1; i <=5; i++) {

  23. UserVo uv = new UserVo();

  24. uv.setUserID(i);

  25. uv.setUserName("user"+i);

  26. uv.setGender(i%2);

  27. uv.setPhone("1380013800"+i);

  28. uv.setEmail("email@12"+i+".com");

  29. uv.setBirthday(new Date(90, 2, 23));

  30. uv.setPassword("123456");

  31. userList.add(uv);

  32. }

  33. }

  34.  
  35. @RequestMapping("getUsers.do")

  36. @ResponseBody

  37. public List<UserVo> getUsers(){

  38. return userList;

  39. }

  40.  
  41. /**

  42. * 以POST提交方式,添加一个新用户

  43. * @param vo

  44. * @return

  45. */

  46. @RequestMapping(value="addUser.do", method=RequestMethod.POST,consumes = "application/json")

  47. @ResponseBody

  48. public int addUser(@RequestBody UserVo vo){

  49. System.out.println(vo);

  50. userList.add(vo);

  51. return 1;

  52. }

  53.  
  54. }


 

index.html

 
  1. <!DOCTYPE html>

  2. <html>

  3. <head>

  4. <meta charset="UTF-8">

  5. <title>Insert title here</title>

  6. </head>

  7. <body>

  8. <a href="user/getUsers.do">查看所有用户</a>

  9.  
  10. <a href="insert.html">添加用户</a>

  11. </body>

  12. </html>


 

insert.html

 
  1. <!DOCTYPE html>

  2. <html>

  3. <head>

  4. <meta charset="UTF-8">

  5. <title>Store</title>

  6. <script type="text/javascript" src="js/jquery-1.8.2.min.js"></script>

  7. <script type="text/javascript">

  8.  
  9. function addUser() {

  10. var name = $("#userName").val();

  11. var password = $("#password").val();

  12. var email = $("#email").val();

  13. var phone = $("#phone").val();

  14. var gender = $("#gender").val();

  15. var birthday = $("#birthday").val();

  16.  
  17. var sendInfo = {

  18. userName: name,

  19. password: password,

  20. email: email,

  21. phone: phone,

  22. gender: gender,

  23. birthday: birthday,

  24. };

  25.  
  26. $.ajax({

  27. type: "POST",

  28. url: "user/addUser.do",

  29. contentType : 'application/json', //默认值: "application/x-www-form-urlencoded"。发送信息至服务器时内容编码类型。

  30. dataType: "json",//预期服务器返回的数据类型

  31. success: function (msg) {

  32. if (msg) {

  33. alert(name + " was added in list !");

  34. } else {

  35. alert("Cannot add to list !");

  36. }

  37. },error:function(msg){

  38. alert("failed");

  39. },

  40.  
  41. data: JSON.stringify(sendInfo)

  42. });

  43. }

  44. </script>

  45. </head>

  46. <body>

  47. Post 提交

  48. <form action="" method="post">

  49. userName:<input type="text" id ="userName" value="david"/><br>

  50. password:<input type="text" id ="password" value="111111"/><br>

  51. email:<input type="text" id ="email" value="[email protected]"/><br>

  52. phone:<input type="text" id ="phone" value="13800138002"/><br>

  53. gender:<input type="text" id ="gender" value="1"><br>

  54. birthday:<input type="text" id ="birthday" value="1990-06-30"><br>

  55. <input type="button" value="OK" onclick="addUser()"/>

  56. </form>

  57. </body>

  58. </html>


 

四、效果图

4.1查看所有用户

4.2添加用户

代码下载地址 https://github.com/psp0001060/Learn_SpringMVC.git

猜你喜欢

转载自blog.csdn.net/SecondDream_1017/article/details/81946213