the*_*ist 17 java spring-mvc thymeleaf
如何在模板上下文中添加"全局"变量(如用户名)?
目前,我将这些显式设置为我的TemplateController中的每个ModelAndView对象.
如果您只是想从你的东西application.properties变成你的thymeleaf模板,然后你可以使用Spring的的规划环境地政司。
${@environment.getProperty('name.of.the.property')}
Run Code Online (Sandbox Code Playgroud)
小智 6
@ControllerAdvice 为我工作:
@ControllerAdvice(annotations = RestController.class)
public class AnnotationAdvice {
@Autowired
UserServiceImpl userService;
@ModelAttribute("currentUser")
public User getCurrentUser() {
UserDetails userDetails = (UserDetails)
SecurityContextHolder.getContext()
.getAuthentication().getPrincipal();
return userService.findUserByEmail(userDetails.getUsername());
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6812 次 |
| 最近记录: |