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 @@ + + +
+