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ù)的格式化問題處理
- SpringMVC返回json數(shù)據(jù)的三種方式
- Springmvc如何返回xml及json格式數(shù)據(jù)
相關(guān)文章
Java使用Knife4j優(yōu)化Swagger接口文檔的操作步驟
在現(xiàn)代微服務(wù)開發(fā)中,接口文檔的質(zhì)量直接影響了前后端協(xié)作效率,Swagger 作為一個(gè)主流的接口文檔工具,雖然功能強(qiáng)大,但其默認(rèn)界面和部分功能在實(shí)際使用中略顯不足,而 Knife4j 的出現(xiàn)為我們提供了一種增強(qiáng)的選擇,本篇文章將詳細(xì)介紹如何在項(xiàng)目中集成和使用 Knife4j2024-12-12
解決SpringBoot2.1.0+RocketMQ版本沖突問題
這篇文章主要介紹了解決SpringBoot2.1.0+RocketMQ版本沖突問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Spring Cloud Gateway + Nacos 實(shí)現(xiàn)動(dòng)態(tài)路由
這篇文章主要介紹了Spring Cloud Gateway + Nacos 實(shí)現(xiàn)動(dòng)態(tài)路由的方法,幫助大家實(shí)現(xiàn)路由信息的自動(dòng)更新,感興趣的朋友可以了解下2020-10-10
SpringCloud?Stream?整合RabbitMQ的基本步驟
這篇文章主要介紹了SpringCloud?Stream?整合RabbitMQ的基本步驟,從項(xiàng)目介紹到生產(chǎn)者結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
在Eclipse中部署Spring Boot/Spring Cloud應(yīng)用到阿里云
這篇文章主要介紹了在Eclipse中部署Spring Boot/Spring Cloud應(yīng)用到阿里云,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-12-12
java代碼實(shí)現(xiàn)MD5加密及驗(yàn)證過程詳解
這篇文章主要介紹了java代碼實(shí)現(xiàn)MD5加密及驗(yàn)證過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
java如何實(shí)時(shí)動(dòng)態(tài)獲取properties文件的內(nèi)容
這篇文章主要介紹了java如何實(shí)時(shí)動(dòng)態(tài)獲取properties文件的內(nèi)容,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09

