HelloController.java 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package com.sf.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.ui.Model;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. @Controller
  7. @RequestMapping("/thymeleaf")
  8. public class HelloController {
  9. // http://localhost:8080/springmvc_demo/thymeleaf/hello
  10. @GetMapping("/hello")
  11. public String hello(Model model) {
  12. // 通过前缀和后缀拼接后的页面名字
  13. // /WEB-INF/templates/hello.html
  14. model.addAttribute("name", "Hello Thymeleaf");
  15. return "hello";
  16. }
  17. // http://localhost:8080/springmvc_demo/thymeleaf/testThymeleaf
  18. // 转发和重定向
  19. @GetMapping("/testThymeleaf")
  20. public String testThymeleaf() {
  21. // /WEB-INF/templates/test.html
  22. return "test";
  23. }
  24. // http://localhost:8080/springmvc_demo/thymeleaf/testForward
  25. @GetMapping("/testForward")
  26. public String testForward() {
  27. // /WEB-INF/templates/test.html
  28. return "forward:/thymeleaf/testThymeleaf";
  29. }
  30. // http://localhost:8080/springmvc_demo/thymeleaf/testRedirect
  31. @GetMapping("/testRedirect")
  32. public String testRedirect() {
  33. return "redirect:/thymeleaf/testThymeleaf";
  34. }
  35. }