首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从Spring Boot方法返回Freemarker视图

无法从Spring Boot方法返回Freemarker视图
EN

Stack Overflow用户
提问于 2020-12-18 22:53:45
回答 1查看 106关注 0票数 0

目前,我正在做一个小项目,其中我需要使用Spring Boot和FreeMarker模板引擎。我尝试过不同的方法,但仍然无法从Spring Boot返回FreeMarker视图。我的项目使用Gradle作为构建工具,下面是里面的内容:

build.gradle:

代码语言:javascript
复制
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“字符串的显示:

代码语言:javascript
复制
@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文件:

代码语言:javascript
复制
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文件夹下。

下面是我的项目结构:

任何帮助都将不胜感激,谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-19 11:08:48

我已经弄清楚了,我的类是用@RestController注释注释的,它本身是@Controller@ResponseBody注释的组合,因为我的类中的每个方法都将返回一个响应体,即使是我在这里发布的那个,这就是为什么它返回主体,因此返回index。我需要将我的@RestController更改为@Controller,这样问题就解决了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65359165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档