SpringMVC后端返回數(shù)據(jù)到前端代碼示例
更新時間:2020年04月23日 16:37:13 作者:筱菜鳥
這篇文章主要介紹了SpringMVC后端返回數(shù)據(jù)到前端代碼示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Springmvc處理ajax請求并返回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方法返回值為簡單類型
- 解決SpringMVC 返回Java8 時間JSON數(shù)據(jù)的格式化問題處理
- SpringMVC返回json數(shù)據(jù)的三種方式
- Springmvc如何返回xml及json格式數(shù)據(jù)
相關(guān)文章
Java二維碼登錄流程實現(xiàn)代碼(包含短地址生成,含部分代碼)
近年來,二維碼的使用越來越風(fēng)生水起,本篇文章主要介紹了Java二維碼登錄流程實現(xiàn)代碼,其中包含短地址生成,有興趣的可以了解一下。2016-12-12
MyBatis如何使用PageHelper實現(xiàn)分頁查詢
這篇文章主要介紹了MyBatis如何使用PageHelper實現(xiàn)分頁查詢,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
java高效打印一個二維數(shù)組的實例(不用遞歸,不用兩個for循環(huán))
下面小編就為大家?guī)硪黄猨ava高效打印一個二維數(shù)組的實例(不用遞歸,不用兩個for循環(huán))。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03
java正則表達(dá)式匹配網(wǎng)頁所有網(wǎng)址和鏈接文字的示例
這篇文章主要介紹了java正則表達(dá)式匹配網(wǎng)頁所有網(wǎng)址和鏈接文字java正則表達(dá)式匹配,需要的朋友可以參考下2014-03-03

