Java之前后端数据交互

1、前台发送数据到服务端,以及接受后台数据

前台发送数据到服务端,有两种方法:

(1)使用<form>表单发送同步请求

<form action="/test/testRequestParam" method="post">    

      参数inputStr:<input type="text" name="inputStr">    

   参数intputInt:<input type="text" name="inputInt">    

</form>  

 

(2)使用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后台控制层获取参数的方式主要有两种,一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取。

(1)继承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";  

}

 

(2)使用注解@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";  

}     

 

(3)使用@ResponseBody注解来解析json对象

@RequestMapping("/testRequestParam") 

@ResponseBody

public String filesUpload(String inputStr,StringinputInt)throws Exception{    

      System.out.println(inputStr+","inputInt);

      return "index";  

}     

3、服务端给客户端返回数据

(1)可以继承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();

}  

 

(2)返回页面,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;

 

(3)返回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; 

(4)将对象(或数组)转换成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();

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值