SPRINGMVC Integrated Velocity

Source: Internet
Author: User

One, what is velocity

Velocity is a Java-based template engine framework that provides a template language that can be used on objects and variables defined in Java. Velocity is a project of the Apache Foundation, and the goal is to separate the persistence and business layers in the MVC pattern. In practice, however, Velocity is not only used in the MVC architecture, but can also be used in some of the following scenarios.

1. WEB Application: Developers can use Velocity to make HTML dynamic content features without using JSP.

2. Source code generation: Velocity can be used to generate Java code, SQL, or PostScript. A lot of open source and commercially developed software is developed using Velocity.

3. Auto Email: Many software user registration, password reminders or reports are generated using Velocity. Instead of stitching strings in Java code, you can use Velocity to generate message content in a text file.

4. Conversion xml:velocity provides an ant task called Anakia that can read the XML file and let it be read by the Velocity template. A more common application is to convert Xdoc documents into styled HTML files.

Detailed reference: http://velocity.apache.org/engine/devel/overview.html


Second, how to integrate

How to use the VM to transplant the official website to learn, here only to explain how to integrate:

1. Add the Velocitylayoutviewresolver view resolver to the Springmvc.xml configuration file:

<!--Velocity Viewresolver--
<beanID= "Velocityconfig"class= "Org.springframework.web.servlet.view.velocity.VelocityConfigurer">
<propertyname= "Resourceloaderpath"value= "/web-inf/velocity/"/>
<propertyname= "Configlocation"value= "/web-inf/velocity.properties"/>
</bean>

<beanID= "Viewresolver"class= "Org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
<propertyname= "Cache"value= "true"/>
<propertyname= "prefix"value=""/>
<propertyname= "suffix"value= ". VM"/>
<propertyname= "Toolboxconfiglocation"value= "/web-inf/velocitytools.xml"/><!--Toolbox configuration file Path--
   <propertyname= "Datetoolattribute"value= "Date"/><!--Date Function name--
   <propertyname= "Numbertoolattribute"value= "Number"/><!--numeric function name--
   <propertyname= "ContentType"value= "Text/html;charset=utf-8"/>
<propertyname= "Exposespringmacrohelpers"value= "true"/><!--support for macro definitions using spring--
   <propertyname= "Exposerequestattributes"value= "true"/><!--Open Request Properties--
   <propertyname= "Requestcontextattribute"value= "req"/><!--Request Property Reference name--
   <propertyname= "Allowsessionoverride"value= "true"/>
<propertyname= "Allowrequestoverride"value= "true"/>
<propertyname= "Layouturl"value= "LAYOUT.VM"/>
</bean>

2. All view files end with. vm.

3. LAYOUT.VM is the view template, which includes the common parts of the page including head and tail. In the public part body, add the following:

$!screen_content</body>

4. In other VM files, write only the changed parts, and the system configures the following properties through the SPRINGMVC configuration file:

<property name= "Layouturl" value= "LAYOUT.VM"/>

This enables LOYOUT.VM to become a template for all other VMs.

This article is from the "Yao Sea" blog, please be sure to keep this source http://tl2011.blog.51cto.com/6006611/1787087

SPRINGMVC Integrated Velocity

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.