【发布时间】:2015-10-29 01:52:42
【问题描述】:
我正在编写我的第一个 springboot 网络应用程序,项目结构如下:
---src/main/java
+com.example.myproject
+--Application.java
+com.example.myproject.domain
+--Person.java
+com.example.myproject.web
+--GreetingController.java
---src/main/resources
+static
+--css
+--js
+templates
+--greeting.html
应用程序.java
package com.example.myproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
app.setShowBanner(false);
app.run(args);
}
}
GreetingController.java
package com.example.myproject.web;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
@RequestMapping("/greeting")
public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
model.addAttribute("name", name);
return "greeting";
}
}
greeting.html
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>
问题是当我运行项目时,在网络浏览器上输入下面的 URL
http://localhost:8080/greeting
结果只显示此文本:greeting,而它应该显示此文本:Hello, World!
我尝试将 greeting.html 从模板文件夹中移出,但仍然不走运。据我了解,springboot 应该会自动扫描组件并正确加载资源文件。
请帮助就这个问题提出建议。
【问题讨论】:
标签: java templates spring-boot thymeleaf