SpringMVC后端返回数据到前端代码示例
作者:筱菜鸟 时间:2023-06-20 13:12:47
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数据到html页面
利用ajax接收数据
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));
}
};
}
来源:https://www.cnblogs.com/Rong-Xiu/p/12742494.html
标签:Spring,MVC,后端,数据,前端
0
投稿
猜你喜欢
完美解决PermGen space异常的问题
2023-08-19 07:54:55
jpa多数据源时Hibernate配置自动生成表不生效的解决
2023-04-24 06:19:55
C#使用ICSharpCode.SharpZipLib.dll进行文件的压缩与解压功能
2022-01-09 15:30:45
Android zygote启动流程详解
2023-09-13 07:44:12
如何在Java SpringBoot项目中配置动态数据源你知道吗
2021-07-23 11:10:08
Java编译器用maven打war包出错解决办法
2022-01-18 06:13:29
Java中的BaseTypeHandler自定义类型转换器的使用
2022-03-09 00:34:16
Android控件CardView实现卡片布局
2022-12-05 02:10:32
android初学者必须掌握的Activity状态的四大知识点(必读)
2022-08-31 13:26:31
Java循环队列原理与用法详解
2023-11-13 20:05:36
很详细的android序列化过程Parcelable
2021-09-15 20:03:51
C#中使用快速排序按文件创建时间将文件排序的源码
2023-06-04 08:19:19
Android实现拍照添加时间水印
2023-10-02 14:23:51
android监听返回按钮事件的方法
2021-12-22 04:57:57
SpringBoot使用validation-api实现对枚举类参数校验的方法
2021-09-22 19:21:32
Java调用CXF WebService接口的两种方式实例
2023-11-09 02:25:11
c# 圆形识别方案和直线识别方案的参考示例
2022-03-10 13:44:11
Spring Hystrix熔断报警原理图例解析
2021-12-15 16:44:01
Android操作SQLite数据库(增、删、改、查、分页等)及ListView显示数据的方法详解
2022-11-28 10:03:09
C#中的Lazy如何使用详解
2023-10-25 12:56:34