解析模板“欢迎”时出错,模板可能不存在或可能无法被任何已配置的模板解析器访问
Posted
技术标签:
【中文标题】解析模板“欢迎”时出错,模板可能不存在或可能无法被任何已配置的模板解析器访问【英文标题】:Error resolving template "welcome", template might not exist or might not be accessible by any of the configured Template Resolvers 【发布时间】:2017-11-05 18:17:14 【问题描述】:Project Structure
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-web-thymeleaf</artifactId>
<packaging>jar</packaging>
<name>Spring Boot Web Thymeleaf Example</name>
<description>Spring Boot Web JSP Example</description>
<url>https://www.mkyong.com</url>
<version>1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- hot swapping, disable cache for template, enable live reload -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- This is a web application -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Tomcat embedded container-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- JSTL for JSP -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Need this to compile JSP -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- Need this to compile JSP,
tomcat-embed-jasper version is not working, no idea why -->
<dependency>
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
<version>4.6.1</version>
<scope>provided</scope>
</dependency>
<!-- Optional, for bootstrap -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Package as an executable jar/war -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
SpringBootWebApplication.java
package com.mkyong;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootWebApplication
public static void main(String[] args) throws Exception
SpringApplication.run(SpringBootWebApplication.class, args);
WelcomeController.java
package com.mkyong;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class WelcomeController
// inject via application.properties
@Value("$welcome.message:test")
private String message = "Hello World";
@RequestMapping("/")
public String welcome(Map<String, Object> model)
model.put("message", this.message);
return "welcome";
output error image
welcome.jsp 页面未访问
如果将welcome.jsp 替换为welcome.html,则输出正常(在这种情况下没有问题)
但我不知道如何在这个 Spring Boot 应用程序中访问welcome.jsp 文件
html为什么在访问,jsp为什么不访问?
请给出解决方案
【问题讨论】:
请尝试使用 .html 而不是 .jsp 文件。而且您缺少以下依赖关系<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
【参考方案1】:
我认为是 Spring 版本:我将版本更改为 1.4.1,然后> mvn clean install 它对我有用。我使用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
希望对你有帮助
【讨论】:
以上是关于解析模板“欢迎”时出错,模板可能不存在或可能无法被任何已配置的模板解析器访问的主要内容,如果未能解决你的问题,请参考以下文章
解析模板 [] 时出错,模板可能不存在或可能无法被任何已配置的模板解析器访问
解析模板时出错 [/],模板可能不存在或可能无法被任何已配置的模板解析器访问
解析模板“登录”时出错,模板可能不存在或可能无法被任何已配置的模板解析器访问
解析模板 [registration] 时出错,模板可能不存在或可能无法被任何已配置的模板解析器访问