我有一個問題,我如何從泉MVC發送JSON對象,以便我可以將其轉換爲我的HTML頁面上的JavaScript對象。Springs MVC JSON響應並將其轉換爲JS對象
以傳統方式執行此操作:下面是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>
所以我要問我該怎麼做相同的泉MVC的東西。如何從Dispatcher Servlet發送JSONObject,並將其轉換爲JSP頁面中的JS對象?
優秀的答案。那正是我在找的東西。 –
歡迎您! – derlinuxer