日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

mfc 弹簧_整合弹簧,速度和瓷砖

發布時間:2023/12/3 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mfc 弹簧_整合弹簧,速度和瓷砖 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

mfc 彈簧

我喜歡 Tiles, 并且聽到了很多有關 Velocity的信息 。 它們似乎有不同的用途,并且據說很容易結合在一起,所以我決定試一試,并在Spring Web應用程序中同時使用它們。 集成實際上花費了許多小時,并且是一次真正的過山車,在此期間,我對這三種技術都了解了很多。 希望這篇文章可以使某人免于這種樂趣,并使他們專注于業務。

目標

使用tile時,我不喜歡tiles.xml的默認(?)方法。 我不想將JS和CSS的導入,頁面標題,導航,正文等放入各自的文件中,如下面的代碼片段所示,因為這使我可以在編輯器窗口之間切換。

<definition name='hello' template='/WEB-INF/templates/main.jsp'><put-attribute name='title' value='Hello' type='string' /><put-attribute name='head' value='/WEB-INF/templates/hello-js-and-css.jsp' /><put-attribute name='nav' value='/WEB-INF/templates/hello-nav.jsp' /><put-attribute name='body' value='/WEB-INF/templates/hello.jsp' /> </definition>

顯然,我也不想在tiles.xml放置太多細節。

我真正喜歡的是每頁一個文件,將模板組裝在一個位置,例如這段JSP:

<tiles:insertTemplate template='template.jsp'><tiles:putAttribute name='title' value='Hello' /><tiles:putAttribute name='head'><script type='text/javascript' src='/js/jQuery.js' /><script type='text/javascript' src='/js/hello.js' /></tiles:putAttribute><tiles:putAttribute name='body'><div>Hello, world!</div></tiles:putAttribute> </tiles:insertTemplate>

在Velocity中,應該看起來像這樣 :

#tiles_insertTemplate({'template': 'template.vm'})#tiles_putAttribute({'name':'title', 'value': 'Hello'})#end#tiles_putAttribute({'name':'head'})<script type='text/javascript' src='/js/jQuery.js' /><script type='text/javascript' src='/js/hello.js' />#end#tiles_putAttribute({'name':'body'})<div>Hello, 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以下內容:

    userdirective=org.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將渲染移交給Tiles,Tile就無法渲染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 List<TilesRequestContextFactory> getTilesRequestContextFactoriesToBeChained(ChainedTilesRequestContextFactory parent) {List<TilesRequestContextFactory> 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屬性。 添加方法如下: Spring&Velocity Tools 。

    參考:我們的JCG合作伙伴 Konrad Garus在Squirrel的博客上集成了Spring,Velocity和Tiles 。


    翻譯自: https://www.javacodegeeks.com/2012/07/integrating-spring-velocity-and-tiles.html

    mfc 彈簧

    創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

    總結

    以上是生活随笔為你收集整理的mfc 弹簧_整合弹簧,速度和瓷砖的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。