Browse Source

0522 视图解析和全注解

Qing 1 year ago
parent
commit
ab5701ac3a
29 changed files with 618 additions and 69 deletions
  1. 1 0
      springmvc-demo/.idea/httpRequests/2024-05-18T134630.200.json
  2. 1 0
      springmvc-demo/.idea/httpRequests/2024-05-18T134717.200.json
  3. 1 0
      springmvc-demo/.idea/httpRequests/2024-05-18T134725.200.json
  4. 1 0
      springmvc-demo/.idea/httpRequests/2024-05-18T134733.200.json
  5. 1 0
      springmvc-demo/.idea/httpRequests/2024-05-18T140300.200.json
  6. 1 0
      springmvc-demo/.idea/httpRequests/2024-05-18T140351.200.json
  7. 1 0
      springmvc-demo/.idea/httpRequests/2024-05-18T142022.200.json
  8. 1 0
      springmvc-demo/.idea/httpRequests/2024-05-18T151110.200.json
  9. 1 0
      springmvc-demo/.idea/httpRequests/2024-05-18T163932.200.json
  10. 1 0
      springmvc-demo/.idea/httpRequests/2024-05-18T163955.200.json
  11. 10 0
      springmvc-demo/.idea/httpRequests/2024-05-18T165953.200.json
  12. 1 0
      springmvc-demo/.idea/httpRequests/http-client.cookies
  13. 116 0
      springmvc-demo/.idea/httpRequests/http-requests-log.http
  14. 47 11
      springmvc-demo/.idea/workspace.xml
  15. 12 0
      springmvc-demo/pom.xml
  16. 36 0
      springmvc-demo/src/main/java/com/sf/MyWebInitConfig.java
  17. 0 13
      springmvc-demo/src/main/java/com/sf/SpringMvcConfig.java
  18. 44 0
      springmvc-demo/src/main/java/com/sf/config/DruidConfig.java
  19. 67 0
      springmvc-demo/src/main/java/com/sf/config/MybatisConfig.java
  20. 11 0
      springmvc-demo/src/main/java/com/sf/config/SpringConfig.java
  21. 48 0
      springmvc-demo/src/main/java/com/sf/config/SpringMvcConfig.java
  22. 42 0
      springmvc-demo/src/main/java/com/sf/controller/HelloController.java
  23. 57 0
      springmvc-demo/src/main/java/com/sf/controller/RespController.java
  24. 25 0
      springmvc-demo/src/main/resources/spring-mvc.xml
  25. 12 0
      springmvc-demo/src/main/webapp/WEB-INF/templates/hello.html
  26. 14 0
      springmvc-demo/src/main/webapp/WEB-INF/templates/list.html
  27. 11 0
      springmvc-demo/src/main/webapp/WEB-INF/templates/test.html
  28. 10 0
      springmvc-demo/src/main/webapp/WEB-INF/templates/testScope.html
  29. 45 45
      springmvc-demo/src/main/webapp/WEB-INF/web.xml

+ 1 - 0
springmvc-demo/.idea/httpRequests/2024-05-18T134630.200.json

@@ -0,0 +1 @@
+hello GetMapping

+ 1 - 0
springmvc-demo/.idea/httpRequests/2024-05-18T134717.200.json

@@ -0,0 +1 @@
+hello PostMapping

+ 1 - 0
springmvc-demo/.idea/httpRequests/2024-05-18T134725.200.json

@@ -0,0 +1 @@
+hello PutMapping

+ 1 - 0
springmvc-demo/.idea/httpRequests/2024-05-18T134733.200.json

@@ -0,0 +1 @@
+hello DeleteMapping

+ 1 - 0
springmvc-demo/.idea/httpRequests/2024-05-18T140300.200.json

@@ -0,0 +1 @@
+hello PostMapping

+ 1 - 0
springmvc-demo/.idea/httpRequests/2024-05-18T140351.200.json

@@ -0,0 +1 @@
+hello PostMapping

+ 1 - 0
springmvc-demo/.idea/httpRequests/2024-05-18T142022.200.json

@@ -0,0 +1 @@
+hello header

+ 1 - 0
springmvc-demo/.idea/httpRequests/2024-05-18T151110.200.json

@@ -0,0 +1 @@
+header1:IntelliJ HTTP Client/IntelliJ IDEA 2024.1

+ 1 - 0
springmvc-demo/.idea/httpRequests/2024-05-18T163932.200.json

@@ -0,0 +1 @@
+header2:IntelliJ HTTP Client/IntelliJ IDEA 2024.1

+ 1 - 0
springmvc-demo/.idea/httpRequests/2024-05-18T163955.200.json

@@ -0,0 +1 @@
+header2:IntelliJ HTTP Client/IntelliJ IDEA 2024.1

+ 10 - 0
springmvc-demo/.idea/httpRequests/2024-05-18T165953.200.json

@@ -0,0 +1,10 @@
+{
+  "id": null,
+  "bookId": null,
+  "bookName": "Spring in Action",
+  "typeId": null,
+  "authorId": null,
+  "authorName": "Sth",
+  "bookImg": null,
+  "bookDesc": null
+}

+ 1 - 0
springmvc-demo/.idea/httpRequests/http-client.cookies

@@ -0,0 +1 @@
+# domain	path	name	value	date

+ 116 - 0
springmvc-demo/.idea/httpRequests/http-requests-log.http

@@ -0,0 +1,116 @@
+GET http://localhost:8080/springmvc_demo/book
+Accept: application/json
+User-Agent: IntelliJ HTTP Client/IntelliJ IDEA 2024.1
+Accept-Encoding: br, deflate, gzip, x-gzip
+content-length: 0
+
+<> 2024-05-18T165953.200.json
+
+###
+
+GET http://localhost:8080/springmvc_demo/header/header2
+Accept: application/json
+User-Agent: IntelliJ HTTP Client/IntelliJ IDEA 2024.1
+Accept-Encoding: br, deflate, gzip, x-gzip
+content-length: 0
+
+<> 2024-05-18T163955.200.json
+
+###
+
+GET http://localhost:8080/springmvc_demo/header/header2
+Accept: application/json
+User-Agent: IntelliJ HTTP Client/IntelliJ IDEA 2024.1
+Accept-Encoding: br, deflate, gzip, x-gzip
+content-length: 0
+
+<> 2024-05-18T163932.200.json
+
+###
+
+GET http://localhost:8080/springmvc_demo/header/header1
+Accept: application/json
+User-Agent: IntelliJ HTTP Client/IntelliJ IDEA 2024.1
+Accept-Encoding: br, deflate, gzip, x-gzip
+content-length: 0
+
+<> 2024-05-18T151110.200.json
+
+###
+
+GET http://localhost:8080/springmvc_demo/requestHeader
+Content-Type: text/plain
+Accept: application/json
+User-Agent: IntelliJ HTTP Client/IntelliJ IDEA 2024.1
+Accept-Encoding: br, deflate, gzip, x-gzip
+content-length: 0
+
+<> 2024-05-18T142022.200.json
+
+###
+
+POST http://localhost:8080/springmvc_demo/request
+Content-Type: application/json
+Accept: application/json
+User-Agent: IntelliJ HTTP Client/IntelliJ IDEA 2024.1
+Accept-Encoding: br, deflate, gzip, x-gzip
+content-length: 0
+
+<> 2024-05-18T140351.200.json
+
+###
+
+POST http://localhost:8080/springmvc_demo/request
+Content-Type: application/json
+Accept: application/json
+User-Agent: IntelliJ HTTP Client/IntelliJ IDEA 2024.1
+Accept-Encoding: br, deflate, gzip, x-gzip
+content-length: 0
+
+<> 2024-05-18T140300.200.json
+
+###
+
+DELETE http://localhost:8080/springmvc_demo/request
+Content-Type: application/json
+Accept: application/json
+User-Agent: IntelliJ HTTP Client/IntelliJ IDEA 2024.1
+Accept-Encoding: br, deflate, gzip, x-gzip
+content-length: 0
+
+<> 2024-05-18T134733.200.json
+
+###
+
+PUT http://localhost:8080/springmvc_demo/request
+Content-Type: application/json
+Accept: application/json
+User-Agent: IntelliJ HTTP Client/IntelliJ IDEA 2024.1
+Accept-Encoding: br, deflate, gzip, x-gzip
+content-length: 0
+
+<> 2024-05-18T134725.200.json
+
+###
+
+POST http://localhost:8080/springmvc_demo/request
+Content-Type: application/json
+Accept: application/json
+User-Agent: IntelliJ HTTP Client/IntelliJ IDEA 2024.1
+Accept-Encoding: br, deflate, gzip, x-gzip
+content-length: 0
+
+<> 2024-05-18T134717.200.json
+
+###
+
+GET http://localhost:8080/springmvc_demo/request
+Accept: application/json
+User-Agent: IntelliJ HTTP Client/IntelliJ IDEA 2024.1
+Accept-Encoding: br, deflate, gzip, x-gzip
+content-length: 0
+
+<> 2024-05-18T134630.200.json
+
+###
+

+ 47 - 11
springmvc-demo/.idea/workspace.xml

@@ -10,17 +10,39 @@
     <option name="autoReloadType" value="SELECTIVE" />
   </component>
   <component name="ChangeListManager">
-    <list default="true" id="8a346ac7-ee3f-4cad-99c9-d339051f5d36" name="Changes" comment="0518 整合mybatis">
+    <list default="true" id="8a346ac7-ee3f-4cad-99c9-d339051f5d36" name="Changes" comment="0518 请求和响应">
       <change afterPath="$PROJECT_DIR$/../.idea/VIP31.iml" afterDir="false" />
       <change afterPath="$PROJECT_DIR$/../JavaSE/JavaSe01/JavaSe01.iml" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/controller/HeaderController.java" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/controller/JsonController.java" afterDir="false" />
-      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/controller/ParamController.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/.idea/httpRequests/2024-05-18T134630.200.json" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/.idea/httpRequests/2024-05-18T134717.200.json" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/.idea/httpRequests/2024-05-18T134725.200.json" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/.idea/httpRequests/2024-05-18T134733.200.json" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/.idea/httpRequests/2024-05-18T140300.200.json" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/.idea/httpRequests/2024-05-18T140351.200.json" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/.idea/httpRequests/2024-05-18T142022.200.json" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/.idea/httpRequests/2024-05-18T151110.200.json" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/.idea/httpRequests/2024-05-18T163932.200.json" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/.idea/httpRequests/2024-05-18T163955.200.json" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/.idea/httpRequests/2024-05-18T165953.200.json" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/.idea/httpRequests/http-client.cookies" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/.idea/httpRequests/http-requests-log.http" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/MyWebInitConfig.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/config/DruidConfig.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/config/MybatisConfig.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/config/SpringConfig.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/config/SpringMvcConfig.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/controller/HelloController.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/java/com/sf/controller/RespController.java" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/webapp/WEB-INF/templates/hello.html" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/webapp/WEB-INF/templates/list.html" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/webapp/WEB-INF/templates/test.html" afterDir="false" />
+      <change afterPath="$PROJECT_DIR$/src/main/webapp/WEB-INF/templates/testScope.html" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/../spring-demo/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/../spring-demo/.idea/workspace.xml" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
-      <change beforePath="$PROJECT_DIR$/src/main/java/com/sf/controller/BookController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/sf/controller/BookController.java" afterDir="false" />
-      <change beforePath="$PROJECT_DIR$/src/main/java/com/sf/controller/RequestController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/sf/controller/RequestController.java" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/main/java/com/sf/SpringMvcConfig.java" beforeDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/main/resources/spring-mvc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/resources/spring-mvc.xml" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/main/webapp/WEB-INF/web.xml" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/webapp/WEB-INF/web.xml" afterDir="false" />
     </list>
     <option name="SHOW_DIALOG" value="false" />
     <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -37,6 +59,7 @@
       <list>
         <option value="Jsp File" />
         <option value="Spring Xml File" />
+        <option value="HTML File" />
         <option value="Class" />
       </list>
     </option>
@@ -81,6 +104,7 @@
   </component>
   <component name="PropertiesComponent"><![CDATA[{
   "keyToString": {
+    "DefaultHtmlFileTemplate": "HTML File",
     "HTTP Request.rest-api_10 | #1.executor": "Run",
     "HTTP Request.rest-api_11 | #1.executor": "Run",
     "HTTP Request.rest-api_8 | #1.executor": "Run",
@@ -94,7 +118,7 @@
     "Tomcat Server.Tomcat 11.0.0-M16.executor": "Run",
     "git-widget-placeholder": "master",
     "kotlin-language-version-configured": "true",
-    "last_opened_file_path": "/Users/Qing/IdeaProjects/SiFu/VIP31/springmvc-demo",
+    "last_opened_file_path": "/Users/Qing/IdeaProjects/SiFu/VIP31/springmvc-demo/src/main/java/com/sf/config",
     "node.js.detected.package.eslint": "true",
     "node.js.detected.package.tslint": "true",
     "node.js.selected.package.eslint": "(autodetect)",
@@ -109,6 +133,8 @@
 }]]></component>
   <component name="RecentsManager">
     <key name="CopyFile.RECENT_KEYS">
+      <recent name="$PROJECT_DIR$/src/main/java/com/sf/config" />
+      <recent name="$PROJECT_DIR$/src/main/webapp/WEB-INF/templates" />
       <recent name="$PROJECT_DIR$" />
     </key>
   </component>
@@ -129,7 +155,7 @@
       <module name="springmvc-demo" />
       <extension name="coverage">
         <pattern>
-          <option name="PATTERN" value="com.sf.*" />
+          <option name="PATTERN" value="com.sf.config.*" />
           <option name="ENABLED" value="true" />
         </pattern>
       </extension>
@@ -286,7 +312,8 @@
       <workItem from="1715994329537" duration="276000" />
       <workItem from="1715994638690" duration="21000" />
       <workItem from="1715994732370" duration="6964000" />
-      <workItem from="1716002556205" duration="14734000" />
+      <workItem from="1716002556205" duration="14772000" />
+      <workItem from="1716356852265" duration="8681000" />
     </task>
     <task id="LOCAL-00001" summary="0515 springmvc">
       <option name="closed" value="true" />
@@ -304,7 +331,15 @@
       <option name="project" value="LOCAL" />
       <updated>1716004905166</updated>
     </task>
-    <option name="localTasksCounter" value="3" />
+    <task id="LOCAL-00003" summary="0518 请求和响应">
+      <option name="closed" value="true" />
+      <created>1716022964439</created>
+      <option name="number" value="00003" />
+      <option name="presentableId" value="LOCAL-00003" />
+      <option name="project" value="LOCAL" />
+      <updated>1716022964439</updated>
+    </task>
+    <option name="localTasksCounter" value="4" />
     <servers />
   </component>
   <component name="TypeScriptGeneratedFilesManager">
@@ -313,6 +348,7 @@
   <component name="VcsManagerConfiguration">
     <MESSAGE value="0515 springmvc" />
     <MESSAGE value="0518 整合mybatis" />
-    <option name="LAST_COMMIT_MESSAGE" value="0518 整合mybatis" />
+    <MESSAGE value="0518 请求和响应" />
+    <option name="LAST_COMMIT_MESSAGE" value="0518 请求和响应" />
   </component>
 </project>

+ 12 - 0
springmvc-demo/pom.xml

@@ -112,6 +112,18 @@
             <version>2.17.0</version>
         </dependency>
 
+        <dependency>
+            <groupId>org.thymeleaf</groupId>
+            <artifactId>thymeleaf</artifactId>
+            <version>3.1.2.RELEASE</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.thymeleaf</groupId>
+            <artifactId>thymeleaf-spring6</artifactId>
+            <version>3.1.2.RELEASE</version>
+        </dependency>
+
     </dependencies>
     <build>
         <plugins>

+ 36 - 0
springmvc-demo/src/main/java/com/sf/MyWebInitConfig.java

@@ -0,0 +1,36 @@
+package com.sf;
+
+import com.sf.config.SpringConfig;
+import com.sf.config.SpringMvcConfig;
+import jakarta.servlet.Filter;
+import org.springframework.web.filter.CharacterEncodingFilter;
+import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
+
+public class MyWebInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
+
+    // Spring配置类
+    @Override
+    protected Class<?>[] getRootConfigClasses() {
+        return new Class[]{SpringConfig.class};
+    }
+
+    // SpringMVC配置类
+    @Override
+    protected Class<?>[] getServletConfigClasses() {
+        return new Class[]{SpringMvcConfig.class};
+    }
+
+    // 配置映射规则
+    @Override
+    protected String[] getServletMappings() {
+        return new String[]{"/"};
+    }
+
+    @Override
+    protected Filter[] getServletFilters() {
+        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
+        characterEncodingFilter.setEncoding("UTF-8");
+//        characterEncodingFilter.setForceEncoding(true);
+        return new Filter[]{characterEncodingFilter};
+    }
+}

+ 0 - 13
springmvc-demo/src/main/java/com/sf/SpringMvcConfig.java

@@ -1,13 +0,0 @@
-package com.sf;
-
-import org.springframework.context.annotation.ComponentScan;
-import org.springframework.context.annotation.Configuration;
-import org.springframework.web.servlet.config.annotation.EnableWebMvc;
-
-// @EnableWebMvc 意思是打开对mvc的使用开关
-// 扫描当前路径   com.sf  包含 com.sf.controller
-@ComponentScan
-@Configuration
-@EnableWebMvc
-public class SpringMvcConfig {
-}

+ 44 - 0
springmvc-demo/src/main/java/com/sf/config/DruidConfig.java

@@ -0,0 +1,44 @@
+package com.sf.config;
+
+
+import com.alibaba.druid.pool.DruidDataSource;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.PropertySource;
+import org.springframework.jdbc.core.JdbcTemplate;
+
+import javax.sql.DataSource;
+
+//  @PropertySource注解可以加载配置文件
+@PropertySource("classpath:db.properties")
+public class DruidConfig {
+
+    @Value("${jdbc.url}")
+    private String url;
+
+    @Value("${jdbc.username}")
+    private String username;
+
+    @Value("${jdbc.password}")
+    private String password;
+
+    @Value("${jdbc.driverName}")
+    private String driverClassName;
+
+    @Bean
+    public DataSource dataSource() {
+        DruidDataSource ds = new DruidDataSource();
+        ds.setUrl(url);
+        ds.setUsername(username);
+        ds.setPassword(password);
+        ds.setDriverClassName(driverClassName);
+        return ds;
+    }
+
+    // spring提供了一个通用的jdbc模版 JdbcTemplate
+    // 只需要把dataSource注入进来
+//    @Bean
+//    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
+//        return new JdbcTemplate(dataSource);
+//    }
+}

+ 67 - 0
springmvc-demo/src/main/java/com/sf/config/MybatisConfig.java

@@ -0,0 +1,67 @@
+package com.sf.config;
+
+
+import org.mybatis.spring.SqlSessionFactoryBean;
+import org.mybatis.spring.annotation.MapperScan;
+import org.mybatis.spring.mapper.MapperScannerConfigurer;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Import;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
+import org.springframework.core.io.support.ResourcePatternResolver;
+
+import javax.sql.DataSource;
+
+// 使用@MapperScan代替扫描dao
+//@Configuration
+//@ComponentScan("com.sf.service")
+//@MapperScan("com.sf.dao")
+//@Import(DruidConfig.class)
+public class MybatisConfig {
+
+    // Resource这里是一个资源类 不是注解 来自import org.springframework.core.io.Resource;
+    // @Value可以获取指定路径的资源
+    @Value("classpath:mybatis-config.xml")
+    private Resource mybatisConfig;
+
+    @Value("classpath:mapper/BookMapper.xml")
+    private Resource mapperConfig;
+
+//      <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
+//            <property name="dataSource" ref="datasource"></property>
+//            <property name="configLocation" value="classpath:mybatis-config.xml"></property>
+//        </bean>
+    @Bean
+    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
+        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
+        sqlSessionFactoryBean.setDataSource(dataSource);
+        sqlSessionFactoryBean.setConfigLocation(mybatisConfig);
+        sqlSessionFactoryBean.setMapperLocations(mapperConfig);
+        return sqlSessionFactoryBean;
+    }
+
+//    @Bean
+//    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) throws Exception {
+//        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
+//        sqlSessionFactoryBean.setDataSource(dataSource);
+//        sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
+//        //加载映射文件,文件目录为resources下面
+//        ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
+//        //读取以Mapper结尾的映射文件
+//        Resource[] mapperLocationResource = resourcePatternResolver.getResources("classpath*:mapper/**/*.xml");
+//        sqlSessionFactoryBean.setMapperLocations(mapperLocationResource);
+//        return sqlSessionFactoryBean;
+//    }
+
+    @Bean
+    public MapperScannerConfigurer mapperScannerConfigurer(){
+        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
+        mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
+        mapperScannerConfigurer.setBasePackage("com.sf.dao");
+        return mapperScannerConfigurer;
+    }
+}

+ 11 - 0
springmvc-demo/src/main/java/com/sf/config/SpringConfig.java

@@ -0,0 +1,11 @@
+package com.sf.config;
+
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Import;
+
+@Configuration
+@ComponentScan("com.sf.service")
+@Import({DruidConfig.class,MybatisConfig.class})
+public class SpringConfig {
+}

+ 48 - 0
springmvc-demo/src/main/java/com/sf/config/SpringMvcConfig.java

@@ -0,0 +1,48 @@
+package com.sf.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.ViewResolver;
+import org.springframework.web.servlet.config.annotation.EnableWebMvc;
+import org.thymeleaf.spring6.SpringTemplateEngine;
+import org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver;
+import org.thymeleaf.spring6.view.ThymeleafViewResolver;
+import org.thymeleaf.templatemode.TemplateMode;
+import org.thymeleaf.templateresolver.ITemplateResolver;
+
+// @EnableWebMvc 意思是打开对mvc的使用开关
+// 扫描当前路径   com.sf  包含 com.sf.controller
+@ComponentScan("com.sf.controller")
+@Configuration
+@EnableWebMvc
+public class SpringMvcConfig {
+
+    // 配置视图解析器
+    @Bean
+    public ITemplateResolver templateResolver() {
+        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
+        // 要注意 这里最后的斜杠 不可省略
+        templateResolver.setPrefix("/WEB-INF/templates/");
+        templateResolver.setSuffix(".html");
+        templateResolver.setTemplateMode(TemplateMode.HTML);
+        templateResolver.setCharacterEncoding("UTF-8");
+        return templateResolver;
+    }
+
+    @Bean
+    public SpringTemplateEngine templateEngine(ITemplateResolver templateResolver) {
+        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
+        templateEngine.setTemplateResolver(templateResolver);
+        return templateEngine;
+    }
+
+    @Bean
+    public ViewResolver viewResolver(SpringTemplateEngine templateEngine) {
+        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
+        viewResolver.setCharacterEncoding("UTF-8");
+        viewResolver.setTemplateEngine(templateEngine);
+        return viewResolver;
+    }
+
+}

+ 42 - 0
springmvc-demo/src/main/java/com/sf/controller/HelloController.java

@@ -0,0 +1,42 @@
+package com.sf.controller;
+
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+@Controller
+@RequestMapping("/thymeleaf")
+public class HelloController {
+
+    // http://localhost:8080/springmvc_demo/thymeleaf/hello
+    @GetMapping("/hello")
+    public String hello(Model model) {
+        // 通过前缀和后缀拼接后的页面名字
+        // /WEB-INF/templates/hello.html
+        model.addAttribute("name", "Hello Thymeleaf");
+        return "hello";
+    }
+
+    // http://localhost:8080/springmvc_demo/thymeleaf/testThymeleaf
+    // 转发和重定向
+    @GetMapping("/testThymeleaf")
+    public String testThymeleaf() {
+        // /WEB-INF/templates/test.html
+        return "test";
+    }
+
+    // http://localhost:8080/springmvc_demo/thymeleaf/testForward
+    @GetMapping("/testForward")
+    public String testForward() {
+        // /WEB-INF/templates/test.html
+        return "forward:/thymeleaf/testThymeleaf";
+    }
+
+    // http://localhost:8080/springmvc_demo/thymeleaf/testRedirect
+    @GetMapping("/testRedirect")
+    public String testRedirect() {
+        return "redirect:/thymeleaf/testThymeleaf";
+    }
+
+}

+ 57 - 0
springmvc-demo/src/main/java/com/sf/controller/RespController.java

@@ -0,0 +1,57 @@
+package com.sf.controller;
+
+import jakarta.servlet.http.HttpServletRequest;
+import org.springframework.stereotype.Controller;
+import org.springframework.ui.Model;
+import org.springframework.ui.ModelMap;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.servlet.ModelAndView;
+
+import java.util.Map;
+
+@Controller
+public class RespController {
+
+    // http://localhost:8080/springmvc_demo/list
+    @GetMapping("/list")
+    public String list() {
+        return "list";
+    }
+
+    @GetMapping("/testScope")
+    public String testScope(HttpServletRequest request) {
+        request.setAttribute("test", "Hello HttpServletRequest");
+        return "testScope";
+    }
+
+    @GetMapping("/testMap")
+    public String testMap(Map<String, Object> map) {
+        map.put("test", "Hello testMap");
+        return "testScope";
+    }
+
+    @GetMapping("/testModelAndView")
+    public ModelAndView testModelAndView() {
+        ModelAndView modelAndView = new ModelAndView();
+        modelAndView.addObject("test","Hello testModelAndView");
+        modelAndView.setViewName("testScope");
+        System.out.println(modelAndView.getClass().getName());
+        return modelAndView;
+    }
+
+    @GetMapping("/testModelMap")
+    public String testModelMap(ModelMap modelMap) {
+        modelMap.addAttribute("test", "Hello testModelMap");
+        System.out.println(modelMap.getClass().getName());
+        return "testScope";
+    }
+
+    @GetMapping("/testModel")
+    public String testModel(Model model) {
+        model.addAttribute("test", "Hello testModel");
+        System.out.println(model.getClass().getName());
+        return "testScope";
+    }
+
+
+}

+ 25 - 0
springmvc-demo/src/main/resources/spring-mvc.xml

@@ -13,4 +13,29 @@
     <context:component-scan base-package="com.sf.controller"/>
     <!-- 开启注解-->
     <mvc:annotation-driven></mvc:annotation-driven>
+
+    <!-- 模版解析器 -->
+    <bean id="templateResolver" class="org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver">
+        <!-- 前缀 -->
+        <property name="prefix" value="/WEB-INF/templates/" />
+        <!-- 后缀 -->
+        <property name="suffix" value=".html" />
+        <!-- 模版模式 -->
+        <property name="templateMode" value="HTML" />
+        <!-- 是否使用缓存 -->
+        <property name="cacheable" value="false" />
+        <property name="characterEncoding" value="UTF-8"/>
+    </bean>
+
+    <!-- 模版引擎 -->
+    <bean id="templateEngine" class="org.thymeleaf.spring6.SpringTemplateEngine">
+        <property name="templateResolver" ref="templateResolver" />
+    </bean>
+
+    <!--  视图解析器-->
+    <bean id="viewResolver" class="org.thymeleaf.spring6.view.ThymeleafViewResolver">
+        <property name="templateEngine" ref="templateEngine" />
+        <property name="characterEncoding" value="UTF-8"/>
+    </bean>
+
 </beans>

+ 12 - 0
springmvc-demo/src/main/webapp/WEB-INF/templates/hello.html

@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<!-- 命名空间 提供标签提示 -->
+<html lang="en" xmlns:th="http://www.thymeleaf.org">
+<head>
+    <meta charset="UTF-8">
+    <title>Title</title>
+</head>
+<body>
+<!-- thymeleaf更灵活 是动静分离的处理 -->
+<p th:text="${name}">name</p>
+</body>
+</html>

+ 14 - 0
springmvc-demo/src/main/webapp/WEB-INF/templates/list.html

@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html lang="en" xmlns:th="http://www.thymeleaf.org">
+<head>
+    <meta charset="UTF-8">
+    <title>Title</title>
+</head>
+<body>
+<a th:href="@{/testScope}"> testScope </a> <br>
+<a th:href="@{/testMap}"> testMap </a> <br>
+<a th:href="@{/testModelAndView}"> testModelAndView </a> <br>
+<a th:href="@{/testModelMap}"> testModelMap </a> <br>
+<a th:href="@{/testModel}"> testModel </a> <br>
+</body>
+</html>

+ 11 - 0
springmvc-demo/src/main/webapp/WEB-INF/templates/test.html

@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<!-- 命名空间 提供标签提示 -->
+<html lang="en" xmlns:th="http://www.thymeleaf.org">
+<head>
+    <meta charset="UTF-8">
+    <title>Title</title>
+</head>
+<body>
+<H1>TEST</H1>
+</body>
+</html>

+ 10 - 0
springmvc-demo/src/main/webapp/WEB-INF/templates/testScope.html

@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html lang="en" xmlns:th="http://www.thymeleaf.org">
+<head>
+    <meta charset="UTF-8">
+    <title>Title</title>
+</head>
+<body>
+<p th:text="${test}">test</p>
+</body>
+</html>

+ 45 - 45
springmvc-demo/src/main/webapp/WEB-INF/web.xml

@@ -2,52 +2,52 @@
         "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
         "http://java.sun.com/dtd/web-app_2_3.dtd" >
 
-<!--<web-app>-->
-<!--</web-app>-->
-<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
-         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
-                      https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
-         version="5.0">
+<web-app>
+</web-app>
+<!--<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"-->
+<!--         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"-->
+<!--         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee-->
+<!--                      https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"-->
+<!--         version="5.0">-->
 
-    <!-- 上下文参数和监听器 -->
-    <context-param>
-        <param-name>contextConfigLocation</param-name>
-        <param-value>classpath:spring.xml</param-value>
-    </context-param>
-    <listener>
-        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
-    </listener>
+<!--    &lt;!&ndash; 上下文参数和监听器 &ndash;&gt;-->
+<!--    <context-param>-->
+<!--        <param-name>contextConfigLocation</param-name>-->
+<!--        <param-value>classpath:spring.xml</param-value>-->
+<!--    </context-param>-->
+<!--    <listener>-->
+<!--        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>-->
+<!--    </listener>-->
 
-    <servlet>
-        <servlet-name>dispatcherServlet</servlet-name>
-        <!-- 配置来自springmvc的一个servlet -->
-        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
-        <init-param>
-            <param-name>contextConfigLocation</param-name>
-            <param-value>classpath:spring-mvc.xml</param-value>
-        </init-param>
-        <!-- tomcat启动时就创建对象-->
-        <load-on-startup>1</load-on-startup>
-    </servlet>
+<!--    <servlet>-->
+<!--        <servlet-name>dispatcherServlet</servlet-name>-->
+<!--        &lt;!&ndash; 配置来自springmvc的一个servlet &ndash;&gt;-->
+<!--        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>-->
+<!--        <init-param>-->
+<!--            <param-name>contextConfigLocation</param-name>-->
+<!--            <param-value>classpath:spring-mvc.xml</param-value>-->
+<!--        </init-param>-->
+<!--        &lt;!&ndash; tomcat启动时就创建对象&ndash;&gt;-->
+<!--        <load-on-startup>1</load-on-startup>-->
+<!--    </servlet>-->
 
-    <!-- 配置映射关系  /意思是拦截所有请求-->
-    <servlet-mapping>
-        <servlet-name>dispatcherServlet</servlet-name>
-        <url-pattern>/</url-pattern>
-    </servlet-mapping>
+<!--    &lt;!&ndash; 配置映射关系  /意思是拦截所有请求&ndash;&gt;-->
+<!--    <servlet-mapping>-->
+<!--        <servlet-name>dispatcherServlet</servlet-name>-->
+<!--        <url-pattern>/</url-pattern>-->
+<!--    </servlet-mapping>-->
 
-    <!-- 过滤器 -->
-    <filter>
-        <filter-name>characterEncodingFilter</filter-name>
-        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
-        <init-param>
-            <param-name>encoding</param-name>
-            <param-value>UTF-8</param-value>
-        </init-param>
-    </filter>
-    <filter-mapping>
-        <filter-name>characterEncodingFilter</filter-name>
-        <url-pattern>/*</url-pattern>
-    </filter-mapping>
-</web-app>
+<!--    &lt;!&ndash; 过滤器 &ndash;&gt;-->
+<!--    <filter>-->
+<!--        <filter-name>characterEncodingFilter</filter-name>-->
+<!--        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>-->
+<!--        <init-param>-->
+<!--            <param-name>encoding</param-name>-->
+<!--            <param-value>UTF-8</param-value>-->
+<!--        </init-param>-->
+<!--    </filter>-->
+<!--    <filter-mapping>-->
+<!--        <filter-name>characterEncodingFilter</filter-name>-->
+<!--        <url-pattern>/*</url-pattern>-->
+<!--    </filter-mapping>-->
+<!--</web-app>-->