Эх сурвалжийг харах

# feat:使用SpringMvcConfig配置类代替spring-mvc.xml配置文件

yang yi 2 долоо хоног өмнө
parent
commit
2e3ef70cf5

+ 70 - 0
src/main/java/space/anyi/config/SpringMvcConfig.java

@@ -0,0 +1,70 @@
+package space.anyi.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.config.annotation.EnableWebMvc;
+import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
+import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+import org.thymeleaf.spring5.SpringTemplateEngine;
+import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
+import org.thymeleaf.spring5.view.ThymeleafViewResolver;
+import org.thymeleaf.templatemode.TemplateMode;
+
+/**
+ * @fileName: SpringMvcConfig
+ * @projectName: SSM_template
+ * @package: space.anyi.config
+ * @author: 杨逸
+ * @date:2026/4/24 14:02
+ * @description: SpringMvc配置类替代spring-mvc.xml
+ */
+@Configuration
+@ComponentScan(basePackages = {"space.anyi.controller"})  // 扫描Controller
+@EnableWebMvc  // 启用Spring MVC注解
+public class SpringMvcConfig implements WebMvcConfigurer {
+    // 配置视图解析器
+    @Bean
+    public ThymeleafViewResolver thymeleafViewResolver(SpringTemplateEngine templateEngine){
+        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
+        viewResolver.setTemplateEngine(templateEngine);
+        viewResolver.setCharacterEncoding("UTF-8");
+        viewResolver.setCache(false);
+        return viewResolver;
+    }
+    //模板引擎
+    @Bean
+    public SpringTemplateEngine templateEngine(SpringResourceTemplateResolver templateResolver) {
+        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
+        templateEngine.setTemplateResolver(templateResolver);
+        templateEngine.setEnableSpringELCompiler(true);
+        return templateEngine;
+    }
+    //模板解析器
+    @Bean
+    public SpringResourceTemplateResolver templateResolver() {
+        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
+        templateResolver.setPrefix("classpath:/templates/");
+        templateResolver.setSuffix(".html");
+        templateResolver.setTemplateMode(TemplateMode.HTML5);
+        templateResolver.setCharacterEncoding("UTF-8");
+        templateResolver.setCacheable(false);
+        return templateResolver;
+    }
+
+    // 配置静态资源处理
+    @Override
+    public void addResourceHandlers(ResourceHandlerRegistry registry) {
+        registry.addResourceHandler("/static/**")
+                .addResourceLocations("/static/");
+    }
+
+    // 配置拦截器(可选)
+    @Override
+    public void addInterceptors(InterceptorRegistry registry) {
+        //registry.addInterceptor(new LoginInterceptor())
+        //        .addPathPatterns("/**")
+        //        .excludePathPatterns("/login", "/register");
+    }
+}

+ 0 - 56
src/main/resources/spring-mvc.xml

@@ -1,56 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<beans xmlns="http://www.springframework.org/schema/beans"
-       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-       xmlns:context="http://www.springframework.org/schema/context"
-       xmlns:mvc="http://www.springframework.org/schema/mvc"
-       xsi:schemaLocation="http://www.springframework.org/schema/beans
-       http://www.springframework.org/schema/beans/spring-beans.xsd
-       http://www.springframework.org/schema/context
-       https://www.springframework.org/schema/context/spring-context.xsd
-       http://www.springframework.org/schema/mvc
-       https://www.springframework.org/schema/mvc/spring-mvc.xsd">
-
-    <!-- 1. 扫描 Controller 包 -->
-    <context:component-scan base-package="space.anyi.controller"/>
-
-    <!-- 2. 配置 MVC 注解驱动 -->
-    <mvc:annotation-driven/>
-
-    <!-- 3. 配置静态资源处理(CSS, JS, 图片等) -->
-    <mvc:default-servlet-handler/>
-
-    <!-- ========== 4. 配置 Thymeleaf 视图解析器(核心部分) ========== -->
-
-    <!-- 4.1 配置模板解析器:设置模板文件的位置和后缀 -->
-    <bean id="templateResolver"
-          class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
-        <!-- 模板文件存放的根路径,通常在 src/main/webapp/WEB-INF/templates/ 或 classpath 下 -->
-        <property name="prefix" value="classpath:templates/"/>
-        <!-- 模板文件后缀 -->
-        <property name="suffix" value=".html"/>
-        <!-- 模板类型 -->
-        <property name="templateMode" value="HTML"/>
-        <!-- 编码格式 -->
-        <property name="characterEncoding" value="UTF-8"/>
-        <!-- 开发环境下建议关闭缓存,以便实时看到修改效果 -->
-        <property name="cacheable" value="false"/>
-    </bean>
-
-    <!-- 4.2 配置模板引擎 -->
-    <bean id="templateEngine"
-          class="org.thymeleaf.spring5.SpringTemplateEngine">
-        <property name="templateResolver" ref="templateResolver"/>
-        <!-- 可选:启用 Spring EL 表达式 -->
-        <property name="enableSpringELCompiler" value="true"/>
-    </bean>
-
-    <!-- 4.3 配置视图解析器:将 Controller 逻辑视图名渲染为 Thymeleaf 模板 -->
-    <bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
-        <property name="templateEngine" ref="templateEngine"/>
-        <!-- 编码格式 -->
-        <property name="characterEncoding" value="UTF-8"/>
-        <!-- 开发环境下关闭缓存 -->
-        <property name="cache" value="false"/>
-    </bean>
-
-</beans>