SpringMVC后端返回数据到前端代码示例
作者:筱菜鸟 发布时间:2023-06-20 13:12:47
标签:Spring,MVC,后端,数据,前端
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
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- 本文实例为大家分享了android中NFC读写功能的具体代码,供大家参考,具体内容如下首先检查一下设备是否支持NFC功能private vo
- CopyOnWriteArrayList介绍它相当于线程安全的ArrayList。和ArrayList一样,它是个可变数组;但是和Array
- 一.内部类的介绍 内部类: 一个类定义在 另一个类 的 内部。 &
- Android 微信摇一摇功能实现,最近学习传感器,就想实现摇一摇的功能,上网查了些资料,就整理下。如有错误,还请指正。开发环境Androi
- 使用mybatis生成逆向工程的详细步骤,我个人感觉这个是最简单的一个了,虽然网上有很多种的方法来生成逆向工程,可是这个方法最简单。在这里我
- 本篇博客我们继续的来聊SpringMVC的东西,下方我们将会聊到js、css这些静态文件的加载配置,以及服务器推送的两种实现方式。当然我们在
- Web服务器是运行及发布Web应用的容器,只有将开发的Web项目放置到该容器中,才能使网络中的所有用户通过浏览器进行访问。开发Java We
- 具体代码如下所示:public class Parent { public static int a = parentStati
- 关于《JavaCV的摄像头实战》系列《JavaCV的摄像头实战》顾名思义,是使用JavaCV框架对摄像头进行各种处理的实战集合,这是欣宸作为
- SpringBoot集成Redis 1.添加redis依赖<dependency> <groupId
- 有了上一节中得到的正则表达式,那么就可以用来构造 NFA 了。NFA 可以很容易的从正则表达式转换而来,也有助于理解正则表达式表示的模式。一
- 新版的IDEA为了防止 pom 更新时,MAVEN 自动导包时卡死的问题,取消了自动导包机制。但新增了导入按钮和快捷键。 问题id
- C#类如下:using System;using System.Collections.Generic;using System.Text;
- 本文实例讲解Android百度地图定位后获取周边位置的实现代码,分享给大家供大家参考,具体内容如下效果图:具体代码:1.布局文件<?x
- 本文实例讲述了Android实现从网络获取图片显示并保存到SD卡的方法。分享给大家供大家参考,具体如下:问题:如何不断获取图片并显示出来,达
- 需要设置允许不安全代码.....项目->属性->生成->允许不安全代码/// <summary> &
- 1.现象描述原来项目在Android studio 2.3一切正常,升级3.0之后报如下错误:Error:Cannot choose bet
- 1.问题项目中有自己企业的通讯录,但是在应用中拨打公司通讯录的联系人,由于手机通讯录中没有相应的信息,只显示一串电话号2 .目的监听系统来电
- 前言上周末我们一起分析了ArrayList的源码并进行了一些总结,因为最近在看Collection这一块的东西,下面的图也是大致的总结了Co
- 今天遇到一个需求,需要处理通过接口传过来的一个参数,参数内容为一个拼接好的Url地址,且该地址还会携带了一些额外的参数,包括但不限于数字,字