☰
-
Php 95
-
Java 94
-
Javascript 37
-
Regex 18
-
Git 17
-
Security 16
-
Docker 07
-
Python 58
-
Machine Learning 14
-
Book Highlights
Java
/
Spring Boot
- 1 Basics 9
-
Classes S
-
Objects S
-
Arrays S
-
Variables S
-
Loops S
-
Numbers S
-
Strings S
-
Exceptions S
-
Regexp S
- 2 OOP 9
-
Inheritance
-
Polymorphism
-
Static S
-
Abstract
-
Interfaces
-
Constructors S
-
Packages
-
Nested Classes
-
Final
- 3 Compiler 2
-
Sublime Text S
-
Apache Ant
- 4 Collections 8
-
Lists
-
Comparable S
-
Sets
-
Maps
-
Generics
-
Properties
-
Streams
-
Json
- 5 Threads 4
-
Create Thread S
-
Sleep
-
Lock
-
Scheduler
- 6 Design Patterns 4
-
Singleton
-
Observer
-
Strategy
-
Mediator
- 7 Swing 12
-
Frame
-
Panel
-
Listener
-
Combo Box
-
Label
-
Image
-
Menu
-
Table
-
Layout
-
Drawing
-
Timer
-
Designer
- 8 I/O 7
-
Streams IO
-
Socket
-
Watching Files
-
Mail
-
Logger
-
Clipboard
-
Encrypt S
- 9 Effective 7
-
Constructors S
-
Dependency Injection
-
Composition
-
Interfaces Default
-
Import Static S
-
Enums
-
Lambdas
- 10 Junit 5
-
About Junit S
-
Test Case
-
Suite Test
-
Annotations
-
Exceptions
- 11 Lambdas 7
-
Expressions S
-
Functional Interfaces
-
Streams
-
Common Operations
-
Default Methods
-
Static Methods S
-
Single Responsibility
- 12 JavaFX 6
-
Openjfx
-
Scene Builder
-
First App
-
Jar Archive
-
On Action
-
Change Listener
- 13 Maven 4
-
Demo
-
Spring Boot
-
Junit
-
Guava
- 14 Spring Boot 8
-
Quick start S
-
Rest service
-
Consuming Rest
-
Templates
-
Security
-
Command Line
-
Scheduling Tasks
-
Ajax
R
Q
Ajax App
Create a new Spring Web project.
package com.example.ajaxapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
The jQuery module consumes the REST service.
The index.html page loads the client into the user's browser.
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
document.addEventListener('DOMContentLoaded', function() {
function crono() {
setTimeout(function() {
$.ajax({
url: "http://localhost:8080/getcurrtime"
}).then(function(data){
$("#crono").html(data.time);
});
crono();
}, 1000);
}
crono();
});
function start() {
$.ajax({
url: "http://localhost:8080/start"
});
}
function stop() {
$.ajax({
url: "http://localhost:8080/stop"
});
}
</script>
</head>
<body>
Get current time (with Ajax):
<a href='javascript: start();'>Start</a> |
<a href='javascript: stop();'>Stop</a>
<div id='crono'></div>
</body>
</html>
The REST controller displays the response as JSON.
package com.example.ajaxapp;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class CronoController {
private Boolean start = true;
private SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
private String lastTime = dateFormat.format(new Date());
@GetMapping("/getcurrtime")
public Crono crono() {
if (start) {
String time = dateFormat.format(new Date());
lastTime = time;
}
return new Crono(lastTime);
}
@GetMapping("/start")
public void start() {
this.start = true;
}
@GetMapping("/stop")
public void stop() {
this.start = false;
}
}
/**
* Data representation class (to return as JSON)
*/
class Crono {
private final String time;
public Crono(String time) {
this.time = time;
}
public String getTime() {
return time;
}
}
Test and run the application.
./mvnw spring-boot:run
http://localhost:8080/
#15:56:30
➥ Questions
Last update: 481 days ago