【发布时间】:2015-12-11 16:32:04
【问题描述】:
FreeMarker 返回模板的以下内容:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="https://someOtherUrl">here</a>.</p>
</body></html>
这根本不可能发生。 FreeMaker 是如何获得它的? FreeMarker 配置已创建并使用:
Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);
...
@PostConstruct
void init()
{
configuration.setDefaultEncoding(StandardCharsets.UTF_8.toString());
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
configuration.setTemplateLoader(new TemplateLoader());
configuration.setLocale(Locale.ENGLISH);
}
...
Template temp = configuration.getTemplate(sourceTemplateUrl);
我可以摆脱这种“聪明”的行为吗?
编辑:
public class TemplateLoader extends URLTemplateLoader
{
private static final String LOCAL_PREFIX = "_" + Locale.ENGLISH;
@Override
protected URL getURL(final String url)
{
try
{
//get rid of "_en" in the url, FreeMarker set it based on Locale
return new URL(url.replace(LOCAL_PREFIX, ""));
}
catch (MalformedURLException e)
{
throw new RuntimeException(e);
}
}
}
【问题讨论】:
-
@AleksandrM,我已将代码发布到问题中
标签: java freemarker