gpt4 book ai didi

java - 为什么我的 thymeleaf 标签是:text don't work

转载 作者:行者123 更新时间:2023-12-02 11:41:30 25 4
gpt4 key购买 nike

在 admin.html 中,我有标签 th:text="${name}"。它不显示来自 Controller 的值。为什么?

Admin.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Admin page</title>
</head>
<body>
Dear <strong><p th:text="${name}"></p></strong>, Welcome to Admin Page.
<a href="/logout">Logout</a>
</body>
</html>

Controller

@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@ModelAttribute("userForm") User userForm, Model model) {

try {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

model.addAttribute("name",authentication.getName());

if (securityService.isAdmin(userForm.getUsername())) {
return "redirect:/admin";
}
} catch (RestException e) {
model.addAttribute("error", "User with username '"+userForm.getUsername()+"' not found");
}

return "redirect:/index";
}

我的MVC配置

@Configuration
@EnableWebMvc
@ComponentScan({ "ru.dev.avtonomki" })
public class WebMVCConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {

private ApplicationContext applicationContext;

public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}

@Bean
public ViewResolver htmlViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine(jspTemplateResolver()));
resolver.setContentType("text/html");
resolver.setCharacterEncoding("UTF-8");
resolver.setOrder(1);
resolver.setViewNames(ArrayUtil.array("*.html"));
return resolver;
}

private TemplateEngine templateEngine(ITemplateResolver templateResolver) {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.addDialect(new LayoutDialect(new GroupingStrategy()));
engine.addDialect(new Java8TimeDialect());
engine.setTemplateResolver(templateResolver);
return engine;
}

private ITemplateResolver htmlTemplateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(applicationContext);
resolver.setPrefix("/WEB-INF/templates/");
resolver.setCacheable(false);
resolver.setTemplateMode(TemplateMode.HTML5);
return resolver;
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}
}

最佳答案

因为在您的“/admin”操作中,您必须传递名称而不是登录名。

    @RequestMapping(value = "/admin", method = RequestMethod.GET)
public String admin(Model model) {
Authentication authentication =SecurityContextHolder.getContext().getAuthentication();

model.addAttribute("name",authentication.getName());

retrun "admin" ;

}

或者你可以设置RedirectAttributes

关于java - 为什么我的 thymeleaf 标签是:text don't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48517572/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com