1
    <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>

how the key word "layoutUrl" work in VelocityLayoutViewResolver?

brolanda
  • 63
  • 6

1 Answers1

2

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?

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.

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.

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>

Following your example...

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

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

MyBrainHurts
  • 2,480
  • 2
  • 27
  • 27