這篇文章主要講解了SpringMVC后端怎么返回數(shù)據(jù)到前端,內(nèi)容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
公司主營業(yè)務:成都做網(wǎng)站、成都網(wǎng)站制作、移動網(wǎng)站開發(fā)等業(yè)務。幫助企業(yè)客戶真正實現(xiàn)互聯(lián)網(wǎng)宣傳,提高企業(yè)的競爭能力。成都創(chuàng)新互聯(lián)公司是一支青春激揚、勤奮敬業(yè)、活力青春激揚、勤奮敬業(yè)、活力澎湃、和諧高效的團隊。公司秉承以“開放、自由、嚴謹、自律”為核心的企業(yè)文化,感謝他們對我們的高要求,感謝他們從不同領域給我們帶來的挑戰(zhàn),讓我們激情的團隊有機會用頭腦與智慧不斷的給客戶帶來驚喜。成都創(chuàng)新互聯(lián)公司推出臨沭免費做網(wǎng)站回饋大家。
1.返回ModelAndView對象(.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頁面代碼
<%@ 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頁面
利用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)); } }; }
看完上述內(nèi)容,是不是對SpringMVC后端怎么返回數(shù)據(jù)到前端有進一步的了解,如果還想學習更多內(nèi)容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
網(wǎng)站題目:SpringMVC后端怎么返回數(shù)據(jù)到前端
本文URL:http://chinadenli.net/article2/gjcjoc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供電子商務、、用戶體驗、網(wǎng)站設計、企業(yè)網(wǎng)站制作、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)