From 0c0bd9c566a79f3e2d1d290f66b96e8e3f840fa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B0=D1=82=D1=8F=20=D0=98=D1=85=D0=BE=D0=BD=D0=BA?= =?UTF-8?q?=D0=B8=D0=BD=D0=B0?= Date: Sun, 12 Feb 2023 23:57:25 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9A=D0=BE=D0=BC=D0=BC=D0=B8=D1=82=202?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 1 + .../example/demo/Controllers/GController.java | 38 +++++++++++++++++++ .../com/example/demo/DemoApplication.java | 16 +++++++- .../com/example/demo/WebConfiguration.java | 14 +++++++ src/main/resources/application.properties | 1 - src/main/resources/templates/home.html | 14 +++++++ src/main/webapp/WEB-INF/jsp/NewFile.jsp | 12 ++++++ 7 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/example/demo/Controllers/GController.java create mode 100644 src/main/java/com/example/demo/WebConfiguration.java create mode 100644 src/main/resources/templates/home.html create mode 100644 src/main/webapp/WEB-INF/jsp/NewFile.jsp diff --git a/build.gradle b/build.gradle index bd92341..f1219a8 100644 --- a/build.gradle +++ b/build.gradle @@ -14,6 +14,7 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' + implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' testImplementation 'org.springframework.boot:spring-boot-starter-test' } diff --git a/src/main/java/com/example/demo/Controllers/GController.java b/src/main/java/com/example/demo/Controllers/GController.java new file mode 100644 index 0000000..b0cc7f4 --- /dev/null +++ b/src/main/java/com/example/demo/Controllers/GController.java @@ -0,0 +1,38 @@ +package com.example.demo.Controllers; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; + +@Controller +class GController { + + @GetMapping("/") + public String home(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model) { + model.addAttribute("name", name); + return "home"; + } + @GetMapping("/hello") + public String hello(@RequestParam(value = "name", defaultValue = "World") String name, Model model) { + model.addAttribute("name", String.format("Hello %s!", name)); + return "home"; + } + @GetMapping("/sum") + public String doSum(@RequestParam int val1, @RequestParam int val2, Model model){ + model.addAttribute("name", String.format("%s", val1+val2)); + return "home"; + } + @GetMapping("/sub") + public String doSub(@RequestParam int val1, @RequestParam int val2, Model model){ + model.addAttribute("name", String.format("%s", val1-val2)); + return "home"; + } + @GetMapping("/hello2") + public String hello2(@RequestParam(value = "name", defaultValue = "World") String name, + @RequestParam(value = "day", defaultValue = "1") Integer day, + @RequestParam(value = "month", defaultValue = "January") String month, Model model) { + model.addAttribute("name", String.format("Hello %s!Your birthday is %s %s", name,day,month)); + return "home"; + } +} \ No newline at end of file diff --git a/src/main/java/com/example/demo/DemoApplication.java b/src/main/java/com/example/demo/DemoApplication.java index 0a72347..2eb1745 100644 --- a/src/main/java/com/example/demo/DemoApplication.java +++ b/src/main/java/com/example/demo/DemoApplication.java @@ -14,8 +14,22 @@ public class DemoApplication { SpringApplication.run(DemoApplication.class, args); } - @GetMapping("/hello") + /*@GetMapping("/hello") public String hello(@RequestParam(value = "name", defaultValue = "World") String name) { return String.format("Hello %s!", name); } + @GetMapping("/sum") + public Integer doSum(@RequestParam int val1, @RequestParam int val2){ + return val1+val2; + } + @GetMapping("/sub") + public Integer doSub(@RequestParam int val1, @RequestParam int val2){ + return val1-val2; + } + @GetMapping("/hello2") + public String hello2(@RequestParam(value = "name", defaultValue = "World") String name, + @RequestParam(value = "day", defaultValue = "1") Integer day, + @RequestParam(value = "month", defaultValue = "January") String month) { + return String.format("Hello %s!Your birthday is %s %s", name,day,month); + }*/ } diff --git a/src/main/java/com/example/demo/WebConfiguration.java b/src/main/java/com/example/demo/WebConfiguration.java new file mode 100644 index 0000000..d4cd4a6 --- /dev/null +++ b/src/main/java/com/example/demo/WebConfiguration.java @@ -0,0 +1,14 @@ +package com.example.demo; + +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("*"); + } + +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b13789..e69de29 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +0,0 @@ - diff --git a/src/main/resources/templates/home.html b/src/main/resources/templates/home.html new file mode 100644 index 0000000..778daf2 --- /dev/null +++ b/src/main/resources/templates/home.html @@ -0,0 +1,14 @@ + + + + Sarafan + + + + + +

+ + + + diff --git a/src/main/webapp/WEB-INF/jsp/NewFile.jsp b/src/main/webapp/WEB-INF/jsp/NewFile.jsp new file mode 100644 index 0000000..406547c --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/NewFile.jsp @@ -0,0 +1,12 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + +InsertTitleHere + + +

Hello ${name}!

+ + \ No newline at end of file