我想在不设置contextPath的情况下访问胸腺细胞中的org.springframework.ui.Model。我在application.properties文件中配置了上下文路径
发布于 2017-03-22 07:15:52
如果您使用的是Thymeleaf,您应该能够使用
<a th:href="@{/XXX}">Link Name</a> 假设这已经被宣布了。
我认为这确实需要使用SpringTemplateEngine。
发布于 2017-03-22 14:54:34
@{/}的url表达式为您提供完整的上下文..。例如,如果您有:
# application.properties
server.contextPath=/your/context然后你可以用
<div th:text="@{/}" />
or
<div th:text="${#request.contextPath}" />
both of which will output
<div>/your/context/</div>发布于 2017-03-22 08:34:42
您可以使用@ConfigurationProperties为类似于application.properties的属性使用一个新类从AppProperties.java获取上下文路径。
然后,您可以在设置ThymeLeaf参数的类中自动创建这个属性类,然后在Thymeleaf上下文中设置该参数,然后在html文件中相应地获取它。
@Configuration
@ConfigurationProperties(prefix = "com.test")
public class AppProperties{
private String contextPath;
}模板引擎中的设置
@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更改:
<div th:replace="${contextPath}"></div>https://stackoverflow.com/questions/42944994
复制相似问题