{"id":5838,"date":"2024-04-16T00:42:34","date_gmt":"2024-04-15T16:42:34","guid":{"rendered":"http:\/\/zyg.luchers.cn\/?p=5838"},"modified":"2024-04-16T00:42:34","modified_gmt":"2024-04-15T16:42:34","slug":"%e4%b8%89%e3%80%81springboot3%e6%95%b4%e5%90%88springmvc","status":"publish","type":"post","link":"http:\/\/zyg.luchers.cn\/index.php\/2024\/04\/16\/%e4%b8%89%e3%80%81springboot3%e6%95%b4%e5%90%88springmvc\/","title":{"rendered":"\u4e09\u3001SpringBoot3\u6574\u5408SpringMVC"},"content":{"rendered":"<h1>\u4e09\u3001SpringBoot3\u6574\u5408SpringMVC<\/h1>\n<h2>1\u3001\u600e\u4e48\u5b9e\u73b0<\/h2>\n<p>\u6b65\u9aa4<\/p>\n<ul>\n<li>\u5bfc\u5165\u4f9d\u8d56<\/li>\n<li>\u914d\u7f6e\u542f\u52a8\u7c7b<\/li>\n<li>\u521b\u5efa\u5b9e\u4f53\u7c7b<\/li>\n<li>\u6d4b\u8bd5<\/li>\n<\/ul>\n<h3>1\u3001\u5bfc\u5165\u4f9d\u8d56<\/h3>\n<p>1\u3001pom.xml\u4f9d\u8d56<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n\n&lt;project xmlns=&quot;http:\/\/maven.apache.org\/POM\/4.0.0&quot; xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd&quot;&gt;  \n  &lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;  \n  &lt;groupId&gt;com.atguigu&lt;\/groupId&gt;  \n  &lt;artifactId&gt;springboot-base-springmvc-03&lt;\/artifactId&gt;  \n  &lt;version&gt;1.0-SNAPSHOT&lt;\/version&gt;\n  &lt;packaging&gt;war&lt;\/packaging&gt;\n\n  &lt;parent&gt;\n    &lt;!--        springboot--&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-parent&lt;\/artifactId&gt;\n    &lt;version&gt;3.0.5&lt;\/version&gt;\n  &lt;\/parent&gt;\n\n  &lt;dependencies&gt;\n    &lt;!--web\u5f00\u53d1\u7684\u573a\u666f\u542f\u52a8\u5668--&gt;\n    &lt;dependency&gt;\n      &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n      &lt;artifactId&gt;spring-boot-starter-web&lt;\/artifactId&gt;\n    &lt;\/dependency&gt;\n    &lt;!--lombook\u7684\u4f9d\u8d56--&gt;\n    &lt;dependency&gt;\n      &lt;groupId&gt;org.projectlombok&lt;\/groupId&gt;\n      &lt;artifactId&gt;lombok&lt;\/artifactId&gt;\n      &lt;version&gt;1.18.30&lt;\/version&gt;\n    &lt;\/dependency&gt;\n  &lt;\/dependencies&gt;\n&lt;\/project&gt;\n<\/code><\/pre>\n<h3>2\u3001\u521b\u5efa\u542f\u52a8\u7c7b<\/h3>\n<h4>1\u3001@SpringBootApplication\u6ce8\u89e3<\/h4>\n<p>1\u3001\u8fd9\u4e2a\u5c31\u662f\u7528\u6765\u5f04\u542f\u52a8\u7c7b\u7684\u6ce8\u89e3<\/p>\n<p>2\u3001\u9700\u8981SpringApplication.run(xxxx.class, args);<\/p>\n<p>3\u3001\u5b9e\u73b0<\/p>\n<pre><code class=\"language-java\">package com.atguigu;\n\nimport org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\n\n@SpringBootApplication\npublic class Main {\n    public static void main(String[] args) {\n        SpringApplication.run(Main.class, args);\n        \/\/ \u542f\u52a8\u9879\u76ee\n    }\n\n}<\/code><\/pre>\n<h3>3\u3001\u521b\u5efa\u5b9e\u4f53\u7c7b<\/h3>\n<p>1\u3001\u8fd9\u4e2a\u5b9e\u4f53\u7c7b\u6839\u636e\u4e1a\u52a1\u6240\u9700\u6765\u3002\u6839\u636e\u6570\u636e\u5e93\u6765<\/p>\n<p>2\u3001\u7528lombom\u4e2d\u7684@Data\u6ce8\u89e3<\/p>\n<h3>4\u3001\u521b\u5efacontroller<\/h3>\n<p>1\u3001controller\u548c\u4e4b\u524d\u5199\u7684\u4e00\u6837<\/p>\n<ul>\n<li>@RestController<\/li>\n<li>@RequestMapping()<\/li>\n<li>\u5177\u4f53\u5b9e\u73b0\u65b9\u6cd5<\/li>\n<\/ul>\n<h2>2\u3001Web\u76f8\u5173\u914d\u7f6e<\/h2>\n<h3>1\u30015\u4e2a\u91cd\u8981\u53c2\u6570<\/h3>\n<p>1\u3001\u5728\u8fd9\u4e2a\u6587\u4ef6\u4e2d\uff1aresources\/application.yml<\/p>\n<p>2\u3001\u53c2\u6570<\/p>\n<ul>\n<li>server.port: \u6307\u5b9a\u5e94\u7528\u7a0b\u5e8f\u7684HTTP\u670d\u52a1\u5668\u7aef\u53e3\u53f7\u3002\n<ul>\n<li>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cSpring Boot\u4f7f\u75288080\u4f5c\u4e3a\u9ed8\u8ba4\u7aef\u53e3\u3002<\/li>\n<li>\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bbe\u7f6eserver.port\u6765\u66f4\u6539\u7aef\u53e3\u53f7\u3002<\/li>\n<\/ul>\n<\/li>\n<li>server.servlet.context-path: \u8bbe\u7f6e\u5e94\u7528\u7a0b\u5e8f\u7684\u4e0a\u4e0b\u6587\u8def\u5f84\u3002\n<ul>\n<li>\u8fd9\u662f\u5e94\u7528\u7a0b\u5e8f\u5728URL\u4e2d\u7684\u57fa\u672c\u8def\u5f84\u3002<\/li>\n<li>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4e0a\u4e0b\u6587\u8def\u5f84\u4e3a\u7a7a\u3002<\/li>\n<li>\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bbe\u7f6eserver.servlet.context-path\u5c5e\u6027\u6765\u6307\u5b9a\u81ea\u5b9a\u4e49\u7684\u4e0a\u4e0b\u6587\u8def\u5f84\u3002<\/li>\n<\/ul>\n<\/li>\n<li>spring.mvc.view.prefix  \u548c  spring.mvc.view.suffix: \u8fd9\u4e24\u4e2a\u5c5e\u6027\u7528\u4e8e\u914d\u7f6e\u89c6\u56fe\u89e3\u6790\u5668\u7684\u524d\u7f00\u548c\u540e\u7f00\u3002\n<ul>\n<li>\u89c6\u56fe\u89e3\u6790\u5668\u7528\u4e8e\u89e3\u6790\u63a7\u5236\u5668\u8fd4\u56de\u7684\u89c6\u56fe\u540d\u79f0\uff0c\u5e76\u5c06\u5176\u6620\u5c04\u5230\u5b9e\u9645\u7684\u89c6\u56fe\u9875\u9762\u3002<\/li>\n<li>spring.mvc.view.prefix\u5b9a\u4e49\u89c6\u56fe\u7684\u524d\u7f00<\/li>\n<li>spring.mvc.view.suffix\u5b9a\u4e49\u89c6\u56fe\u7684\u540e\u7f00\u3002<\/li>\n<\/ul>\n<\/li>\n<li>spring.resources.static-locations: \u914d\u7f6e\u9759\u6001\u8d44\u6e90\u7684\u4f4d\u7f6e\u3002\n<ul>\n<li>\u9759\u6001\u8d44\u6e90\u53ef\u4ee5\u662fCSS\u3001JavaScript\u3001\u56fe\u50cf\u7b49\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cSpring Boot\u4f1a\u5c06\u9759\u6001\u8d44\u6e90\u653e\u5728classpath:\/static\u76ee\u5f55\u4e0b\u3002<\/li>\n<li>\u60a8\u53ef\u4ee5\u901a\u8fc7\u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bbe\u7f6espring.resources.static-locations\u5c5e\u6027\u6765\u81ea\u5b9a\u4e49\u9759\u6001\u8d44\u6e90\u7684\u4f4d\u7f6e\u3002<\/li>\n<\/ul>\n<\/li>\n<li>spring.http.encoding.charset\u548cspring.http.encoding.enabled: \u8fd9\u4e24\u4e2a\u5c5e\u6027\u7528\u4e8e\u914d\u7f6eHTTP\u8bf7\u6c42\u548c\u54cd\u5e94\u7684\u5b57\u7b26\u7f16\u7801\u3002\n<ul>\n<li>spring.http.encoding.charset\u5b9a\u4e49\u5b57\u7b26\u7f16\u7801\u7684\u540d\u79f0\uff08\u4f8b\u5982UTF-8\uff09<\/li>\n<li>spring.http.encoding.enabled\u7528\u4e8e\u542f\u7528\u6216\u7981\u7528\u5b57\u7b26\u7f16\u7801\u7684\u81ea\u52a8\u914d\u7f6e\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>3\u3001\u4f8b\u5b50<\/p>\n<pre><code class=\"language-yaml\"># web\u76f8\u5173\u7684\u914d\u7f6e\n# https:\/\/docs.spring.io\/spring-boot\/docs\/current\/reference\/html\/application-properties.html#appendix.application-properties.server\nserver:\n  # \u7aef\u53e3\u53f7\u8bbe\u7f6e\n  port: 80\n  # \u9879\u76ee\u6839\u8def\u5f84\n  servlet:\n    context-path: \/boot<\/code><\/pre>\n<h2>3\u3001\u9759\u6001\u8d44\u6e90\u5904\u7406<\/h2>\n<h3>1\u3001\u9700\u6c42<\/h3>\n<p>1\u3001\u73b0\u5728\u4f7f\u7528Spring Boot\u505a\u5f00\u53d1 , \u9879\u76ee\u4e2d\u6ca1\u6709webapp\u76ee\u5f55 , \u6211\u4eec\u7684\u9879\u76ee\u662f\u4e00\u4e2ajar\u5de5\u7a0b\uff0c\u90a3\u4e48\u5c31\u6ca1\u6709webapp\uff0c\u6211\u4eec\u7684\u9759\u6001\u8d44\u6e90\u8be5\u653e\u54ea\u91cc\u5462\uff1f<\/p>\n<h3>2\u3001\u8def\u5f84<\/h3>\n<h4>1\u3001\u9ed8\u8ba4\u8def\u5f84<\/h4>\n<p>1\u3001\u9ed8\u8ba4\u7684\u9759\u6001\u8d44\u6e90\u8def\u5f84\u4e3a\uff1a<\/p>\n<ul>\n<li>\n<p><strong>\u00b7 classpath:\/META-INF\/resources\/<\/strong><\/p>\n<\/li>\n<li>\n<p><strong>\u00b7 classpath:\/resources\/<\/strong><\/p>\n<\/li>\n<li>\n<p><strong>\u00b7 classpath:\/static\/<\/strong><\/p>\n<\/li>\n<li>\n<p><strong>\u00b7 classpath:\/public\/<\/strong><\/p>\n<\/li>\n<\/ul>\n<p>2\u3001\u6211\u4eec\u53ea\u8981\u9759\u6001\u8d44\u6e90\u653e\u5728\u8fd9\u4e9b\u76ee\u5f55\u4e2d\u4efb\u4f55\u4e00\u4e2a\uff0cSpringMVC\u90fd\u4f1a\u5e2e\u6211\u4eec\u5904\u7406\u3002 \u6211\u4eec\u4e60\u60ef\u4f1a\u628a\u9759\u6001\u8d44\u6e90\u653e\u5728classpath:\/static\/ \u76ee\u5f55\u4e0b\u3002\u5728resources\u76ee\u5f55\u4e0b\u521b\u5efaindex.html\u6587\u4ef6<\/p>\n<p>3\u3001spring\u6e90\u7801<\/p>\n<pre><code class=\"language-java\">package org.springframework.boot.autoconfigure.web;\n\/\/..................\npublic static class Resources {\n        private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{&quot;classpath:\/META-INF\/resources\/&quot;, &quot;classpath:\/resources\/&quot;, &quot;classpath:\/static\/&quot;, &quot;classpath:\/public\/&quot;};\n        private String[] staticLocations;\n        private boolean addMappings;\n        private boolean customized;\n        private final Chain chain;\n        private final Cache cache;\n\n        public Resources() {\n            this.staticLocations = CLASSPATH_RESOURCE_LOCATIONS;\n            this.addMappings = true;\n            this.customized = false;\n            this.chain = new Chain();\n            this.cache = new Cache();\n        }\n\/\/...........        <\/code><\/pre>\n<p>4\u3001\u4f8b\u5b50<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/04\/image-20240121200503705.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/04\/image-20240121200503705.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20240121200503705\" \/><\/div><\/p>\n<h4>2\u3001\u8986\u76d6\u8def\u5f84<\/h4>\n<p>1\u3001\u5c31\u662f\u53bbyaml\u4e2d\u7684Web\u76f8\u5173\u914d\u7f6e\u5316\u603b\u8fdb\u884c\u8bbe\u7f6e<\/p>\n<p>2\u3001spring.resources.static-locations: \u914d\u7f6e\u9759\u6001\u8d44\u6e90\u7684\u4f4d\u7f6e\u3002<\/p>\n<p>3\u3001\u4f8b\u5b50<\/p>\n<pre><code class=\"language-yaml\"># web\u76f8\u5173\u7684\u914d\u7f6e\n# https:\/\/docs.spring.io\/spring-boot\/docs\/current\/reference\/html\/application-properties.html#appendix.application-properties.server\nserver:\n  # \u7aef\u53e3\u53f7\u8bbe\u7f6e\n  port: 80\n  # \u9879\u76ee\u6839\u8def\u5f84\n  servlet:\n    context-path: \/boot\nspring:\n  web:\n    resources:\n      # \u914d\u7f6e\u9759\u6001\u8d44\u6e90\u5730\u5740,\u5982\u679c\u8bbe\u7f6e,\u4f1a\u8986\u76d6\u9ed8\u8ba4\u503c\n      static-locations: classpath:\/webapp<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/04\/image-20240121200639451.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"http:\/\/zyg.luchers.cn\/wp-content\/uploads\/2024\/04\/image-20240121200639451.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"image-20240121200639451\" \/><\/div><\/p>\n<h2>4\u3001\u81ea\u5b9a\u4e49\u62e6\u622a\u5668\uff08SpringMvc\u914d\u7f6e\uff09<\/h2>\n<h3>1\u3001\u914d\u7f6e\u8bf4\u660e<\/h3>\n<p>1\u3001\u53ea\u8981\u4fdd\u8bc1\u8be5\u914d\u7f6e\u5728\u542f\u52a8\u7c7b\u7684\u540c\u5305\u6216\u8005\u5b50\u5305\u65b9\u53ef\u751f\u6548\uff01<\/p>\n<p>2\u3001WebMvcConfig.java<\/p>\n<pre><code class=\"language-java\">package com.atguigu.config;\n\nimport com.atguigu.interceptor.MyInterceptor;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.context.annotation.Configuration;\nimport org.springframework.web.servlet.config.annotation.InterceptorRegistry;\nimport org.springframework.web.servlet.config.annotation.WebMvcConfigurer;\n\n@Configuration\npublic class MvcConfig implements WebMvcConfigurer {\n\n    @Autowired\n    private MyInterceptor myInterceptor ;\n\n    \/**\n     * \/**  \u62e6\u622a\u5f53\u524d\u76ee\u5f55\u53ca\u5b50\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u8def\u5f84 \/user\/**   \/user\/findAll  \/user\/order\/findAll\n     * \/*   \u62e6\u622a\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u4ee5\u53ca\u5b50\u8def\u5f84   \/user\/*     \/user\/findAll\n     * @param registry\n     *\/\n    @Override\n    public void addInterceptors(InterceptorRegistry registry) {\n        registry.addInterceptor(myInterceptor).addPathPatterns(&quot;\/**&quot;);\n    }\n}<\/code><\/pre>\n<h3>2\u3001\u62e6\u622a\u5668\u58f0\u660e<\/h3>\n<p>1\u3001\u58f0\u660e\u62e6\u622a\u5668<\/p>\n<pre><code class=\"language-java\">package com.atguigu.interceptor;\n\nimport jakarta.servlet.http.HttpServletRequest;\nimport jakarta.servlet.http.HttpServletResponse;\nimport org.springframework.stereotype.Component;\nimport org.springframework.web.servlet.HandlerInterceptor;\nimport org.springframework.web.servlet.ModelAndView;\n\n@Component\npublic class MyInterceptor implements HandlerInterceptor {\n    @Override\n    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {\n        System.out.println(&quot;MyInterceptor\u62e6\u622a\u5668\u7684preHandle\u65b9\u6cd5\u6267\u884c....&quot;);\n        return true;\n    }\n\n    @Override\n    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {\n        System.out.println(&quot;MyInterceptor\u62e6\u622a\u5668\u7684postHandle\u65b9\u6cd5\u6267\u884c....&quot;);\n    }\n\n    @Override\n    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {\n        System.out.println(&quot;MyInterceptor\u62e6\u622a\u5668\u7684afterCompletion\u65b9\u6cd5\u6267\u884c....&quot;);\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e09\u3001SpringBoot3\u6574\u5408SpringMVC 1\u3001\u600e\u4e48\u5b9e\u73b0 \u6b65\u9aa4 \u5bfc\u5165\u4f9d\u8d56 \u914d\u7f6e\u542f\u52a8\u7c7b \u521b\u5efa\u5b9e\u4f53\u7c7b \u6d4b\u8bd5  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[],"class_list":["post-5838","post","type-post","status-publish","format-standard","hentry","category-springboot"],"_links":{"self":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/5838","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/comments?post=5838"}],"version-history":[{"count":1,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/5838\/revisions"}],"predecessor-version":[{"id":5839,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/posts\/5838\/revisions\/5839"}],"wp:attachment":[{"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/media?parent=5838"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/categories?post=5838"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/zyg.luchers.cn\/index.php\/wp-json\/wp\/v2\/tags?post=5838"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}