关于springMVC的响应数据

    技术2022-07-11  92

    一、返回值分类:

    1、响应返回值是string类型

    (1)给出一个实体类User

    public class User implements Serializable{ private String username; private String password; private Integer age; //getset方法 //ToString方法 }

    (2)给出配置文件(web.xml 和 springmvc.xml) (3)给出前台代码

    <a href="user/testString">点击跳转</a>

    (4)给出控制器

    @Controller @RequestMapping("/user") public class UserController { @RequestMapping("/testString") public String testString(Model model) { System.out.print("testString is coming"); //模拟从数据库中查数据 User user = new User(); user.setUsername("lin"); user.setPassword("123"); user.setAge(21); model.addAttribute("user", user); return "success"; } }

    (5)给出success页面

    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %> <html> <head> <title>Title</title> </head> <body> <h3>SpringMVC is coming!</h3> ${user.username} ${user.password} ${user.age} </body> </html>

    分析:response,jsp一发请求,后台(Controller控制器)查数据,把数据存到model对象,它帮我们存到request域里,转发到页面,在success页面里面,从request域里面把数据取出来。

    2、响应返回值是void类型

    当返回值是void类型时,我们就不能直接 return "success";,那又该怎样做呢?? (1)方法1 —— 请求转发

    @RequestMapping("/testVoid") public void testVoid(HttpServletRequest request , HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("/WEB-INF/pages/success.jsp").forward(request, response); return; }

    注意!!!!请求路径一定要写正确!!! 如果使用了请求转发的方式跳转到success.jsp页面,那springmvc.xml中的视图解析器就不需要配置了。

    (2)方法2 —— 重定向 重定向等于又重新发了个新的请求,直接发请求是不能请求/WEB-INF/pages/目录的。WEB-INF里面不能直接(通过重定向)去请求,但是转发可以。

    所以此时不能继续跳转到success页面了,新建一个新的页面hello.jsp UserController.java这么写:

    @RequestMapping("/testVoid") public void testVoid(HttpServletRequest request , HttpServletResponse response) throws ServletException, IOException { response.sendRedirect(request.getContextPath() + "/hello.jsp"); return; }

    (3)方法3 —— 直接响应

    @RequestMapping("/testVoid") public void testVoid(HttpServletRequest request , HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); response.getWriter().write("嘻嘻"); return; }

    使用关键字的方式进行转发或者重定向

    @RequestMapping("/testForwardOrRedirect") public String testForwardOrRedirect(){ System.out.println("testForwardOrRedirect方法执行了..."); // 请求的转发 // return "forward:/WEB-INF/pages/success.jsp"; // 重定向 return "redirect:/hello.jsp"; }

    3、响应返回值是ModelAndView类型

    ModelAndView 是 SpringMVC 为我们提供的一个对象,该对象也可以用作控制器方法的返回值。

    @RequestMapping("/testModelAndView") public ModelAndView testModelAndView(){ // 创建ModelAndView对象 ModelAndView mv = new ModelAndView(); System.out.println("testModelAndView方法执行了..."); // 模拟从数据库中查询出User对象 User user = new User(); user.setUsername("lin"); user.setPassword("456"); user.setAge(21); // 把user对象存储到mv对象中,也会把user对象存入到request对象 mv.addObject("user",user); // 跳转到哪个页面 mv.setViewName("success"); return mv; }

    二、异步的请求方式

    1、初步实现 前后端 的交互,拿到前台json数据,并打印到控制台

    当页面发送异步请求(比如ajax请求),此时后台需要把对象转换成json字符串响应回去。

    <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>主页</title> <script src="js/jquery.min.js"></script> <script> // 页面加载,绑定单击事件 $(function(){ $("#btn").click(function(){ // alert("hello btn"); // 发送ajax请求 $.ajax({ // 编写json格式,设置属性和值 url:"user/testAjax", contentType:"application/json;charset=UTF-8", data:'{"username":"hehe","password":"123","age":30}', dataType:"json", type:"post", success:function(data){ // data服务器端响应的json的数据,进行解析 } }); }); }); </script> </head> <body> <button id="btn">发送ajax的请求</button> </body> </html>

    控制器里面:

    @RequestMapping("/testAjax") public void testAjax(@RequestBody String body){ System.out.println("testAjax方法执行了..."); System.out.println(body); }

    我们之前在web.xml中已经配置了拦截器,所以需要在springmvc.xml中配置,过滤静态资源

    <!--前端控制器,哪些静态资源不拦截--> <mvc:resources location="/js/" mapping="/js/**"/>

    可以看到,此时后台能拿到前台发过来的json数据

    2、把发过来的数据,封装到一个javaBen里面。

    首先需要额外的导包: 1)改写后台代码

    @RequestMapping("/testAjax") public @ResponseBody User testAjax(@RequestBody User user){ System.out.println("testAjax方法执行了..."); // 客户端发送ajax的请求,传的是json字符串,后端把json字符串封装到user对象中 System.out.println(user); // 做响应,模拟查询数据库 user.setUsername("haha"); user.setAge(40); // 做响应 return user; }

    改写前台代码

    Processed: 0.010, SQL: 9