jsp界面中将后端的java复杂对象传递给前端js对象的方法:
- 用JSP表达式--<%=%>加JSON数据转换
复杂对象需要转成JSON数据进行前后端传输,所以后端传值前、前端接收对象后都需要进行JSON数据处理。
后端代码:
@RequestMapping(value = "/user/common/defaultReport")
public String defaultReport(HttpServletRequest req, Model model) {
List<ActUserDeptAndFac> audfList = processCusReportService.getUDFList();
req.setAttribute("audfList", audfList);
return "user/activitiCommon/defaultReportView";
}
前端代码jsp代码:
<%@ page import="com.factory.pro.entity.ActUserDeptAndFac" %>
<%@ page import="java.util.List" %>
<%@ page import="net.sf.json.JSONArray" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
List<ActUserDeptAndFac> audfList = (List<ActUserDeptAndFac>) request.getAttribute("audfList");
String audfListStr = JSONArray.fromObject(audfList).toString();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="utf-8">
<title>maaaaaaaaaagic~</title>
</head>
<body>
</body>
<script type="text/javascript">
var reportList = JSON.parse('<%=audfListStr%>');
console.log(reportList)
</script>
</html>