如何在具有速度的 spring-webmvc 中使用 VelocityLayoutViewResolver [英] How to use VelocityLayoutViewResolver in spring-webmvc with velocity

查看:19
本文介绍了如何在具有速度的 spring-webmvc 中使用 VelocityLayoutViewResolver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    <bean id="viewResolver" class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
    <property name="prefix" value="" />
    <property name="suffix" value=".vm"></property>
    <property name="contentType" value="text/html;charset=UTF-8" />
    <property name="layoutUrl" value="layout/default.vm" />
</bean>

关键字layoutUrl"在 VelocityLayoutViewResolver 中是如何工作的?

how the key word "layoutUrl" work in VelocityLayoutViewResolver?

推荐答案

将动态网页分为布局部分和内容部分是很常见的.布局部分可能由页眉、页脚、侧边栏、导航等组成.也就是说,每个响应中的元素看起来或多或少都相同.但内容部分不同,因为那是动作发生的地方,对吗?

Its very common to have a dynamic web page divided into a layout part and a content part. The layout part might consist of a header, a footer, a sidebar, a navigation and so on. Elements meant to look more or less the same on every response, that is. But the content part differs, because that's where the action goes on, right?

布局和内容应该在不同的 .vm 文件中分开,这样布局只需设计(和更改)一次,内容部分不必重复任何内容.

Layout and content should be kept apart in different .vm files, so that the layout has to be designed (and changed) only once and the content part doesn't have to repeat anything.

问题是如何将这两个部分放在每个响应中.一种方法是解析每个内容文件中的布局文件.但由于布局通常会包装内容,这很可能导致每个内容文件解析的布局文件不止一个.

The question is how to put those two parts together on each response. One approach is to parse the layout file in every content file. But as the layout usually wraps the content this very likely leads to more than one parsed layout file per content file.

更好的方法是将其反转并将内容合并到布局中.这更容易处理.您所要做的就是声明一个 .vm 文件作为通用布局文件.在此文件中,您放置了一个名为 $screen_content 的变量,并且神奇地将您在控制器中根据特定请求返回的视图混合在该位置.

A better way is to reverse that and to merge the content into the layout. This is way easier to handle. All you have to do is to declare a .vm file to work as the general layout file. In this file you put a var named $screen_content and magically the view you returned in your controller at a certain request is blended in at that spot.

你的 layoutUrl 属性告诉你的布局文件相对于你在这个 bean 中声明的 resourceLoaderPath 的路径和文件名

Your layoutUrl property tells path and file name of your layout file relative to the resourceLoaderPath you have declared in this bean

<bean
    id="velocityConfig"
    class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
    <beans:property name="resourceLoaderPath" value="/WEB-INF/templates/" />
</bean>

按照你的例子...

<bean 
    id="viewResolver"
    class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
    ...
    <property name="layoutUrl" value="layout/default.vm" />
</bean>

...你的布局文件必须是 /WEB-INF/templates/layout/default.vm

...your layout file has to be /WEB-INF/templates/layout/default.vm

这篇关于如何在具有速度的 spring-webmvc 中使用 VelocityLayoutViewResolver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆