Selaa lähdekoodia

# config:springMVC配置

yang yi 1 viikko sitten
vanhempi
sitoutus
d8ec10cce3
1 muutettua tiedostoa jossa 56 lisäystä ja 0 poistoa
  1. 56 0
      src/main/resources/spring-mvc.xml

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

@@ -0,0 +1,56 @@
+<?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>