1;这个方法既不能返回数据也不能返回视图,可以响应Ajax异步请求。返回json格式字符串。
首先我们先引入相关的jar包:jackson的jar包来处理把java对象转化成json格式的字符串
com.fasterxml.jackson.core jackson-core2.9.0 com.fasterxml.jackson.core jackson-databind2.9.0
导入js文件,用jquery库来提交ajax请求
发起Ajax请求:
前端控制器接收:
@RequestMapping(value = "/ajax.do",method = RequestMethod.POST) public void ajax(HttpServletResponse response,Integer age,String name) throws IOException { System.out.println(123); Student student = new Student(name,age); System.out.println(name + age); PrintWriter pw = response.getWriter(); ObjectMapper mapper = new ObjectMapper(); String json = mapper.writevalueAsString(student); System.out.println(json); pw.write(json); pw.flush(); pw.close(); }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)