Java 为什么我的thymeleaf标签th:文本不起作用
在admin.html中,我有标记th:text=${name}。它不显示来自控制器的值。为什么? Admin.html 我的MVC配置是Java 为什么我的thymeleaf标签th:文本不起作用,java,spring,thymeleaf,Java,Spring,Thymeleaf,在admin.html中,我有标记th:text=${name}。它不显示来自控制器的值。为什么? Admin.html 我的MVC配置是 因为从/admin操作中,您必须传递名称,而不是登录名 @RequestMapping(value = "/admin", method = RequestMethod.GET) public String admin(Model model) { Authentication authentication =SecurityCont
因为从/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改进的代码格式当您使用redirect:,这是一个全新的调用。旧请求中没有保留任何模型属性。请使用@GetMapping进行简化。包括返回管理;最后
@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";
}
@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/");
}
}
@RequestMapping(value = "/admin", method = RequestMethod.GET)
public String admin(Model model) {
Authentication authentication =SecurityContextHolder.getContext().getAuthentication();
model.addAttribute("name",authentication.getName());
retrun "admin" ;
}