1. 程式人生 > >學習學習Spring,前後端互動方式

學習學習Spring,前後端互動方式

最近在修煉程式碼,學習了SSM框架,學到了其中一個關鍵點就是前後臺如何互動的,首先要知道,Spring是如何在前端如何輸出內容的,就是通過

@Controller
@RequestMapping("/viewContent")
public class CollegeManagController {
	@Autowired
	private CollegeService collegeService;
	
	@RequestMapping(value="/index",method=RequestMethod.GET)
	@ResponseBody
	private Map<String,Object> getCollegeCategory(HttpServletRequest request){
		Map<String,Object> modelMap=new HashMap<String,Object>();
		List<College> collegeList=new ArrayList<College>();
		try {
			collegeList=collegeService.getCollegeList();
			modelMap.put("collegeList", collegeList);
		}catch(Exception e) {
			modelMap.put("success", false);
			modelMap.put("errMsg", e.toString());
		}
		return modelMap;
		
	}
}

這四個標籤來實現的當瀏覽器訪問localhost:8080/project01/viewContent/index 的時候,頁面就會返回JSON資料

然後前端如何請求這個地址來完成前端現實呢?

通過JavaScript來實現

$(function() {
    $.getJSON(Url, function(data) {
        //TODO
    });
})

Url填寫上面的地址,然後對獲取到的JSON串進行解析處理,完成頁面的顯示。