在Controller中,组装好JSON格式的数据,然后输入到页面,或者通过ajax请求在页面进行解析,都可以做到。
1、Controller
/** * JSON DATA TO PAGE VEIW * @param user * @param request * @return * @throws IOException */ @RequestMapping("/addUser3") public void add3(HttpServletRequest request,HttpServletResponse response) throws IOException{ response.setContentType("application/json;charset=utf-8"); PrintWriter out = response.getWriter(); String str = "{username:‘残缺的孤独‘,password:‘admin123‘}"; out.write(str); }
2、请求
请求 http://localhost:8080/TestSpringMVC3/data/addUser3,结果如下:
时间: 2024-10-19 03:53:46