我不明白,如果 html 模板与我在 ModelAndView 中获得的属性链接,如何将 @Controller 更改为 @RestController 以获取 RESTFull 服务
@Controller
public class MyController{
@GetMapping("/index")
public ModelAndView index(){
return new ModelAndView("/index", name, userService.getUser().getName());
}
}
Run Code Online (Sandbox Code Playgroud)
在百里香模板中它看起来像
<p th:text="'Hello, ' + ${name} + '!'" />
Run Code Online (Sandbox Code Playgroud)
但我想转到索引页面,并在后台获取用户名
@RestController
public class MyController{
@GetMapping("/api/user")
public String index(){
return userService.getUser().getName();
}
}
Run Code Online (Sandbox Code Playgroud)
我可以使用ajax来更新标签“p”,但这样使用百里香叶没有任何好处,我可以使用jsp。那么休息时使用百里香的最佳方法是什么,它是否合理?