首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Thymeleaf中访问contextPath?

如何在Thymeleaf中访问contextPath?
EN

Stack Overflow用户
提问于 2017-03-22 07:12:19
回答 4查看 17.3K关注 0票数 4

我想在不设置contextPath的情况下访问胸腺细胞中的org.springframework.ui.Model。我在application.properties文件中配置了上下文路径

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-03-22 07:15:52

如果您使用的是Thymeleaf,您应该能够使用

代码语言:javascript
复制
<a th:href="@{/XXX}">Link Name</a> 

假设这已经被宣布了。

我认为这确实需要使用SpringTemplateEngine。

票数 12
EN

Stack Overflow用户

发布于 2017-03-22 14:54:34

@{/}的url表达式为您提供完整的上下文..。例如,如果您有:

代码语言:javascript
复制
# application.properties
server.contextPath=/your/context

然后你可以用

代码语言:javascript
复制
<div th:text="@{/}" />
or
<div th:text="${#request.contextPath}" />
both of which will output
<div>/your/context/</div>
票数 8
EN

Stack Overflow用户

发布于 2017-03-22 08:34:42

您可以使用@ConfigurationProperties为类似于application.properties的属性使用一个新类从AppProperties.java获取上下文路径。

然后,您可以在设置ThymeLeaf参数的类中自动创建这个属性类,然后在Thymeleaf上下文中设置该参数,然后在html文件中相应地获取它。

代码语言:javascript
复制
@Configuration
@ConfigurationProperties(prefix = "com.test")
public class AppProperties{

private String contextPath;
}

模板引擎中的设置

代码语言:javascript
复制
@Service
public class MailContentBuilder {

private TemplateEngine templateEngine;

@Autowired
public MailContentBuilder(TemplateEngine templateEngine) {
    this.templateEngine = templateEngine;
}

public String build(CodeRequest codeRequest, String lang) {
    Context context = new Context();
    context.setVariable("footerFile", "footer_" + lang);
    context.setVariable("customerInfo", codeRequest);
    context.setVariable("imageLogo", "cid:" + Constants.LOGO);
    context.setVariable("imageBorder", "cid:" + Constants.LEFT_BORDER);
    context.setVariable("imageFooter", "cid:" + Constants.LOGO_FOOTER);
    return templateEngine.process("template", context);
}
}

HTML更改:

代码语言:javascript
复制
<div th:replace="${contextPath}"></div>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42944994

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档