tomcat 我不能使用freemarker当我正在建立我的Spring Boot 的web项目

vojdkbi0  于 2022-11-13  发布在  Spring
关注(0)|答案(3)|浏览(142)

我的请求可以进入Welcomecontroller的方法,但它表示此方法无法向我返回freemarker页面,或者Sping Boot 无法区分freemarker(我已将.ftl文件放入/resources/templates/)

当我输入网址http://localhost:8080/index

  • 我从我的chrome得到了这个,并且没有在我的IDEA的控制台中报告任何错误:*

Whitelabel Error Page此应用程序没有/error的显式Map,因此您将其视为一个回退。
2020年1月4日星期六22:52:53 CST出现意外错误(类型=未找到,状态=404)。无可用消息

  • 我的代码如下:*

第一个

  • 欢迎光临。FTL:*
<!DOCTYPE html>
<html lang="en">
<body>
    Date: ${time?date}
    <br>
    Time: ${time?time}
    <br>
    Message: ${message}
</body>
</html>
  • 应用程序.属性:*
application.message: Hello, Andy
  • 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.UU</groupId>
    <artifactId>questionsite</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>questionsite</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <commons-lang3-version>3.1</commons-lang3-version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
ghhkc1vu

ghhkc1vu1#

从Sping Boot 2.2开始,Freemarker模板的后缀是ftlh,而不是ftl
发行说明中对此进行了说明。
将您的welcome.ftl重命名为welcome.ftlh,它将使用默认配置。

jq6vz3qz

jq6vz3qz2#

将此添加到应用程序.yml

server:
  servlet:
    context-path: /cx

然后尝试http://localhost:8080/cx/index
请确保将.ftl文件放入/resources/templates/
P.S.您不需要此依赖项,因为它已经包含在spring-boot-starter-web中:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
hsgswve4

hsgswve43#

适用于Spring Boot2.6.0

控制器

WelcomeController.java

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

...

@Controller
public class WelcomeController {

    @GetMapping("/index")
    public String welcome(Model model) {

        Map<String, Object> mydataMap = new HashMap<>();

        LocalDate localDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd LLLL yyyy");
        String formattedDateString = localDate.format(formatter);

        mydataMap.put("Message", "hello");
        mydataMap.put("Date", formattedDateString);

        model.addAttribute("mydata", mydataMap); // the attrbuteName should same with the corresponding variable in the template file

        return "welcome"; // same with the template file name
    }
}

welcome.ftl

<!DOCTYPE html>
<html lang="en">
<body>
    <#list mydata as mykey, myvalue>
         <br> ${mykey}: ${myvalue}
    </#list>
</body>
</html>

application.properties

# 
# Default value true for springboot 2.6.0
spring.freemarker.enabled=true
# Default value /templates
spring.freemarker.template-loader-path=classpath:/templates
# Default value ftlh
spring.freemarker.suffix=.ftl

pom.xml

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

相关问题