springmvc与ajax传递单个对象代码、map形式数据,前端显示数据代码

本文介绍了如何使用Ajax与Spring MVC进行数据交互,包括从后台向前台传递Map形式的数据,并在JSP页面上显示,以及通过Ajax向后台发送单个对象。示例代码详细展示了数据的获取、处理和展示过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(一)后台向前台传送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";
    }


这时候,在后台显示:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值