Ric*_*ick 1 spring-mvc thymeleaf spring-boot
我有一个应用程序,它是Spring Bookmark教程的简化版本.在其中,控制器使用注释,@RestController应用程序仅返回JSON.
我已经添加了通过Thymeleaf模板返回HTML的能力.我的模板正在退回,但它们似乎不是由Thymeleaf处理的.我正在使用spring-boot,而且我已经使用spring-boot-starter-thymeleaf了我的build.gradle 文件,但这似乎还不够.
例如,这是一个简单的root控制器:
package com.latencyzero.hoa;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/")
class
MainController
{
@RequestMapping(method = RequestMethod.GET)
ModelAndView
index()
{
ModelAndView mav = new ModelAndView("index");
mav.addObject("version", "0.1");
return mav;
}
}
Run Code Online (Sandbox Code Playgroud)
并且src/main/resources/templates/index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>HOA v${version}</title>
</head>
<body>
<h1>HOA v${version}</h1>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
呈现以下页面中的结果:
我发现的例子表明这是我需要做的全部,但它不起作用.我在某处需要一些额外的配置注释吗?
谢谢.
当我用 @Controller 替换它时,我遇到了与 @RestController 类似的问题,它按预期工作。
这些是基于Thymeleaf Tutorials的2个选项
内联表达式
- MainController.java
package com.latencyzero.hoa;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/")
class MainController
{
@RequestMapping(method = RequestMethod.GET)
ModelAndView
index()
{
ModelAndView mav = new ModelAndView("index");
mav.addObject("version", "0.1");
return mav;
}
}
Run Code Online (Sandbox Code Playgroud)
- index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8"/>
<title>HOA v[[${version}]]</title>
</head>
<body>
<h1>HOA v[[${version}]]</h1>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
自然模板
- MainController.java
package com.latencyzero.hoa;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/")
class MainController
{
@RequestMapping(method = RequestMethod.GET)
ModelAndView
index()
{
ModelAndView mav = new ModelAndView("index");
mav.addObject("hoa_version", "HOA v0.1");
return mav;
}
}
Run Code Online (Sandbox Code Playgroud)
- index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8"/>
<title th:text="${hoa_version}">Default Title</title>
</head>
<body>
<h1 th:text="${hoa_version}">Default Header</h1>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10499 次 |
| 最近记录: |