MVC全名是Model View Controller,是模子(model)-視圖(view)-節制器(controller)的縮寫,一種軟件設計典型,用一種營業邏輯、數據、界面顯示分手的方式組織代碼,將營業邏輯堆積到一個部件里面,在改良和個性化心猿意馬制界面及用戶交互的同時,不需要從頭編寫營業邏輯。MVC被怪異的成長起來用于映射傳統的輸入、處置和輸出功能在一個邏輯的圖形化用戶界面的布局中。
第一步:思緒清算。
1、springmvc已經對json數據進行領會析,只要屬性名字和后臺實體類的名字對應上就會主動復制。
第二步:后臺代碼編寫。
package com.example.download;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.util.Map;@RestControllerpublic class JsonController { @RequestMapping("toIndex") public ModelAndView toIndex(HttpServletRequest request, HttpServletResponse response){ //想數據庫請求查詢獲得原始文件名展示 ModelAndView mv = new ModelAndView("index"); mv.addObject("name","火影"); return mv; } @RequestMapping("jsonTest") public void jsonTest(HttpServletRequest request, HttpServletResponse response, User user){ System.out.println(user.getName()); System.out.println(user.getSex()); }}
第三步:前天代碼編寫。
1、引入jquery <script src="../jquery-3.3.1.min.js"></script
2、編寫html和js代碼 <button onclick="testJson()">ajax請求json數據</button> <script> function testJson(){ $.ajax({ type: "GET", dataType: "json", url: '/jsonTest', data: {name:"張三",sex:"男"}, success: function (data) { } }); } </script>
第三步:測試。
1、打開頁面
2、后臺節制臺信息展示,前臺json數據轉為了user對象
0 篇文章
如果覺得我的文章對您有用,請隨意打賞。你的支持將鼓勵我繼續創作!