我在构建 spring boot 的 web 项目时无法使用 freemarker
I can not use freemarker when i am building my spring boot's web project
我的请求可以进入我的 Welcomecontroller 的方法,但是似乎这个方法不能 return 给我一个 freemarker 页面或者 spring 引导没有区分freemarker(我已经把.ftl文件放到了/resources/templates/)
当我输入 url http://localhost:8080/index
我从我的 chrome 得到这个并且没有在我的 IDEA 控制台中报告任何错误:
白标错误页面
此应用程序没有 /error 的显式映射,因此您将其视为后备。
1 月 4 日星期六 22:52:53 CST 2020
出现意外错误(类型=未找到,状态=404)。
没有留言
我的代码如下:
@Controller
public class WelcomeController {
@Value("${application.message}")
private String message;
@GetMapping("/index")
public String welcome(Map<String, Object> model) {
model.put("time", new Date());
model.put("message", this.message);
return "welcome";
}
}
@SpringBootApplication
public class QuestionsiteApplication {
public static void main(String[] args) {
SpringApplication.run(QuestionsiteApplication.class, args);
}
}
welcome.ftl:
<!DOCTYPE html>
<html lang="en">
<body>
Date: ${time?date}
<br>
Time: ${time?time}
<br>
Message: ${message}
</body>
</html>
application.properties:
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>
将此添加到 application.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>
从 Spring Boot 2.2 开始,Freemarker 模板的后缀是 ftlh
而不是 ftl
。
这记录在 Release Notes。
将您的 welcome.ftl
重命名为 welcome.ftlh
,它将使用默认配置。
我的请求可以进入我的 Welcomecontroller 的方法,但是似乎这个方法不能 return 给我一个 freemarker 页面或者 spring 引导没有区分freemarker(我已经把.ftl文件放到了/resources/templates/)
当我输入 url http://localhost:8080/index
我从我的 chrome 得到这个并且没有在我的 IDEA 控制台中报告任何错误:
白标错误页面 此应用程序没有 /error 的显式映射,因此您将其视为后备。
1 月 4 日星期六 22:52:53 CST 2020 出现意外错误(类型=未找到,状态=404)。 没有留言
我的代码如下:
@Controller
public class WelcomeController {
@Value("${application.message}")
private String message;
@GetMapping("/index")
public String welcome(Map<String, Object> model) {
model.put("time", new Date());
model.put("message", this.message);
return "welcome";
}
}
@SpringBootApplication
public class QuestionsiteApplication {
public static void main(String[] args) {
SpringApplication.run(QuestionsiteApplication.class, args);
}
}
welcome.ftl:
<!DOCTYPE html>
<html lang="en">
<body>
Date: ${time?date}
<br>
Time: ${time?time}
<br>
Message: ${message}
</body>
</html>
application.properties:
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>
将此添加到 application.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>
从 Spring Boot 2.2 开始,Freemarker 模板的后缀是 ftlh
而不是 ftl
。
这记录在 Release Notes。
将您的 welcome.ftl
重命名为 welcome.ftlh
,它将使用默认配置。