【问题标题】:SpringBoot Returning a String rather than a rendered html pageSpring Boot 返回字符串而不是呈现的 html 页面
【发布时间】:2021-08-14 00:43:44
【问题描述】:

我使用 Eclipse 中的动态 Web 项目创建了一个 Spring Boot 应用程序,文件设置如下:

我的 spring boot 配置没有使用 maven 配置,因此没有 pom.xml 文件(我导入了所有需要的依赖项)。我的App文件包含spring boot的运行:

package Main;


import java.util.Collections;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App 
{

public static void main(String[] args) 
{
    SpringApplication app = new SpringApplication(App.class);
    app.setDefaultProperties(Collections
            .singletonMap("server.port", "9004"));
    app.run(args);
}

}

我也在尝试渲染一个名为 test.html 的 html 页面:

<html xmlns:th="http://www.thymeleaf.org"
xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Test</title>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body>
    <p>Number: <span th:text="${number}"></span></p>
    <p>Name: <span th:text="${firstName}"></span></p>
</body>

这样通过springboot渲染它:

package Main;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/")
public class MainController 
{
@GetMapping("testing")
@ResponseBody
public String testPage(Model model) 
{
    String name = "John";
    model.addAttribute("number", 42);
    model.addAttribute("firstName", name);
    return "test";



}
}

我遇到的问题是它返回的是字符串 test,而不是 test.html。我尝试改用@RestController(删除@ResponseBody),但没有成功。我很确定它与 test.html 的 URL 有关,但我不确定。任何帮助将不胜感激。

【问题讨论】:

    标签: java eclipse spring-mvc thymeleaf


    【解决方案1】:

    删除@ResponseBody,Spring 将尝试查找要返回的视图。

    删除@ResponseBody 但添加@RestController 失败的原因是@RestController 是结合@ResponseBody@Controller 的元注释,即这样做没有任何改变。

    您可以在ControllerResponseBody 文档中看到,响应正文实际上意味着返回值将被转换为 http 响应的正文,而不是查找控制器的经典视图。

    这是您的代码的一个工作示例,

    https://github.com/DarrenForsythe/so-67693611

    我还添加了一个测试,可以在不运行整个应用程序的情况下验证行为,

    package com.darrenforsythe.controllerview.so67693611;
    
    import org.junit.jupiter.api.Test;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
    import org.springframework.test.web.servlet.MockMvc;
    
    import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
    import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
    
    @WebMvcTest
    class MainControllerTests {
    
        private final MockMvc mockMvc;
    
        @Autowired
        MainControllerTests(MockMvc mockMvc) {
            this.mockMvc = mockMvc;
        }
    
        @Test
        void testViewShouldBeReturned() throws Exception {
            mockMvc.perform(get("/testing"))
                    .andExpect(status().isOk())
                    .andExpect(view().name("test"))
                    .andExpect(model().attribute("firstName", "John"))
                    .andExpect(model().attribute("number", 42));
    
        }
    }
    

    如果您克隆存储库并运行 build-and-run.sh,它应该创建 Spring Boot Jar,运行它,然后打开 URL http://localhost:9004/testing

    我猜你的 IDE 没有同步资源,一个干净的构建可能会解决它。

    【讨论】:

    • 如果我在这种情况下删除“@ResponseBody”,我会收到一个 404 错误,它无法找到 /testing。这可以使用“@RestController”解决,但它也会导致返回字符串“test”而不是 test.html
    • 是的,在这种情况下它找不到test 视图。如前所述,ResponseBody 用于将返回值转换为响应的主体,例如当你想返回一个 JSON 对象时。我会确保你的资源文件夹在构建时被打包
    • 是的,但这是我的全部问题。我试图弄清楚为什么 spring boot 找不到 test.html。我什至尝试过其他方法,例如 return new ModelAndView("test.html");这也是不成功的。
    • @JmanxC 我已经用一个工作示例和存储库更新了我的答案。
    猜你喜欢
    • 2019-05-10
    • 2014-02-22
    • 2020-01-13
    • 2020-01-09
    • 2014-06-01
    • 2018-07-27
    • 2018-05-07
    • 1970-01-01
    • 2015-10-03
    相关资源
    最近更新 更多