SpringMVC后端返回数据到前端代码示例
程序员文章站
2022-06-22 10:54:27
1.返回modelandview对象(.jsp)controller代码:package controller;import java.util.list;import javax.annotatio...
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)); } }; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 爬虫计算两张验证图片的距离(情况一)
下一篇: 莽古济是什么结局?她为什么会被凌迟