目前,我正在做一个小项目,其中我需要使用Spring Boot和FreeMarker模板引擎。我尝试过不同的方法,但仍然无法从Spring Boot返回FreeMarker视图。我的项目使用Gradle作为构建工具,下面是里面的内容:
build.gradle:
plugins {
id 'org.springframework.boot' version '2.3.3.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
sourceCompatibility = 11
repositories {
mavenCentral()
}
sourceSets.main.resources.srcDirs = ["src/resources"]
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
compile("org.springframework.boot:spring-boot-starter-web")
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'org.springframework.boot:spring-boot-starter-freemarker'
}下面是我的方法,我希望重定向到带有列表的FreeMarker视图,但是,当我向这个URL发送GET请求时,我只得到"index“字符串的显示:
@GetMapping(path = "/code/latest")
public String getTop10LatestCode(@ModelAttribute("model") ModelMap model) {
model.addAttribute("codes", codes.subList(Math.max(0, codes.size() - 10), Math.max(codes.size() - 1, 0)));
return "index";
}这是我的application.properties文件:
server.port=8889
management.endpoints.web.exposure.include=*
management.endpoint.shutdown.enabled=true
spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.suffix=.ftlh我的FreeMarker视图已经在resources文件夹内的templates文件夹下。
下面是我的项目结构:

任何帮助都将不胜感激,谢谢。
发布于 2020-12-19 11:08:48
我已经弄清楚了,我的类是用@RestController注释注释的,它本身是@Controller和@ResponseBody注释的组合,因为我的类中的每个方法都将返回一个响应体,即使是我在这里发布的那个,这就是为什么它返回主体,因此返回index。我需要将我的@RestController更改为@Controller,这样问题就解决了。
https://stackoverflow.com/questions/65359165
复制相似问题