前后台交互

xiaoxiao2021-02-28  77

原文

目录

前台发送数据到服务端 使用 form 表单发送同步请求使用ajax发送异步请求,发送的数据为json对象服务端后台(SpringMVC)接收数据 继承HttpServlet类,使用request.getParameter(“name”)方法获取请求参数使用注解@RequestParam直接获取使用@ResponseBody注解来解析json对象服务端后台(SpringMVC)给客户端返回数据 可以继承HttpServlet类,获取请求参数,同时将数据通过流的形式发送到前台。返回的可以是字符串,也可以是json对象。返回页面,Controller中方法返回JSON对象,如果需要携带数据通过ModelAndView(相当于一个Map)传递到view, view中使用jstl的EL表达式来绑定ModelAndView带来的数据。返回Json对象,利用@ResponseBody来实现。spring MVC自动将Java对象转化成了json对象传回了客户端,返回对象可以是Pojo也可以是List直接操作Response自己实现想要的效果。将对象(或数组)转换成json对象(或json数组),发送到前台

1. 前台发送数据到服务端

使用 form 表单发送同步请求 <form action="/test/testRequestParam" method="post"> 参数inputStr:<input type="text" name="inputStr"> 参数intputInt:<input type="text" name="inputInt"> </form> 使用ajax发送异步请求,发送的数据为json对象 <div> 参数username:<input type="text" name="username"> 参数password:<input type="text" name="password"> </div> <scripttype="text/JavaScript"> function test(){ $.ajax({ url:"test.do", type:"POST", cache: false, data:{"username":username,"password":password}, dataType:"json", success:function(data){console.log(data);}, error:function(){} }); } </script>

2. 服务端后台(SpringMVC)接收数据

继承HttpServlet类,使用request.getParameter(“name”)方法获取请求参数 @RequestMapping("testRequestParam") public String test( HttpServletRequest request,HttpServletResponseresponse) { String inputStr=request.getParameter("inputStr") int inputInt = Integer.valueOf(request.getParameter("inputInt")); System.out.println(inputStr+","+inputInt); return "index"; } 使用注解@RequestParam直接获取 @RequestMapping("testRequestParam") public String filesUpload(@RequestParam String inputStr, HttpServletRequest request) { System.out.println(inputStr); int inputInt = Integer.valueOf(request.getParameter("inputInt")); System.out.println(inputInt); return "index"; } 使用@ResponseBody注解来解析json对象 @RequestMapping("/testRequestParam") @ResponseBody public String filesUpload(String inputStr,StringinputInt)throws Exception{ System.out.println(inputStr+","inputInt); return "index"; }

3. 服务端后台(SpringMVC)给客户端返回数据

可以继承HttpServlet类,获取请求参数,同时将数据通过流的形式发送到前台。返回的可以是字符串,也可以是json对象。 @RequestMapping(value="/test.do") public void doPost(HttpServletRequest request,HttpServletResponse response)throws Exception { String username = request.getParameter("username"); String password =request.getParameter("password"); response.setContentType("text/html; charset=utf-8"); //设置响应头 response.setHeader("pragma", "no-cache"); //无缓存 response.setHeader("cache-control", "no-cache"); //无缓存 PrintWriter out = response.getWriter(); out.println(username+","+password); //发送的是字符串 out.flush(); out.close(); } 返回页面,Controller中方法返回JSON对象,如果需要携带数据通过ModelAndView(相当于一个Map)传递到view, view中使用jstl的EL表达式来绑定ModelAndView带来的数据。 @RequestMapping(value="/getView",method=RequestMethod.GET) @ResponseBody public ModelAndViewgetView(String username,String password)throws Exception{ ModelAndView mav=new ModelAndView(); mav.addObject("Object",obj); mav.setViewName("index"); return mav; } 返回Json对象,利用@ResponseBody来实现。spring MVC自动将Java对象转化成了json对象传回了客户端,返回对象可以是Pojo也可以是List直接操作Response自己实现想要的效果。 @RequestMapping(value="/getPojoJson",method=RequestMethod.GET) @ResponseBody public PojogetPojoJson(){ Pojo pojo = new Pojo(); pojo.setPojoName("testName"); pojo.setPojoValue("testValue"); return pojo; } 将对象(或数组)转换成json对象(或json数组),发送到前台 @RequestMapping("/test.do") public void doPost(HttpServletRequestrequest, HttpServletResponse response) throws Exception { response.setCharacterEncoding("UTF-8"); response.setContentType("text/html"); response.setHeader("Access-Control-Allow-Origin","*"); List<Object> objs=baseService.findForList();//查询出来的是对象集合 JSONArray jsonArray =JSONArray.fromObject(objs);//转化成json对象,将对象集合之间转换成了json数组 PrintWriter out = response.getWriter(); out.println(jsonArray); out.flush(); out.close(); }
转载请注明原文地址: https://www.6miu.com/read-43629.html

最新回复(0)