如若转载请注明出处:http://blog.csdn.net/qq_20011175/article/details/76723928 前后端分离式开发无需返回值跳转,直接使用response输出json数据 这种开发方式减少了前后端开发者的冲突。 控制层:
public class ReportAction { private static final long serialVersionUID = 1L; private IReportService reportService; public ReportAction() { reportService = ServiceFactory.getReportInstance(); } public void getReportJson() { String jsonResult = reportService.getReportByDeptno(); System.out.println(jsonResult); try { PrintWriter out = ServletActionContext.getResponse().getWriter(); out.println(jsonResult); } catch (IOException e) { e.printStackTrace(); } } }
Service业务层:@Overridepublic String getReportByDeptno() { String jsonResult = null; try { List<Report> reportList = reportDao.getReportByDeptno(); //目标:通过GSON将该集合变成json格式的字符串,所谓结果 returnGson gson = new Gson(); jsonResult = gson.toJson(reportList, new TypeToken<List<Report>>(){}.getType()); } catch (Exception e) { e.printStackTrace(); } return jsonResult; }
Json--后台将List集合转换成json数据
最新推荐文章于 2024-01-09 09:57:54 发布