响应信息方式如下:
    1.返回值为void,表示不需要框架帮我们处理响应信息,只能通过原生response获取输出流,回写响应信息
    2.返回值为String(没有@ResponseBody注解),表示请求框架帮我们处理响应信息路径的转发或重定向(forward、redirect)
    3.通常发送异步请求(map、list、set……),表示需要框架帮我们处理响应信息,将这些对象转换成JSON形式响应回去
    ①导包image.png
    ②方法上面添加@ResponseBody注解,告知框架帮我们解析

    我们还演示了发送请求如果是JSON形式该如何处理
    发送端设置JSON形式
    接收端(服务端的方法) 参数上面@RequestBody即可 domain对象接收
    index.jsp

    1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2. <html>
    3. <head>
    4. <script type="text/javascript">
    5. window.onload=function () {
    6. var testButtonEle=document.getElementById("testJ");
    7. testButtonEle.onclick=function () {
    8. var xhr=new XMLHttpRequest();
    9. xhr.open("POST","testAJAX.do",true);
    10. //如果想要发送的是json格式,需要告知浏览器解析规则
    11. xhr.setRequestHeader("Content-type","application/json;charset=UTF-8");
    12. xhr.onreadystatechange=function () {
    13. if(xhr.readyState==4&&xhr.status==200){
    14. alert(xhr.responseText);
    15. }
    16. }
    17. //发送请求
    18. xhr.send('{"sid":1,"sname":"klxy"}');
    19. }
    20. }
    21. </script>
    22. </head>
    23. <body>
    24. <input id="testJ" type="button" value="测试JSON">
    25. </body>
    26. </html>

    TestController

    1. package controller;
    2. import domain.Student;
    3. import domain.User;
    4. import org.springframework.stereotype.Controller;
    5. import org.springframework.web.bind.annotation.RequestBody;
    6. import org.springframework.web.bind.annotation.RequestMapping;
    7. import org.springframework.web.bind.annotation.ResponseBody;
    8. import javax.servlet.http.HttpServletResponse;
    9. import java.io.IOException;
    10. import java.util.HashMap;
    11. import java.util.Map;
    12. @Controller
    13. public class TestController{
    14. @ResponseBody
    15. @RequestMapping("testAJAX.do")
    16. public Map testAJAX(@RequestBody Student student){
    17. System.out.println(student);
    18. //经过业务层处理,通常会获得一些结果,并需要我们响应这些结果
    19. //1.此处假设我们查询到一个对象,要将其返回
    20. //Student result=new Student(1,"klxh","女");
    21. //return result;
    22. //2.map集合
    23. Map map=new HashMap();
    24. map.put("sid",1);
    25. map.put("sname","klxx");
    26. map.put("ssex","女");
    27. return map;
    28. //3.list集合......
    29. //想让框架帮我们转化,框架本身没有对应的jar包,需要导包
    30. //且需要告知框架这个返回值类型需要帮我们处理 @ResponseBody
    31. }
    32. }