我有一个问题,如何从 Springs MVC 发送 JSON 对象,以便我可以将其转换为我的 HTML 页面上的 JavaScript 对象。
我以传统方式执行此操作:下面是 Java Servlet 的片段,它设置请求属性并将其转发到 JSP 页面。
JSONObject jsonObj = new JSONObject();
jsonObj.put("name","test");
jsonObj.put("age",24);
request.setAttribute("jsonObj",jsonObj);
request.getRequestDispatcher("test.jsp").forward(request,response);
在 JSP 中,我检索为:
<script type="text/javascript">
var jsonObj =<%=request.getAttribute("jsonObj"); %>;
alert("name = "+jsonObj.name+" ; age = "+jsonObj.age); // This will output name and age from the JSON Object
</script>
所以我需要问的是如何在 Springs MVC 中做同样的事情。如何从 Dispatcher Servlet 发送 JSONObject,并将其转换为我的 JSP 页面中的 JS 对象?