Java 如何将自定义属性添加到标准thymeleaf方言';th';
在我的SpringMVC应用程序中,我试图创建一个自定义的thymeleaf方言来将ASCII字符串转换为文本。我能够创建前缀不是Java 如何将自定义属性添加到标准thymeleaf方言';th';,java,spring-mvc,thymeleaf,Java,Spring Mvc,Thymeleaf,在我的SpringMVC应用程序中,我试图创建一个自定义的thymeleaf方言来将ASCII字符串转换为文本。我能够创建前缀不是'th'的方言。但是如果我尝试使用'th'作为前缀,那么服务器将抛出以下运行时异常 org.thymeleaf.exceptions.ConfigurationException: When using SpringTemplateEngine, at least one of the configured dialects must be or extend org
'th'
的方言。但是如果我尝试使用'th'
作为前缀,那么服务器将抛出以下运行时异常
org.thymeleaf.exceptions.ConfigurationException: When using SpringTemplateEngine, at least one of the configured dialects must be or extend org.thymeleaf.spring4.dialect.SpringStandardDialect.
org.thymeleaf.spring4.SpringTemplateEngine.initializeSpecific(SpringTemplateEngine.java:147)
org.thymeleaf.TemplateEngine.initialize(TemplateEngine.java:831)
org.thymeleaf.spring4.view.ThymeleafView.renderFragment(ThymeleafView.java:203)
org.thymeleaf.spring4.view.ThymeleafView.render(ThymeleafView.java:190)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1228)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1011)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
基本上,我需要在这里实现的是创建一个像这样的自定义方言th:asciitext
。任何帮助都将不胜感激
注意:如果有人需要查看我已经尝试过的代码,请在评论部分询问。请参考此URL:
我认为您的场景与场景1类似。我能找到的唯一方法是扩展
SpringTemplateEngine
(它是为扩展而设计的)。然后,用新的模板引擎替换默认的SpringTemplateEngine
。查看下面的代码:
MySpringTemplateEngine
public class MySpringTemplateEngine extends SpringTemplateEngine {
private Set<IProcessor> additionalProcessors;
public Set<IProcessor> getAdditionalProcessors() {
return additionalProcessors;
}
public void setAdditionalProcessors(Set<IProcessor> additionalProcessors) {
this.additionalProcessors = additionalProcessors;
}
@Override
public void afterPropertiesSet() throws Exception {
super.afterPropertiesSet();
Map<String, IDialect> dialectsByPrefix = this.getDialectsByPrefix();
StandardDialect springDialect = (StandardDialect) dialectsByPrefix.get("th");
springDialect.setAdditionalProcessors(additionalProcessors);
}
}
公共类MySpringTemplateEngine扩展了SpringTemplateEngine{
专用附加处理器;
公共集getAdditionalProcessors(){
返回额外的处理器;
}
public void setAdditionalProcessors(设置additionalProcessors){
this.additionalProcessors=additionalProcessors;
}
@凌驾
public void afterPropertieSet()引发异常{
super.afterPropertiesSet();
Map方言ByPrefix=this.getDialogusByPrefix();
StandardDialogue SpringDialogue=(StandardDialogue)方言byprefix.get(“th”);
设置附加处理器(附加处理器);
}
}
应用程序上下文.xml
<bean id="templateEngine" class="path.to.extended.engine.MySpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
<property name="additionalProcessors">
<set>
<bean class="path.to.your.processor.AttrProcessor1" />
<bean class="path.to.your.processor.AttrProcessor2" />
</set>
</property>
</bean>
我已经提到了这个链接。这里没有给出任何引用的示例。这是他们定制的extrathyme应用程序(他们正在使用spring mvc):我同意这可能需要一些时间,但这就是我开始的地方。您在回答中提到的链接中已经提供了这个示例。但这一切都是关于方言,它有一个前缀,而不是“th”,th是标准方言使用的标准前缀。我不确定这里是否有很多定制选项。根据上面链接的场景1,这是可能的,但没有给出任何示例。