thymeleaf 相关问题

Thymeleaf是一个XML / XHTML / HTML5模板引擎(可扩展到其他格式),可以在Web和非Web环境中工作。它更适合在Web应用程序的视图层提供XHTML / HTML5,它甚至可以在脱机环境中处理任何XML文件。它提供了一个可选模块,用于与Spring MVC集成,因此您可以在使用此技术的应用程序中将其用作JSP的完全替代,即使使用HTML5也是如此。

th:样式在我的项目中不起作用,具体取决于情况 true:false

我的文本必须使用 Thymeleaf 根据 true:false 情况改变颜色,但无论我做什么都不起作用。 我的文本必须使用 Thymeleaf 根据 true:false 情况改变颜色,但无论我做什么,它都不起作用。 <table th:each="book : ${assignedBooks}"> <tr th:style="${book.getExpired()? 'color: red;' : 'color: blue;'}" th:text="${book.getName() + ', ' + book.getAuthor() + ', ' + book.getYear()}">'someBook'</tr> </table> 我已经检查了 .getExpired == true,但我不明白为什么它仍然不起作用。 您的 <table> HTML 包含一个或多个 <tr> 行 - 但这些行不包含任何 <td> 单元格。将文本(来自 th:text="...")放在 <td> 内。 <table th:each="book : ${assignedBooks}"> <tr th:style="${book.getExpired()? 'color: red;' : 'color: blue;'}"> <td th:text="${book.getName() + ', ' + book.getAuthor() + ', ' + book.getYear()}">'someBook'></td> </tr> </table> 您可以通过查看 Thymeleaf 生成并显示在浏览器中的 HTML 自行解决此类问题。请记住:Thymeleaf 在服务器上生成 HTML,然后将该 HTML 发送到浏览器。因此,您可以准确地看到 Thymeleaf 生成的内容(除非它在服务器上引发错误/异常)。 通常您的浏览器中有一个“查看源代码”菜单项(取决于您使用的浏览器)。 在您的情况下,使用问题中的方法,您可能会看到类似以下内容: The Adventures of Tom Sawyer, Mark Twain, 1876 <table> <tbody> <tr style="color: red;"></tr> </tbody> </table> 了解缺少任何 <td> 单元格如何导致文本呈现在表格之外。每个浏览器如何尝试渲染不完整/无效的 HTML 取决于每个浏览器 - 因此您可能会看到类似但不同的内容。 有关如何创建有效/完整 HTML 表格的更多详细信息,请参阅 table。

回答 1 投票 0

如何在 ThymeLeaf(非 Web 环境)中为类型注册格式化程序

我有一个基于SpringBoot的应用程序(SpringBoot 3.2.3)。该应用程序不是 Web 应用程序,即它不使用 servlet、视图等。 在应用程序中,我想以 HTML 格式创建报告

回答 1 投票 0

带有 Thymeleaf 的样式标签

我有这段代码: h1 {颜色...</desc> <question vote="0"> <p>我有这段代码:</p> <pre><code>&lt;h2 class=&#34;title&#34; &gt; &lt;a th:href=&#34;${blogItem.getLink()}&#34; th:text=&#34;${blogItem.getTitle()}&#34;&gt;&lt;style&gt;h1 {color:&#39;${blogItem.getTitleColor()}&#39;;} Title&lt;/style&gt;&lt;/a&gt; &lt;/h2&gt; </code></pre> <p>但是我有这个错误:<pre><code>Mismatched property value (&lt;color&gt; | [initial | inherit | unset | revert])</code></pre></p> </question> <answer tick="false" vote="0"> <p>假设您希望链接样式为标题,您可以用 <pre><code>&lt;h1&gt;</code></pre> 包围链接并使用 Thymeleaf 的 <pre><code>th:style</code></pre> 来实现您想要的:</p> <pre><code>&lt;h1&gt;&lt;a th:href=&#34;${blogItem.getLink()}&#34; th:text=&#34;${blogItem.getTitle()}&#34; th:style=&#34;&#39;color: &#39; + ${blogItem.getTitleColor()} + &#39;;&#39;&#34;&gt;Title&lt;/a&gt;&lt;/h1&gt; </code></pre> </answer> </body></html>

回答 0 投票 0

Thymeleaf 模板无法处理并抛出 NoSuchMethodError

我的 Thymeleaf 模板中的 th:href 属性遇到问题。我正在使用 Thymeleaf 版本 3.1.2.RELEASE 以及 Thymeleaf Spring5 集成。尽管设置了 th:href

回答 1 投票 0

我无法使用 Thymeleaf 重定向到 Spring Boot 3 中的更新页面

我遇到的问题是模板解析期间发生错误(模板:“类路径资源[templates/update-customer.html]”) org.thymeleaf.exceptions.TemplateInputException:呃...

回答 1 投票 0

Thymeleaf Security(秒:授权标签)在 Spring 中不起作用

我有一个问题:当我实现 thymeleaf 安全性并添加依赖项时,我注意到 sec:authorize 标签甚至不起作用! 我浏览了很多问题,但所有问题都已过时或被人伪造......

回答 1 投票 0

在 Spring Boot 中重定向到“/create”时出现问题

我有这个 ProductController.java 代码片段: @GetMapping("/创建") 公共字符串showCreatePage(模型模型){ ProductDto ProductDto = new ProductDto(); model.addAttribute(&...

回答 1 投票 0

使用布局方言时 Thymeleaf 和 Micronaut 视图错误

我们正在使用 Thymeleaf 视图和布局方言运行 Micronaut(我们通过覆盖 Micronaut 的 ThymeleafFactory 手动添加它)。以下是依赖项(Micronaut 版本为 3.2.7):

回答 3 投票 0

如何使用thymeleaf搜索并带回数据

[Thymeleaf] 如何将搜索数据传输到控制器 我正在进行搜索并带回结果,但我使用 th:value,它不起作用以及如何使用两个条件进行搜索。 <...

回答 1 投票 0

如果用户具有特定角色,如何附加CSS类?

仅当用户具有特定角色时,如何使用 th:classappend 将类附加到元素? 像这样的东西: 我正在使用

回答 3 投票 0

更新 Thymeleaf 中的内容,无需重新加载整个页面

我想更新div“output”的内容而不重新加载整个页面。当用户输入内容并点击运行按钮时,服务器将在 div 上输出评估“

回答 2 投票 0

Spring Boot 中 Thymeleaf 的几个模板位置

目前,Spring Boot 允许使用 spring.thymeleaf.prefix 属性为 Thymeleaf 模板位置使用一个值。 默认值为类路径:/templates/。 我想要另一个地点...

回答 2 投票 0

如何在 Thymeleaf 中使用枚举作为方法参数?

我正在尝试调用一个方法,该方法将枚举作为我的 Thymeleaf 模板中的参数。枚举非常简单: 公共枚举语言{ C, 爪哇, 点网 } 正在调用的方法...

回答 1 投票 0

如何使用 Thymeleaf 在 HTML5 日期时间本地输入上显示 ZonedDateTime?

如何使用 Thymeleaf 在 HTML5 日期时间本地输入上显示 ZonedDateTime? ExampleModel.datetime 能够使用 ZonedDateTimeConverter 从 HTML 日期时间本地输入进行转换,但无法显示...

回答 1 投票 0

Spring Security 的 Thymeleaf 授权不起作用[重复]

我在 Spring Boot 应用程序中使用 Spring Security,但 Thymeleaf 授权似乎无法正常工作。 我有 Thymeleaf 模板,代码如下: 我在 Spring Boot 应用程序中使用 Spring Security,但 Thymeleaf 授权似乎无法正常工作。 我有带有以下代码的 Thymeleaf 模板: <div class="container"> <div class="row" sec:authorize="isAuthenticated()"> <h2 style="color:green">User is Logged In</h2> <p sec:authentication="principal.username">username</p> </div> <div class="row" sec:authorize="!isAuthenticated()"> <h2 style="color:red">User is Logged Out</h2> </div> <div class="row" sec:authorize="hasRole('ROLE_SUPERUSER')"> <h2>This will only be displayed if authenticated user has role ROLE_SUPERUSER.</h2> </div> <div class="row" sec:authorize="hasRole('ROLE_ADMIN')"> <h2>This will only be displayed if authenticated user has role ROLE_ADMIN.</h2> </div> <div class="row" sec:authorize="hasRole('ROLE_USER')"> <h2>This will only be displayed if authenticated user has role ROLE_USER.</h2> </div> <div th:if="${#authorization.expression('hasRole(''ROLE_ADMIN'')')}"> This will only be displayed if authenticated user has role ROLE_ADMIN. </div> <div th:if="${#authorization.expr('hasRole(''ROLE_ADMIN'')')}"> This will only be displayed if authenticated user has role ROLE_ADMIN. </div> </div> 示例取自:https://github.com/thymeleaf/thymeleaf-extras-springsecurity 但是,显示的唯一内容是 sec:authorize="isAuthenticated()" 和 sec:authorize="!isAuthenticated()",并且无论用户的角色如何,授权始终被忽略。 我的百里香配置是: @Configuration public class ThymeleafConfig { @Bean public TemplateResolver defaultTemplateResolver() { TemplateResolver resolver = new TemplateResolver(); resolver.setResourceResolver(thymeleafResourceResolver()); resolver.setPrefix("classpath:/templates/"); resolver.setSuffix(".html"); resolver.setTemplateMode("HTML5"); resolver.setCharacterEncoding("UTF-8"); resolver.setCacheable(true); return resolver; } @Bean public SpringResourceResourceResolver thymeleafResourceResolver() { return new SpringResourceResourceResolver(); } @Bean public SpringTemplateEngine templateEngine(TemplateResolver templateResolver) { SpringTemplateEngine engine = new SpringTemplateEngine(); engine.setTemplateResolver(templateResolver); engine.addDialect(new SpringSecurityDialect()); engine.addDialect(new LayoutDialect()); return engine; } @Bean public ThymeleafViewResolver thymeleafViewResolver(SpringTemplateEngine templateEngine) { ThymeleafViewResolver resolver = new ThymeleafViewResolver(); resolver.setTemplateEngine(templateEngine); resolver.setCharacterEncoding("UTF-8"); resolver.setContentType("text/html"); resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 5); return resolver; } } 我对 thymeleaf-extras-springsecurity4 使用以下依赖项: <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity4</artifactId> <version>2.1.3.RELEASE</version> </dependency> 版本 3.0.2.RELEASE 根本不起作用,并且 sec 命名空间总是被 Thymeleaf 忽略。 我的 Spring Boot 版本是 1.5.2.RELEASE。 可能是什么原因? 更新。 configure(HttpSecurity http)中的SecurityConfig方法如下所示: @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().ignoringAntMatchers("/h2-console").disable() .authorizeRequests() .antMatchers("/webjars/**", "/static/**", "/images/**", "/**/favicon.ico").permitAll() .antMatchers("/heat/**", "/power/**", "/water/**").permitAll() // start allowing h2-console .antMatchers("/h2-console/**").permitAll(); http.csrf().disable(); http.headers().frameOptions().disable() // end allowing h2-console .and().authorizeRequests().antMatchers("/info").permitAll() .and().authorizeRequests().antMatchers("/users/**").authenticated() .and().authorizeRequests().antMatchers("/users/**").hasAnyAuthority("ADMIN", "SUPERUSER") .and().formLogin() .loginPage("/login") .permitAll() .and() .logout() .permitAll() .deleteCookies("remove") .logoutUrl("/logout") .logoutSuccessUrl("/") .invalidateHttpSession(true) .and().exceptionHandling().accessDeniedPage("/access_denied"); } 并且来自 IndexController 的映射非常简单,它只是返回 login 模板: @RequestMapping("/login") public String loginForm() { return "login"; } 版本说明: 由于新的安全限制,以下内容在 Thymeleaf 3.1+ 中不起作用。 解决任务的另一种方法是使用此语法来检查角色: <div class="row" th:if="${#request.isUserInRole('SUPERUSER')}"> <h2>This will only be displayed if authenticated user has role ROLE_SUPERUSER.</h2> </div> 它不使用 sec 命名空间,实际上根本不需要 thymeleaf-extras-springsecurity4 依赖项来使用它。 经过多次尝试不同的配置,我找到了解决方法。在这种情况下,sec:authorize="hasAuthority('ADMIN')"属性起作用: <div class="row" sec:authorize="hasRole('ROLE_ADMIN')"> <div class="col-md-10 col-md-offset-2"> <h2>User Has Role Admin</h2> </div> </div> <div class="row" sec:authorize="hasAuthority('ADMIN')"> <div class="col-md-10 col-md-offset-2"> <h2>User Has Authority Admin</h2> </div> </div> 和 User Has Authority Admin 标题呈现在页面上。 仍然不知道为什么 sec:authorize="hasRole('ROLE_ADMIN')" 属性不起作用,因为 thymeleaf-extras-springsecurity GitHub 页面上建议将其作为示例: https://github.com/thymeleaf/thymeleaf-extras-springsecurity#using-the-attributes 希望这可以帮助某人,尽管问题仍然悬而未决。 要尝试的事情: 1) 用 @EnableWebMvc 注释您的配置。 2) 将 ROLE_ADMIN 替换为 ADMIN(其他也相应地)。 3)在您的控制器中,打印此内容以查看您当前的角色: Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Set<String> roles = authentication.getAuthorities().stream() .map(r -> r.getAuthority()).collect(Collectors.toSet()); System.out.println(roles); 如果这对您不起作用,也许可以尝试 getUserPrincipal() 中的 HttpServletRequest。 简短地说: 我包含了我的 MVC 配置,以便您可以尝试最新的 Thymeleaf 和 Spring Security 版本。那里有一些额外的配置,因此您可以删除与您的项目无关的内容。 @Configuration @EnableWebMvc public class WebMvcConfig extends WebMvcConfigurerAdapter { @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configuration) { configuration.enable(); } @Bean public ThymeleafViewResolver viewResolver() { ThymeleafViewResolver resolver = new ThymeleafViewResolver(); resolver.setOrder(1); resolver.setCharacterEncoding(StandardCharsets.UTF_8.name()); resolver.setTemplateEngine(templateEngine()); return resolver; } @Bean public TemplateEngine templateEngine() { Set<ITemplateResolver> templateResolvers = new LinkedHashSet<>(1); templateResolvers.add(webTemplateResolver()); SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.setTemplateResolvers(templateResolvers); Set<IDialect> dialects = new LinkedHashSet<>(2); dialects.add(new SpringSecurityDialect()); dialects.add(new Java8TimeDialect()); templateEngine.setAdditionalDialects(dialects); return templateEngine; } @Bean public ITemplateResolver webTemplateResolver() { SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver(); resolver.setPrefix("/WEB-INF/thymeleaf/"); resolver.setTemplateMode(TemplateMode.HTML); resolver.setCharacterEncoding(StandardCharsets.UTF_8.name()); resolver.setSuffix(".html"); resolver.setCacheable(false); resolver.setOrder(2); return resolver; } @Bean public ViewResolver tilesViewResolver() { UrlBasedViewResolver viewResolver = new UrlBasedViewResolver(); viewResolver.setViewClass(TilesView.class); viewResolver.setOrder(0); return viewResolver; } @Bean public TilesConfigurer tilesConfigurer() { TilesConfigurer configurer = new TilesConfigurer(); configurer.setDefinitions("/WEB-INF/**/views.xml"); return configurer; } @Bean public LocalValidatorFactoryBean validator() { return new LocalValidatorFactoryBean(); } @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/403"); registry.addViewController("/404"); registry.addViewController("/about"); //edited for brevity } @Bean public ReloadableResourceBundleMessageSource messageSource() { ReloadableResourceBundleMessageSource source = new ReloadableResourceBundleMessageSource(); source.setBasename("classpath:messages"); source.setDefaultEncoding(StandardCharsets.UTF_8.name()); return source; } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(localeChangeInterceptor()); registry.addInterceptor(themeChangeInterceptor()); registry.addInterceptor(deviceResolverHandlerInterceptor()); super.addInterceptors(registry); } @Bean public HandlerInterceptor localeChangeInterceptor() { LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor(); interceptor.setParamName("lang"); return interceptor; } @Bean public HandlerInterceptor themeChangeInterceptor() { ThemeChangeInterceptor interceptor = new ThemeChangeInterceptor(); interceptor.setParamName("theme"); return interceptor; } @Bean public ResourceBundleThemeSource themeSource() { ResourceBundleThemeSource themeSource = new ResourceBundleThemeSource(); themeSource.setBasenamePrefix("theme-"); return themeSource; } @Bean public PersistedThemeResolver themeResolver() { PersistedThemeResolver resolver = new PersistedThemeResolver(); resolver.setDefaultThemeName("default"); return resolver; } @Bean public HandlerInterceptor deviceResolverHandlerInterceptor() { return new DeviceResolverHandlerInterceptor(); } @Bean public CookieLocaleResolver localeResolver() { CookieLocaleResolver resolver = new CookieLocaleResolver(); resolver.setDefaultLocale(Locale.US); return resolver; } //removed custom bean declaration @Override public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { argumentResolvers.add(new ServletWebArgumentResolverAdapter(new DeviceWebArgumentResolver())); super.addArgumentResolvers(argumentResolvers); } @Bean public Executor taskExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setCorePoolSize(100); executor.initialize(); return executor; } } 工作 pom 摘录: <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <version>3.0.2.RELEASE</version> <exclusions> <exclusion> <groupId>javassist</groupId> <artifactId>javassist</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring4</artifactId> <version>3.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-springsecurity4</artifactId> <version>3.0.0.RELEASE</version> </dependency>

回答 3 投票 0

通过网络上传/下载文件夹

嘿,我目前正在学习一般开发, 我目前有一个带有 thymeleaf 的 springboot 项目,我尝试使用 AdminLTE 模板(简单表)以不同的方式显示上传的文件

回答 1 投票 0

无法导入js文件或用Thymeleaf显示图像

所以我正在使用 Thymeleaf 构建一个 Spring Boot 项目,文件结构如下: 源代码 |_main |_ java |_ 资源 |_ 静态 |_ 图像 ...

回答 1 投票 0

Spring Boot中的ThymeLeaf找不到模板位置:classpath:/templates/

我想配置一个简单的控制器: @配置 公共类 Config 实现 WebMvcConfigurer { @Autowired 私有最终ApplicationContext applicationContext; 公共配置(

回答 1 投票 0

在 thymeleaf 中设置 th:field 属性正在替换 name 属性

我有一个表格,我需要在其中显示许多问题和多个答案。我将 Spring Boot 与 Thymeleaf 结合使用,并循环显示问题标签和多个单选按钮的列表

回答 2 投票 0

无法发布包含多个(超过 256 个)值的表单

我正在使用 Spring Boot 1.2.2 和 Thymeleaf。 我的问题是我尝试在表单中发布一长串项目(一些标签,一个复选框),该表单无法执行我列表中的这么多项目。 (我测试过小...

回答 2 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.