diff --git a/mvc/SpringMVC笔记.md b/mvc/SpringMVC笔记.md new file mode 100644 index 0000000..8c1fed0 --- /dev/null +++ b/mvc/SpringMVC笔记.md @@ -0,0 +1,129 @@ +# SpringMVC笔记 + +## 访问控制 + +请求地址时返回对应的网页文件 + +- `@RestController`用于返回对象格式的内容,在后面会使用`ModelAndView`可以返回网页文件 +- `@Controller`用于返回网页文件 + +### 环境要求 + +```xml + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + + org.springframework.boot + spring-boot-devtools + runtime + true + + + + org.projectlombok + lombok + 1.18.36 + +``` + +### 错误页面设置 + +在这个路径下可以配置错误码要访问的页面,也就是可以自定义页面内容 + +![image-20250122171536808](./images/SpringMVC笔记/image-20250122171536808.png) + +### 使用`@Controller` + +返回需要访问的HTML内容页面,最后返回的字符串就是页面,这个页面位于`templates`目录下 + +```java +@RequestMapping("/use") +@Controller +public class UseController { + + // 带参数访问 + @RequestMapping(value = "hello", method = RequestMethod.GET, params = {"name"}) + public String hello() { + return "hello"; + } + + @GetMapping("jumpPage") + public String jumpPage() { + return "jumpPage"; + } + + @GetMapping("index") + public String quick() { + return "user"; + } + + // 跳转的页面 + @GetMapping("toJump") + public String toJump() { + return "redirect:jumpPage"; + } +} +``` + +如果在使用`@Controller`需要返回JSON内容,需要在控制器方法上加上`@ResponseBody` + +```java +@GetMapping("getJson") +@ResponseBody +public List getJson() { + ArrayList list = new ArrayList<>(); + list.add("a"); + list.add("b"); + list.add("c"); + + return list; +} +``` + +### 使用`@RestController` + +如果使用`@RestController`那么返回的就是JSON对象,但是这时候要想返回网页文件,需要使用`ModelAndView` + +```java +@RequestMapping("userRest") +@RestController +public class UseRestController { + + @GetMapping("page/test") + public ModelAndView test() { + ModelAndView modelAndView = new ModelAndView(); + modelAndView.setViewName("page/test"); + modelAndView.addObject("message", "这是消息内容"); + return modelAndView; + } +} +``` + +我们引入了`thymeleaf`所以有以下内容`

` + +```html + + + + + 使用RestController返回页面信息 + + +

使用RestController返回页面信息

+

+ + +``` \ No newline at end of file diff --git a/mvc/images/SpringMVC笔记/image-20250122171536808.png b/mvc/images/SpringMVC笔记/image-20250122171536808.png new file mode 100644 index 0000000..c7e888c Binary files /dev/null and b/mvc/images/SpringMVC笔记/image-20250122171536808.png differ diff --git a/mvc/pom.xml b/mvc/pom.xml index 22c6259..8ff8f16 100644 --- a/mvc/pom.xml +++ b/mvc/pom.xml @@ -32,23 +32,36 @@ org.springframework.boot - spring-boot-starter-thymeleaf + spring-boot-starter-web org.springframework.boot - spring-boot-starter-web + spring-boot-starter-test + test - + + + org.springframework.boot + spring-boot-starter-thymeleaf + + org.springframework.boot spring-boot-devtools runtime true + - org.springframework.boot - spring-boot-starter-test - test + org.projectlombok + lombok + 1.18.36 + + + + com.github.xiaoymin + knife4j-openapi3-jakarta-spring-boot-starter + 4.5.0 diff --git a/mvc/src/main/java/cn/bunny/mvc/MvcApplication.java b/mvc/src/main/java/cn/bunny/mvc/MvcApplication.java index b393517..5c0b17b 100644 --- a/mvc/src/main/java/cn/bunny/mvc/MvcApplication.java +++ b/mvc/src/main/java/cn/bunny/mvc/MvcApplication.java @@ -5,9 +5,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class MvcApplication { - public static void main(String[] args) { SpringApplication.run(MvcApplication.class, args); } - } diff --git a/mvc/src/main/java/cn/bunny/mvc/configuration/Knife4jConfig.java b/mvc/src/main/java/cn/bunny/mvc/configuration/Knife4jConfig.java new file mode 100644 index 0000000..331f43c --- /dev/null +++ b/mvc/src/main/java/cn/bunny/mvc/configuration/Knife4jConfig.java @@ -0,0 +1,33 @@ +package cn.bunny.mvc.configuration; + +import io.swagger.v3.oas.models.ExternalDocumentation; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.info.Contact; +import io.swagger.v3.oas.models.info.Info; +import io.swagger.v3.oas.models.info.License; +import lombok.extern.slf4j.Slf4j; +import org.springdoc.core.models.GroupedOpenApi; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@Slf4j +public class Knife4jConfig { + @Bean + public OpenAPI openAPI() { + // 作者等信息 + Contact contact = new Contact().name("Bunny").email("1319900154@qq.com").url("http://bunny-web.site"); + // 使用协议 + License license = new License().name("MIT").url("https://MUT.com"); + // 相关信息 + Info info = new Info().title("家庭理财管理系统").description("家庭理财管理系统").version("v1.0.0").contact(contact).license(license).termsOfService("MIT"); + + return new OpenAPI().info(info).externalDocs(new ExternalDocumentation()); + } + + // 管理员相关分类接口 + @Bean + public GroupedOpenApi groupedOpenAdminApi() { + return GroupedOpenApi.builder().group("后台管理").pathsToMatch("/user/**").build(); + } +} diff --git a/mvc/src/main/java/cn/bunny/mvc/controller/UseController.java b/mvc/src/main/java/cn/bunny/mvc/controller/UseController.java new file mode 100644 index 0000000..8347632 --- /dev/null +++ b/mvc/src/main/java/cn/bunny/mvc/controller/UseController.java @@ -0,0 +1,48 @@ +package cn.bunny.mvc.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +import java.util.ArrayList; +import java.util.List; + +@RequestMapping("/use") +@Controller +public class UseController { + + // 带参数访问 + @RequestMapping(value = "hello", method = RequestMethod.GET, params = {"name"}) + public String hello() { + return "hello"; + } + + @GetMapping("jumpPage") + public String jumpPage() { + return "jumpPage"; + } + + @GetMapping("index") + public String quick() { + return "user"; + } + + // 跳转的页面 + @GetMapping("toJump") + public String toJump() { + return "redirect:jumpPage"; + } + + @GetMapping("getJson") + @ResponseBody + public List getJson() { + ArrayList list = new ArrayList<>(); + list.add("a"); + list.add("b"); + list.add("c"); + + return list; + } +} diff --git a/mvc/src/main/java/cn/bunny/mvc/controller/UseRestController.java b/mvc/src/main/java/cn/bunny/mvc/controller/UseRestController.java new file mode 100644 index 0000000..b75156e --- /dev/null +++ b/mvc/src/main/java/cn/bunny/mvc/controller/UseRestController.java @@ -0,0 +1,19 @@ +package cn.bunny.mvc.controller; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.servlet.ModelAndView; + +@RequestMapping("userRest") +@RestController +public class UseRestController { + + @GetMapping("page/test") + public ModelAndView test() { + ModelAndView modelAndView = new ModelAndView(); + modelAndView.setViewName("page/test"); + modelAndView.addObject("message", "这是消息内容"); + return modelAndView; + } +} diff --git a/mvc/src/main/resources/static/error/400.html b/mvc/src/main/resources/static/error/400.html new file mode 100644 index 0000000..beaaf7f --- /dev/null +++ b/mvc/src/main/resources/static/error/400.html @@ -0,0 +1,10 @@ + + + + + 400 + + +

400

+ + \ No newline at end of file diff --git a/mvc/src/main/resources/static/error/404.html b/mvc/src/main/resources/static/error/404.html new file mode 100644 index 0000000..e5f9d7b --- /dev/null +++ b/mvc/src/main/resources/static/error/404.html @@ -0,0 +1,10 @@ + + + + + 404 + + +

404

+ + \ No newline at end of file diff --git a/mvc/src/main/resources/static/error/500.html b/mvc/src/main/resources/static/error/500.html new file mode 100644 index 0000000..6071bf4 --- /dev/null +++ b/mvc/src/main/resources/static/error/500.html @@ -0,0 +1,64 @@ + + + + + 500 - 服务器错误 + + + + +
+

:'(

+

服务器开小差啦!管理员正在修理中...

+

还请阁下静候站点恢复~

+
+ + \ No newline at end of file diff --git a/mvc/src/main/resources/templates/hello.html b/mvc/src/main/resources/templates/hello.html new file mode 100644 index 0000000..05a8084 --- /dev/null +++ b/mvc/src/main/resources/templates/hello.html @@ -0,0 +1,40 @@ + + + + + + hello + + +

Hello

+
+ 带参数显示 +

{{ message }}

+

{{count}}

+ + + +
+ + + \ No newline at end of file diff --git a/mvc/src/main/resources/templates/jumpPage.html b/mvc/src/main/resources/templates/jumpPage.html new file mode 100644 index 0000000..d30c7b7 --- /dev/null +++ b/mvc/src/main/resources/templates/jumpPage.html @@ -0,0 +1,10 @@ + + + + + jumpPage + + +跳转的页面 + + \ No newline at end of file diff --git a/mvc/src/main/resources/templates/page/test.html b/mvc/src/main/resources/templates/page/test.html new file mode 100644 index 0000000..35a78e9 --- /dev/null +++ b/mvc/src/main/resources/templates/page/test.html @@ -0,0 +1,11 @@ + + + + + 使用RestController返回页面信息 + + +

使用RestController返回页面信息

+

+ + \ No newline at end of file diff --git a/mvc/src/main/resources/templates/user.html b/mvc/src/main/resources/templates/user.html new file mode 100644 index 0000000..f385631 --- /dev/null +++ b/mvc/src/main/resources/templates/user.html @@ -0,0 +1,10 @@ + + + + + 测试用户界面 + + +

哈哈哈1ss

+ + \ No newline at end of file