最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java編寫中容易搞錯的一些東西

    Java編寫中容易搞錯的一些東西

    Java編寫中容易搞錯的一些東西...
    2006-12-12
  • Java使用Knife4j優(yōu)化Swagger接口文檔的操作步驟

    Java使用Knife4j優(yōu)化Swagger接口文檔的操作步驟

    在現(xiàn)代微服務(wù)開發(fā)中,接口文檔的質(zhì)量直接影響了前后端協(xié)作效率,Swagger 作為一個主流的接口文檔工具,雖然功能強(qiáng)大,但其默認(rèn)界面和部分功能在實際使用中略顯不足,而 Knife4j 的出現(xiàn)為我們提供了一種增強(qiáng)的選擇,本篇文章將詳細(xì)介紹如何在項目中集成和使用 Knife4j
    2024-12-12
  • 解決SpringBoot2.1.0+RocketMQ版本沖突問題

    解決SpringBoot2.1.0+RocketMQ版本沖突問題

    這篇文章主要介紹了解決SpringBoot2.1.0+RocketMQ版本沖突問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Spring Cloud Gateway + Nacos 實現(xiàn)動態(tài)路由

    Spring Cloud Gateway + Nacos 實現(xiàn)動態(tài)路由

    這篇文章主要介紹了Spring Cloud Gateway + Nacos 實現(xiàn)動態(tài)路由的方法,幫助大家實現(xiàn)路由信息的自動更新,感興趣的朋友可以了解下
    2020-10-10
  • SpringCloud?Stream?整合RabbitMQ的基本步驟

    SpringCloud?Stream?整合RabbitMQ的基本步驟

    這篇文章主要介紹了SpringCloud?Stream?整合RabbitMQ的基本步驟,從項目介紹到生產(chǎn)者結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • SpringCache的基本使用方法

    SpringCache的基本使用方法

    Spring?Cache利用了AOP,實現(xiàn)了基于注解的緩存功能,并且進(jìn)行了合理的抽象,業(yè)務(wù)代碼不用關(guān)心底層是使用了什么緩存框架,只需要簡單地加一個注解,就能實現(xiàn)緩存功能了,本文介紹SpringCache的基本使用方法,感興趣的朋友一起看看吧
    2024-01-01
  • 在Eclipse中部署Spring Boot/Spring Cloud應(yīng)用到阿里云

    在Eclipse中部署Spring Boot/Spring Cloud應(yīng)用到阿里云

    這篇文章主要介紹了在Eclipse中部署Spring Boot/Spring Cloud應(yīng)用到阿里云,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • java代碼實現(xiàn)MD5加密及驗證過程詳解

    java代碼實現(xiàn)MD5加密及驗證過程詳解

    這篇文章主要介紹了java代碼實現(xiàn)MD5加密及驗證過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-10-10
  • java如何實時動態(tài)獲取properties文件的內(nèi)容

    java如何實時動態(tài)獲取properties文件的內(nèi)容

    這篇文章主要介紹了java如何實時動態(tài)獲取properties文件的內(nèi)容,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 詳解MySQL的簡易封裝以及使用

    詳解MySQL的簡易封裝以及使用

    本文主要介紹了MySQL的簡易封裝以及使用。具有一定的參考價值,下面跟著小編一起來看下吧
    2017-01-01

最新評論

应用必备| 滦平县| 镇赉县| 丹寨县| 永仁县| 长岭县| 罗平县| 兰州市| 锦州市| 康平县| 阿图什市| 镶黄旗| 广州市| 长丰县| 泰宁县| 鄂尔多斯市| 长子县| 大荔县| 丰台区| 康定县| 灵宝市| 乌拉特后旗| 德安县| 西乌| 岳池县| 泰和县| 定边县| 阿拉善右旗| 武定县| 靖宇县| 松江区| 阜南县| 晋宁县| 闻喜县| 湖南省| 巴马| 道孚县| 宝坻区| 如东县| 彭州市| 镶黄旗|