springmvc與ajax傳遞單個物件程式碼、map形式資料,前端顯示資料程式碼
(一)後臺向前臺傳送map形式的資料,前臺獲取Map物件形式顯示在頁面上:
首先在頁面上傳遞一個引數
在jsp頁面中:
<body>
<button onclick="test01()">click</button>
</body>
<script src="lib/jquery/jquery.min.js"></script>
<script>
function test01(){
//路徑
var JsonUrl = "<%=basePath%>selectPexamByPIDQuestionsByMap?PID=3";
$.ajax({
url:JsonUrl,
type:'get',
dataType:'json',
success:function(data){
var array = new Array();
//遍歷,先獲取,然後map在array中是棧,先進後出,因此pop出來。
$.each(data,function(i,val){
array.push(val);
});
var q1 = JSON.stringify(array.pop());
var q2 = JSON.stringify(array.pop());
alert(q1);//彈出資料。
},
error:function(data){
alert("ajax error");
}
});
}
在springmvc的controller中:
//返回一個藉口,這裡是提取試卷考卷的方法
@ResponseBody
@RequestMapping("/selectPexamByPIDQuestionsByMap")
//http://localhost:8888/CourseOnlineProject///selectPexamByPID?PID=8
public Map<String,List<Questions>> selectPexamByPIDQuestionsByMap(@RequestParam("PID")String PID) {
//2.查詢所有根據ID查詢,以map格式返回
List<Questions> questionsList=pExamServiceImpl.selectPexamByPIDQuestions(PID);
//Map<String,List<Questions>> mapQuestions=new HashMap<String,List<Questions>>();
Map<String, List<Questions>> map = new HashMap<>();
for(int i=0;i<questionsList.size();i++){
System.out.println("questionsList.get("+i+")"+questionsList.get(i));
System.out.println("questionsList.get("+i+").getQuestionstype().getQtype()"+questionsList.get(i).getQuestionstype().getQtype());
String typeName = questionsList.get(i).getQuestionstype().getQtype();
if(map.containsKey(typeName)){
List<Questions> qList = map.get(typeName);
qList.add(questionsList.get(i));
map.put(typeName, qList);
}else{
List<Questions> qList = new ArrayList<>();
qList.add(questionsList.get(i));
map.put(typeName, qList);
}
}
return map;//以map形式傳遞給前臺
}
當在瀏覽器輸入://http://localhost:8888/CourseOnlineProject///selectPexamByPID?PID=8
時候,就會獲取到資料如下:(json形式)
{"選擇題":[{"qid":"Q0417645","qquesttion":"歲數大編譯JavaApplication源程式檔案將產生相應的位元組碼檔案,這些位元組碼檔案的副檔名為()。","qchoose":"A.java B.class C..html D.exe ","qrightAnswer":"A","qscore":2}],"判斷題":[{"qid":"Q0260198","qquesttion":"簡答題是對是錯","qchoose":"A. 錯 B.對","qrightAnswer":"A","qscore":6},{"qid":"Q0260198","qquesttion":"簡答題是對是錯","qchoose":"A. 錯 B.對","qrightAnswer":"A","qscore":6},{"qid":"Q5182661","qquesttion":"簡答題是對是錯","qchoose":"A. 錯 B.對","qrightAnswer":"A","qscore":6},{"qid":"Q5207134","qquesttion":"簡答題是對是錯","qchoose":"A. 錯 B.對","qrightAnswer":"A","qscore":6}]}而同時,在開啟頁面的時候,會彈出資料:
(二)通過ajax像後臺傳送單個物件
首先在jsp頁面中,寫一個簡單的測試demo:
<body>
<form>
<input type="text" name="sno"/><br/>
<input type="text" name="sname"/><br/>
<input type="button" onclick="test02()" value="submit"/>
</form>
</body>
<script>
function test02(){
var sno = $("input[name='sno']").val();
var sname = $("input[name='sname']").val();
var jsonUrl = '<%=basePath%>test/test01';
var inputData = {'sno':sno, 'sname':sname}; //傳物件到後臺
$.ajax({
url:jsonUrl,
type:'post',
dataType:'text',
data:inputData,
success:function(data){
alert(data);
},
error:function(){
alert("ajax error");
}
});
}
</script>
在controller中:
@Controller
@RequestMapping(value="/test")
public class TestController0530 {
@ResponseBody
@RequestMapping(value="/test01",method=RequestMethod.POST)
public String test01(@ModelAttribute Student s){
System.out.println(s.getSno()+"--"+s.getSname());
return "success";
}
這時候,在後臺顯示: