河南网站建设哪里有,网站搭建的注意事项,企业短视频推广,网站设计的内容mfc 弹簧我喜欢 Tiles#xff0c; 并且听到了很多有关 Velocity的信息 。 它们似乎有不同的用途#xff0c;并且据说很容易结合在一起#xff0c;所以我决定试一试#xff0c;并在Spring Web应用程序中同时使用它们。 集成实际上花费了许多小时#xff0c;并且是一次真正的… mfc 弹簧 我喜欢 Tiles 并且听到了很多有关 Velocity的信息 。 它们似乎有不同的用途并且据说很容易结合在一起所以我决定试一试并在Spring Web应用程序中同时使用它们。 集成实际上花费了许多小时并且是一次真正的过山车在此期间我对这三种技术都了解了很多。 希望这篇文章可以使某人免于这种乐趣并使他们专注于业务。 目标 使用tile时我不喜欢tiles.xml的默认方法。 我不想将JS和CSS的导入页面标题导航正文等放入各自的文件中如下面的代码片段所示因为这使我可以在编辑器窗口之间切换。 definition namehello template/WEB-INF/templates/main.jspput-attribute nametitle valueHello typestring /put-attribute namehead value/WEB-INF/templates/hello-js-and-css.jsp /put-attribute namenav value/WEB-INF/templates/hello-nav.jsp /put-attribute namebody value/WEB-INF/templates/hello.jsp /
/definition 显然我也不想在tiles.xml放置太多细节。 我真正喜欢的是每页一个文件将模板组装在一个位置例如这段JSP tiles:insertTemplate templatetemplate.jsptiles:putAttribute nametitle valueHello /tiles:putAttribute nameheadscript typetext/javascript src/js/jQuery.js /script typetext/javascript src/js/hello.js //tiles:putAttributetiles:putAttribute namebodydivHello, world!/div/tiles:putAttribute
/tiles:insertTemplate 在Velocity中应该看起来像这样 #tiles_insertTemplate({template: template.vm})#tiles_putAttribute({name:title, value: Hello})#end#tiles_putAttribute({name:head})script typetext/javascript src/js/jQuery.js /script typetext/javascript src/js/hello.js /#end#tiles_putAttribute({name:body})divHello, world!/div#end
#end 但是 有关集成的文档确实旨在向基于Tiles的应用程序添加一些Velocity支持而我想要的却恰恰相反在我丰富的Velocity应用程序中使用Tiles并完全支持spring上下文宏等。 解 简而言之我们要做的是 使用VelocityViewResolver解析和渲染页面 向此Velocity渲染引擎添加对Tiles宏的支持 扩展Tiles渲染器以全面支持Velocity包括Spring上下文宏等。最终我们将使其使用Spring创建的原始Velocity引擎。 github上以最小完整的Web应用程序形式提供了完整的源代码。 有关详细信息请参见下文。 弹簧和速度-瓷砖 第一步我们像这样定义viewResolver和velocityConfig Bean
public VelocityConfig velocityConfig() {VelocityConfigurer cfg new VelocityConfigurer();cfg.setResourceLoaderPath(/WEB-INF/velocity/);cfg.setConfigLocation(context.getResource(/WEB-INF/velocity.properties));return cfg;
}Bean
public ViewResolver viewResolver() {VelocityViewResolver resolver new VelocityViewResolver();resolver.setViewClass(VelocityToolboxView.class);resolver.setSuffix(.vm);return resolver;
} 重要的是我们在那里使用VelocityToolboxView 否则tile指令将不起作用。 我们还需要在velocity.properties以下内容 userdirectiveorg.apache.tiles.velocity.template.AddAttributeDirective,\org.apache.tiles.velocity.template.AddListAttributeDirective,\org.apache.tiles.velocity.template.DefinitionDirective,\org.apache.tiles.velocity.template.GetAsStringDirective,\org.apache.tiles.velocity.template.ImportAttributeDirective,\org.apache.tiles.velocity.template.InsertAttributeDirective,\org.apache.tiles.velocity.template.InsertDefinitionDirective,\org.apache.tiles.velocity.template.InsertTemplateDirective,\org.apache.tiles.velocity.template.PutAttributeDirective,\org.apache.tiles.velocity.template.PutListAttributeDirective 这为Velocity增加了对Tiles指令的基本支持但是它仍然没有用因为一旦Velocity将渲染移交给TilesTile就无法渲染Velocity并只会忽略它将#directives语法渲染到浏览器。 瓷砖-速度 我们需要教导Tiles使用Velocity。 为此我们需要一个自定义的TilesInitializer Bean
public TilesConfigurer tilesConfigurer() {TilesConfigurer cfg new TilesConfigurer();cfg.setTilesInitializer(new VelocityTilesInitializer(velocityConfig()));return cfg;
}public class VelocityTilesInitializer extends DefaultTilesInitializer {private VelocityConfig velocityConfig;public VelocityTilesInitializer(VelocityConfig velocityConfig) {this.velocityConfig velocityConfig;}Overrideprotected AbstractTilesContainerFactory createContainerFactory(TilesApplicationContext context) {return new BasicTilesContainerFactory() {Overrideprotected ListTilesRequestContextFactory getTilesRequestContextFactoriesToBeChained(ChainedTilesRequestContextFactory parent) {ListTilesRequestContextFactory factories super.getTilesRequestContextFactoriesToBeChained(parent);registerRequestContextFactory(VelocityTilesRequestContextFactory.class.getName(),factories, parent);return factories;}Overrideprotected AttributeRenderer createTemplateAttributeRenderer(BasicRendererFactory rendererFactory,TilesApplicationContext applicationContext,TilesRequestContextFactory contextFactory,TilesContainer container,AttributeEvaluatorFactory attributeEvaluatorFactory) {ContextPassingVelocityAttributeRenderer var new ContextPassingVelocityAttributeRenderer(velocityConfig.getVelocityEngine());var.setApplicationContext(applicationContext);var.setRequestContextFactory(contextFactory);var.setAttributeEvaluatorFactory(attributeEvaluatorFactory);var.commit();return var;}};}
} 我们快到了但是有一点棘手。 通常在第31-32行中您将放置velocityAttributeRenderer 。 但是此渲染器完全忽略了Tiles从Velocity接收到的Spring增强的Velocity上下文和引擎。 它创建自己的VelocityEngine并进行渲染丢弃所有Spring和tile指令以及上下文对象。 在Tiles中无法更改此行为否则在设计模式和可扩展性方面似乎是一项有趣的研究。 我什至为此创建了两个JIRA问题 541用于转发上下文而542用于注入VelocityEngine 。 同时我们必须解决此变通方法有关完整源请参见github public class ContextPassingVelocityAttributeRenderer extendsAbstractTypeDetectingAttributeRenderer {// ...private VelocityEngine engine;public ContextPassingVelocityAttributeRenderer(VelocityEngine engine) {this.engine engine;}// ...public void commit() {velocityView new VelocityView(new TilesApplicationContextJeeConfig());velocityView.setVelocityEngine(engine);}Overridepublic void write(Object value, Attribute attribute,TilesRequestContext request) throws IOException {if (value ! null) {if (value instanceof String) {InternalContextAdapter adapter (InternalContextAdapter) ((VelocityTilesRequestContext) request).getRequestObjects()[0];Context context adapter.getInternalUserContext();Template template velocityView.getTemplate((String) value);velocityView.merge(template, context, request.getWriter());} else {throw new InvalidTemplateException(Cannot render a template that is not a string: value.toString());}} else {throw new InvalidTemplateException(Cannot render a null template);}}// ... 它可以解决JIRA的两个问题并让我们实现最终目标 该VelocityEngine注入VelocityView原来这里是VelocityEngine从春天。 除其他外它支持Spring指令和上下文相关工具。 write方法中的TilesRequestContext仍然包含从Spring脚手架创建的原始Velocity上下文。 VelocityAttributeRenderer标准实现只是将其丢弃。 上面的变通办法提取原始上下文并将其用于呈现。 结论 这段旅程花了比我想象更多的时间。 尚无此类案例的文档因此我花了数小时进行调试阅读源代码进行实验祈祷和诅咒。 当我对Spring视图分辨率和渲染引擎以及Tiles和Velocity的内部知识几乎为零时它变得更加有趣。 自从我学到了很多有关所有这些技术的知识之后最终能够以一种相当优雅的方式解决它这真是令人满足。 但这也是一个令人沮丧且耗时的难题我希望这篇文章可以避免给别人带来麻烦。 更新–速度工具 不久之后我发现此解决方案不支持Velocity Tools属性。 添加方法如下 SpringVelocity Tools 。 参考我们的JCG合作伙伴 Konrad Garus在Squirrel的博客上集成了SpringVelocity和Tiles 。 翻译自: https://www.javacodegeeks.com/2012/07/integrating-spring-velocity-and-tiles.htmlmfc 弹簧