|
| 1 | +<%@taglib uri="http://www.springframework.org/tags" prefix="spring" %> |
| 2 | +<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %> |
| 3 | +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> |
| 4 | +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %> |
| 5 | + |
| 6 | +<c:set var="ctx" value="${pageContext.request.contextPath}"/> |
| 7 | + |
| 8 | +<!DOCTYPE html> |
| 9 | +<html lang="en"> |
| 10 | +<head> |
| 11 | + <title>Spring MVC Async</title> |
| 12 | + <link href="<c:url value="/resources/styles/style.css"/>" rel="stylesheet"> |
| 13 | +</head> |
| 14 | +<body> |
| 15 | +<main> |
| 16 | + <h2>Spring MVC Async</h2> |
| 17 | + <div id="rbe"></div> |
| 18 | + <div id="sse"></div> |
| 19 | + <div id="srb"></div> |
| 20 | +</main> |
| 21 | +</body> |
| 22 | +<script> |
| 23 | +
|
| 24 | + /** |
| 25 | + * AJAX Helpers. |
| 26 | + */ |
| 27 | +
|
| 28 | + var xhr = function(url) { |
| 29 | + return new Promise(function(resolve, reject) { |
| 30 | + try { |
| 31 | + var xmhr = new XMLHttpRequest(); |
| 32 | +
|
| 33 | + //Listen for API Response |
| 34 | + xmhr.onreadystatechange = function() { |
| 35 | + if (xmhr.readyState == XMLHttpRequest.DONE && xmhr.status == 200) return resolve(xmhr.responseText); |
| 36 | + }; |
| 37 | +
|
| 38 | + //Open connection |
| 39 | + xmhr.open("GET", url, true); |
| 40 | + //Additional headers as needed |
| 41 | + //x.withCredentials = true; |
| 42 | + //x.setRequestHeader("Accept", "application/json"); |
| 43 | + //x.setRequestHeader("Content-Type", "text/plain"); |
| 44 | +
|
| 45 | + //Perform the actual AJAX call |
| 46 | + xmhr.send(); |
| 47 | +
|
| 48 | + } catch (ex) { |
| 49 | + reject("Exception: Oh CORS's you've made a mistake!"); |
| 50 | + } |
| 51 | + }); |
| 52 | + }; |
| 53 | +
|
| 54 | + /** |
| 55 | + * RBE |
| 56 | + */ |
| 57 | +
|
| 58 | + xhr('http://localhost:8080/rbe').then(function(success){ |
| 59 | + var el = document.getElementById('rbe'); |
| 60 | + el.appendChild(document.createTextNode(success)); |
| 61 | + el.appendChild(document.createElement('br')) |
| 62 | + }); |
| 63 | +
|
| 64 | + /** |
| 65 | + * SSE |
| 66 | + */ |
| 67 | +
|
| 68 | + var sse = new EventSource('http://localhost:8080/sse'); |
| 69 | + sse.onmessage = function (evt) { |
| 70 | + var el = document.getElementById('sse'); |
| 71 | + el.appendChild(document.createTextNode(evt.data)); |
| 72 | + el.appendChild(document.createElement('br')) |
| 73 | + }; |
| 74 | +
|
| 75 | + /** |
| 76 | + * SRB |
| 77 | + */ |
| 78 | +
|
| 79 | + xhr('http://localhost:8080/srb').then(function(success){ |
| 80 | + var el = document.getElementById('srb'); |
| 81 | + el.appendChild(document.createTextNode(success)); |
| 82 | + el.appendChild(document.createElement('br')) |
| 83 | + }); |
| 84 | +
|
| 85 | +</script> |
| 86 | +</html> |
0 commit comments