Configure Velocity in Spring MVC

Source: Internet
Author: User


In the previous article we covered the content of the velocity template engine, and below we describe how to use velocity in spring mvc.

--------------------------------------------------------------------------------------------------------------- -----------------------------------------------

Velocity is a Java template engine.

Similar to Jsp,freemarker, it is used to display Web content.

Unlike JSPs, Velocity can show only the data in the action and cannot process the data. You cannot write Java code, but you can use velocity notation.

Velocity's page (template) is a file of any type (text/html).

When velocity is applied to web development, velocity separates Java code from Web pages, and interface designers can synchronize with Java program developers to develop a Web site that follows the MVC architecture.

That is, the page designer can focus on the display of the page, while the Java program developer focuses on the business logic code.


The following is a brief description of the steps to configure velocity in spring MVC:

1. The package required for the introduction of velocity

2. Adding configuration information

3. Testing


Detailed steps:

1. Introducing Velocity Package: Velocity-1.7.jar, Velocity-tools-2.0.jar, Spring-context-support-4.2.5.release.jar (ignoring version number)

Pom file configuration, the following jar package can be introduced

[HTML]  View plain  copy  print? <!--velocity start-->   <dependency>       <groupid >org.apache.velocity</groupId>       <artifactid>velocity</ artifactid>       <version>1.6</version>   </ dependency>   <dependency>       <groupId> org.apache.velocity</groupid>       <artifactid>velocity-tools</ artifactid>       <version>2.0</version>   </ dependency>   <dependency>       <groupId> org.springframework</groupid>       <artifactId> spring-context-support</artifactid>       <version>4.2.5.release</ version>   </dependency>      <!--velocity end-->  
2, add configuration information in the configuration file

The velocityconfigurer is responsible for setting the velocity engine in spring. Here, through the property resourceloaderpath tell velocity where to look for its template. It is recommended that you place the template under a subdirectory under Web-inf to ensure that the templates are not directly accessible.

There are two ways to configure properties when you configure them:

(1) by configuration file mode

[HTML] view plain copy print? <!--profile information--<bean id= "Velocityconfigurer" class= " Org.springframework.web.servlet.view.velocity.VelocityConfigurer "> <

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.