【问题标题】:Need equivalent of JSP tag in Thymeleaf在 Thymeleaf 中需要等效的 JSP 标记
【发布时间】:2020-03-23 13:39:46
【问题描述】:

我是 Thymeleaf 的新手。在 jsp 世界中,我可以在 jsp 的“head”中使用以下标签更改 Springboot Web Application 的主题:

<spring:theme code="stylesheet" var="themeName" />
<link href='<spring:url value="css/${themeName}"/>' rel="stylesheet" />

如果我要使用“Thymeleaf 模板”而不是 jsp,我应该写什么“与上述完全相同”?我正在使用(并且必须使用)spring boot(带有网络和数据)。有人可以给我指出一条出路吗?

更新

添加更多信息。

我有我的config 和豆子,如下所示:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**")
            .addResourceLocations("/resources/static/css**", "/resources/static/**", "/static/")
            .setCacheControl(CacheControl.maxAge(2, TimeUnit.HOURS).cachePublic());
}

@Bean
public ThemeSource themeSource() {
    ResourceBundleThemeSource themeSource = new ResourceBundleThemeSource();
    themeSource.setBasenamePrefix("theme/");
    return themeSource;
}

@Bean
public ThemeResolver themeResolver() {
    CookieThemeResolver resolver = new CookieThemeResolver();
    resolver.setDefaultThemeName("cosmo");
    return resolver;
}

而且我在主题文件夹下还有一堆(例如 Cosmo.BootStrap.min.css)。当我从JSP 中选择主题名称时,我的整个 web 应用程序的主题都会更改。

简而言之,在我的“header.html 片段”中使用 Thymeleaf,我试图实现与此类似的东西 (${somename}) -

<link href="css/${somename}.css" rel="stylesheet" type="text/css" />

现在我一无所知。请提供一些指导。

【问题讨论】:

    标签: spring-boot spring-mvc spring-data-jpa thymeleaf


    【解决方案1】:

    您始终可以在 Thymeleaf 中构建动态 URL,例如:

    <head th:with="themeName=${'stylesheet'}">
      <link th:href="@{|/css/${themeName}|}" rel="stylesheet"/>
    </head>
    

    现在themeName 可以从ModelModelMap 类中的控制器填充,也可以在模板HTML 中计算。

    【讨论】:

    • 谢谢。我会试一试,然后会更新结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 2019-01-08
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 2012-07-11
    相关资源
    最近更新 更多