【发布时间】: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