Configure velocity templates in spring

Source: Internet
Author: User

Keyword: the velocity template in spring is first configured with the velocity engine itself. To do this, you can declare a velocityconfigurer Bean in the spring configuration file in the following ways:
<bean id= "Velocityconfigurer" class= "Org.springframework.web.servlet.view.velocity.VelocityConfigurer" > <property name= "Resourceloaderpath" > <value>WEB-INF/velocity/</value> </property> </ Bean> Velocityconfigurer is responsible for setting the velocity engine in spring. Here, we tell velocity by attribute Resourceloaderpath where to look for its template. We recommend that the templates be placed under a subdirectory of Web-inf to ensure that they are not accessed directly. You can also set the other velocity configuration details by using the Velocityproperties property. For example, the following velocityconfigurer configuration:
<bean id= "Velocityconfigurer" class= "Org.springframework.web.servlet.view.velocity.VelocityConfigurer" > <property name= "Resourceloaderpath" > <value>WEB-INF/velocity/</value> </property> < Property name= "Velocityproperties" > <props> <prop key= "Directive.foreach.counter.name" >loopcounter </prop> <prop key= "Directive.foreach.counter.initial.value" >0</prop> </props> </ Property> </bean> You can note that the Velocityproperties property uses a <props> element to set multiple properties. The properties that can be set here are the same as those set by the "velocity.properties" file in a typical velocity application. By default, Velocity's #foreach loops maintain a loop counter called $velocitycount, which counts at the beginning of the first round of the loop starting at 1. But here we set the property directive.foreach.counter.name to Loopcounter, so we'll use $loopcounter to refer to the loop counter. We also set the loop counter to count from zero by setting the property directive.foreach.counter.initial.value to 0. (For information about velocity configuration properties, refer to Velocity Developer's Guide http://jakarta.apache.org/velocity/developer-guide.html.) )
9.1.3 Analytic Velocity View
The last thing you have to do to use the Velocity template view is to configure a view parser. Specifically, you need to declare a velocityviewresolver Bean in the spring context configuration in the following way:
<bean id= "Viewresolver" class= "Org.springframework". Web.servlet.view.velocity.VelocityViewResolver "> <property name=" suffix "><value>.vm</value> The relationship between </property> </bean> velocityviewresolver and velocity is similar to that of Internalresourceviewresolver and JSP. As Internalresourceviewresolver, it uses the prefix property and the suffix property to construct the path to the template file by the logical name of the view. Here we just set the suffix property to the ". VM" extension. Because the path to the template directory has been configured with the Velocityconfigurer Resourceloaderpath property, there is no need to set a prefix here.

Note: This sets the Bean's ID to viewresolver. This is important because we have not configured Dispatcherservlet to detect all the view parsers. If you want to use multiple view parsers at the same time, you probably need to change the ID to a more appropriate name (and is unique), such as Velocityviewresolver.

Now, your application system can already render the view based on the velocity template. You only need to refer to the desired view through the logical name in the returned Modelandview object. In Listcoursecontroller, for example, there is no need to do anything else, because it has returned the following Modelandview object:

return new Modelandview ("Courselist", "courses", allcourses);

The logical name of the view is "Courselist". When parsing this view, the "courselist" plus the suffix ". VM" constitutes a template name "COURSELIST.VM". Velocityviewresolver will look for this template under the Web-inf/velocity path.

As for the "courses" model object, it is exposed as a velocity attribute to the velocity template for use. In Listing 9.1, this is the collection object that is used in the #foreach instruction.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.