This commit is contained in:
Данила Мочалов 2023-02-07 11:43:09 +04:00
parent 1721a866fe
commit 8157875d1e
6 changed files with 116 additions and 8 deletions

View File

@ -1,6 +1,6 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.0.2'
id 'org.springframework.boot' version '2.7.8'
id 'io.spring.dependency-management' version '1.1.0'
}

22
front/index.html Normal file
View File

@ -0,0 +1,22 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Главная</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script defer src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
<script src="main.js"></script>
</head>
<body>
<h3 id="date" class="text-center my-3 "></h3>
<input id="ilovetextfield">
<button id = "ilovebutton" onclick="iloveclick()">Готово</button>
<input id="asktextfield">
<button id = "askbutton" onclick="askclick()">Спросить</button>
<input id="uppertextfield">
<button id = "upperbutton" onclick="upperclick()">Поднять</button>
</body>
</html>

36
front/main.js Normal file
View File

@ -0,0 +1,36 @@
fetch("http://127.0.0.1:8080/date")
.then(response => response.text())
.then((response) => {
console.log(response)
document.getElementById("date").textContent = response
})
function iloveclick() {
var text = document.getElementById("ilovetextfield").value
fetch ("http://127.0.0.1:8080/ilove?thing=" + text)
.then(response => response.text())
.then((response) => {
console.log(response)
document.getElementById("ilovetextfield").value = response
})
}
function askclick() {
var text = document.getElementById("asktextfield").value
fetch ("http://127.0.0.1:8080/ask?question=" + text)
.then(response => response.text())
.then((response) => {
console.log(response)
document.getElementById("asktextfield").value = response
})
}
function upperclick() {
var text = document.getElementById("uppertextfield").value
fetch ("http://127.0.0.1:8080/touppercase?content=" + text)
.then(response => response.text())
.then((response) => {
console.log(response)
document.getElementById("uppertextfield").value = response
})
}

View File

@ -2,20 +2,13 @@ package com.webproglabs.lab1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class Lab1Application {
public static void main(String[] args) {
SpringApplication.run(Lab1Application.class, args);
}
@GetMapping("/ilove")
public String ilove(@RequestParam(value = "thing", defaultValue = "cookies") String thing) {
return String.format("I love %s!", thing);
}
}

View File

@ -0,0 +1,13 @@
package com.webproglabs.lab1;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfiguration implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedMethods("*");
}
}

View File

@ -0,0 +1,44 @@
package com.webproglabs.lab1.controllers;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;
@RestController
public class Lab1Controller {
@GetMapping("/ilove")
public String ilove(@RequestParam(value = "thing", defaultValue = "cookies") String thing) {
return String.format("I love %s!", thing);
}
@GetMapping("/ask")
public String question(@RequestParam(value = "question", defaultValue = "Задайте вопрос") String question) {
if (question.contains("Задайте вопрос")) return question;
String[] answers = new String[] {
"Не знаю",
"Да",
"Нет",
"Спросите у мамы"
};
Random random = new Random();
return answers[random.nextInt(4)];
}
@GetMapping("/touppercase")
public String tuupper(@RequestParam(value = "content", defaultValue = "Введите строку") String content) {
return content.toUpperCase();
}
@GetMapping("/date")
public String date () {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime now = LocalDateTime.now();
return dtf.format(now);
}
}