SpringMVC后端返回?cái)?shù)據(jù)到前端代碼示例
1.返回ModelAndView對(duì)象(.jsp)
controller代碼:
package controller; import java.util.List; import javax.annotation.Resource; import model.Comment; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import service.CommentService; @Controller //@RequestMapping("comment") public class CommentController { @Resource private CommentService commentService; @RequestMapping(value="showComments") public ModelAndView test(){ ModelAndView mav = new ModelAndView(); List<Comment> comments = commentService.selectAllComment(); for(Comment com:comments){ System.out.println(com.getC_text()); } mav.addObject("msg",comments); mav.setViewName("textIndex.jsp"); return mav; } }
jsp頁(yè)面代碼
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>" rel="external nofollow" > <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" > --> </head> <body> <c:forEach items="${msg}" var="com"> ${com.getUid()}:${com.getC_text()}:${com.getC_date()}<br> </c:forEach> </body> </html>
2.返回JSON數(shù)據(jù)到html頁(yè)面
利用ajax接收數(shù)據(jù)
ajax({ method:'post', url:'http://localhost:8080/graduate/showComments.do', data:'null', success:function(response){ console.log(response); } })
controller
@Controller //@RequestMapping("comment") public class CommentController { @Resource private CommentService commentService; @RequestMapping(value="showComments") @ResponseBody public List<Comment> test(){ List<Comment> comments = commentService.selectAllComment(); for(Comment com:comments){ System.out.println(com.getC_text()); } return comments; } }
3.順便記錄一下原生ajax,方便以后使用
function ajax(opt) { opt = opt || {}; opt.method = opt.method.toUpperCase() || 'POST'; opt.url = opt.url || ''; opt.async = opt.async || true; opt.data = opt.data || null; opt.success = opt.success || function () {}; var xmlHttp = null; if (XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } else { xmlHttp = new ActiveXObject('Microsoft.XMLHTTP'); }var params = []; for (var key in opt.data){ params.push(key + '=' + opt.data[key]); } var postData = params.join('&'); if (opt.method.toUpperCase() === 'POST') { xmlHttp.open(opt.method, opt.url, opt.async); xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8'); xmlHttp.send(postData); } else if (opt.method.toUpperCase() === 'GET') { xmlHttp.open(opt.method, opt.url + '?' + postData, opt.async); xmlHttp.send(null); } xmlHttp.onreadystatechange = function () { if (xmlHttp.readyState == 4 && xmlHttp.status == 200) { opt.success(JSON.parse(xmlHttp.responseText)); } }; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Springmvc處理ajax請(qǐng)求并返回json數(shù)據(jù)
- Springmvc 4.x利用@ResponseBody返回Json數(shù)據(jù)的方法
- springMVC返回復(fù)雜的json格式數(shù)據(jù)方法
- SpringMVC中controller返回json數(shù)據(jù)的方法
- springMVC利用FastJson接口返回json數(shù)據(jù)相關(guān)配置詳解
- 詳解springmvc之json數(shù)據(jù)交互controller方法返回值為簡(jiǎn)單類型
- 解決SpringMVC 返回Java8 時(shí)間JSON數(shù)據(jù)的格式化問(wèn)題處理
- SpringMVC返回json數(shù)據(jù)的三種方式
- Springmvc如何返回xml及json格式數(shù)據(jù)
相關(guān)文章
Java實(shí)現(xiàn)拓?fù)渑判虻氖纠a
這篇文章我們要講的是拓?fù)渑判?,這是一個(gè)針對(duì)有向無(wú)環(huán)圖的算法,主要是為了解決前驅(qū)后繼的關(guān)系,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-05-05Java二維碼登錄流程實(shí)現(xiàn)代碼(包含短地址生成,含部分代碼)
近年來(lái),二維碼的使用越來(lái)越風(fēng)生水起,本篇文章主要介紹了Java二維碼登錄流程實(shí)現(xiàn)代碼,其中包含短地址生成,有興趣的可以了解一下。2016-12-12MyBatis如何使用PageHelper實(shí)現(xiàn)分頁(yè)查詢
這篇文章主要介紹了MyBatis如何使用PageHelper實(shí)現(xiàn)分頁(yè)查詢,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11java高效打印一個(gè)二維數(shù)組的實(shí)例(不用遞歸,不用兩個(gè)for循環(huán))
下面小編就為大家?guī)?lái)一篇java高效打印一個(gè)二維數(shù)組的實(shí)例(不用遞歸,不用兩個(gè)for循環(huán))。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03java正則表達(dá)式匹配網(wǎng)頁(yè)所有網(wǎng)址和鏈接文字的示例
這篇文章主要介紹了java正則表達(dá)式匹配網(wǎng)頁(yè)所有網(wǎng)址和鏈接文字java正則表達(dá)式匹配,需要的朋友可以參考下2014-03-03Java中對(duì)list元素進(jìn)行排序的方法詳解
這篇文章主要介紹了Java中對(duì)list元素進(jìn)行排序的方法詳解,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09