IDEA 创建 Java 项目 SpringMVC Thymeleaf 碰到的问题

发布时间:2026/6/11 1:46:08

IDEA 创建 Java 项目 SpringMVC Thymeleaf 碰到的问题 IDEA 创建 Java 项目 SpringMVC Thymeleaf 碰到的问题一、环境配置1.1、Maven 配置Maven 配置阿里云镜像等1.2、IDEA 配置 Maven1.3、IDEA 添加 Tomcat二、创建 Project?xml version1.0 encodingUTF-8?projectxmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.yangjunbo/groupIdartifactIdspringmvc-demo-01/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source17/maven.compiler.sourcemaven.compiler.target17/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/propertiesdependencies!-- SpringMVC --dependencygroupIdorg.springframework/groupIdartifactIdspring-webmvc/artifactIdversion5.3.1/version/dependency!-- 日志 --dependencygroupIdch.qos.logback/groupIdartifactIdlogback-classic/artifactIdversion1.2.3/version/dependency!-- Spring5和Thymeleaf整合包 --dependencygroupIdorg.thymeleaf/groupIdartifactIdthymeleaf-spring5/artifactIdversion3.0.12.RELEASE/version/dependency/dependencies/project?xml version1.0 encodingUTF-8?web-appxmlnshttps://jakarta.ee/xml/ns/jakartaeexmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttps://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsdversion5.0!-- 配置SpringMVC的前端控制器对浏览器发送的请求统一进行处理 --servletservlet-namespringMVC/servlet-nameservlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-class!-- 通过初始化参数指定SpringMVC配置文件的位置和名称 --init-param!-- contextConfigLocation为固定值 --param-namecontextConfigLocation/param-name!-- 使用classpath:表示从类路径查找配置文件例如maven工程中的src/main/resources --param-valueclasspath:springMVC.xml/param-value/init-param!-- 作为框架的核心组件在启动过程中有大量的初始化操作要做 而这些操作放在第一次请求时才执行会严重影响访问速度 因此需要通过此标签将启动控制DispatcherServlet的初始化时间提前到服务器启动时 --load-on-startup1/load-on-startup/servletservlet-mappingservlet-namespringMVC/servlet-name!-- 设置springMVC的核心控制器所能处理的请求的请求路径 /所匹配的请求可以是/login或.html或.js或.css方式的请求路径 但是/不能匹配.jsp请求路径的请求 --url-pattern//url-pattern/servlet-mapping/web-app?xml version1.0 encodingUTF-8?beansxmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://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!-- 自动扫描包 --context:component-scanbase-packageyang.junbo.mvc/!-- 配置Thymeleaf视图解析器 --beanidviewResolverclassorg.thymeleaf.spring5.view.ThymeleafViewResolverpropertynameordervalue1/propertynamecharacterEncodingvalueUTF-8/propertynametemplateEnginebeanclassorg.thymeleaf.spring5.SpringTemplateEnginepropertynametemplateResolverbeanclassorg.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver!-- 视图前缀 --propertynameprefixvalue/WEB-INF/templates//!-- 视图后缀 --propertynamesuffixvalue.html/propertynametemplateModevalueHTML5/propertynamecharacterEncodingvalueUTF-8//bean/property/bean/property/bean/beanspackageyang.junbo.mvc;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;/** * ClassName: HelloController * Package: yang.junbo.mvc * Description: * * Author 杨钧博 * Create 2026/6/10 19:39 * Version 1.0 */ControllerpublicclassHelloController{// localhost:8080/springMVC/RequestMapping(/)publicStringindex(){//设置视图名称returnindex;}}!DOCTYPEhtmlhtmllangenxmlns:thhttp://www.thymeleaf.orgheadmetacharsetUTF-8title首页/title/headbodyh1首页/h1ath:href{/hello}HelloWorld/abr//body/html三、部署启动四、排查问题现在有两个解决办法更换 Tomcat 9 以下版本升级 SpringMVC 依赖的版本升级 SpringMVC 依赖的版本更换 Tomcat 9 以下版本

相关新闻