From 8157875d1e9d765b1a0c34a14471e1bee5a5e121 Mon Sep 17 00:00:00 2001 From: Danila_Mochalov Date: Tue, 7 Feb 2023 11:43:09 +0400 Subject: [PATCH] Lab1 --- build.gradle | 2 +- front/index.html | 22 ++++++++++ front/main.js | 36 +++++++++++++++ .../com/webproglabs/lab1/Lab1Application.java | 7 --- .../webproglabs/lab1/WebConfiguration.java | 13 ++++++ .../lab1/controllers/Lab1Controller.java | 44 +++++++++++++++++++ 6 files changed, 116 insertions(+), 8 deletions(-) create mode 100644 front/index.html create mode 100644 front/main.js create mode 100644 src/main/java/com/webproglabs/lab1/WebConfiguration.java create mode 100644 src/main/java/com/webproglabs/lab1/controllers/Lab1Controller.java diff --git a/build.gradle b/build.gradle index 9d1d051..fa31eb8 100644 --- a/build.gradle +++ b/build.gradle @@ -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' } diff --git a/front/index.html b/front/index.html new file mode 100644 index 0000000..cdc1497 --- /dev/null +++ b/front/index.html @@ -0,0 +1,22 @@ + + + + + Главная + + + + + +

+ + + + + + + + + + + \ No newline at end of file diff --git a/front/main.js b/front/main.js new file mode 100644 index 0000000..323c81c --- /dev/null +++ b/front/main.js @@ -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 + }) +} \ No newline at end of file diff --git a/src/main/java/com/webproglabs/lab1/Lab1Application.java b/src/main/java/com/webproglabs/lab1/Lab1Application.java index c0db9c2..0495784 100644 --- a/src/main/java/com/webproglabs/lab1/Lab1Application.java +++ b/src/main/java/com/webproglabs/lab1/Lab1Application.java @@ -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); - } } diff --git a/src/main/java/com/webproglabs/lab1/WebConfiguration.java b/src/main/java/com/webproglabs/lab1/WebConfiguration.java new file mode 100644 index 0000000..24be522 --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/WebConfiguration.java @@ -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("*"); + } +} \ No newline at end of file diff --git a/src/main/java/com/webproglabs/lab1/controllers/Lab1Controller.java b/src/main/java/com/webproglabs/lab1/controllers/Lab1Controller.java new file mode 100644 index 0000000..c6e280c --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/controllers/Lab1Controller.java @@ -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); + } + + +}