SpringMVC integrates velocity and springmvcvelocity

Source: Internet
Author: User

SpringMVC integrates velocity and springmvcvelocity
Preface

In the era where there is no artist, the corresponding template view development is inevitable. In the jsp era, we are also very comfortable to use, and everything is absolutely necessary. when the project is large, and the data model is complicated, jsp cannot be competent.

 

Development Environment

Idea2016, jdk1.8, and tomcat8.0.35

Spring 4.3.6, velocity1.6, and velocity-tools 2.0

 

SpringMVC integrates velocity

1. VelocityViewResolver.

<! -- VelocityViewResolver view configuration --> <! -- <Bean id = "viewResolver" class = "org. springframework. web. servlet. view. velocity. VelocityViewResolver"> --> <! -- <Property name = "suffix" value = ". vm"/> --> <! -- <Property name = "prefix" value = ""/> --> <! -- <Property name = "contentType" value = "text/html; charset = UTF-8"/> --> <! -- </Bean> -->

 

2. VelocityLayoutViewResolver. That is, you can customize the template layout. Compared with VelocityViewResolver, a layout. vm is added, which is configured in the WEB-INF/views/layout. vm.

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

 

3, complete springmvc-servlet.xml

<Context: component-scan base-package = "com. autohome. controller"/> <! -- Annotation-driven --> <mvc: annotation-driven/> <! -- Velocity template configuration --> <bean id = "velocityConfig" class = "org. springframework. web. servlet. view. velocity. velocitycycler "> <property name =" resourceLoaderPath "value ="/WEB-INF/views/"/> <property name =" configLocation "value =" classpath: velocity. properties "/> <property name =" velocityProperties "> <props> <prop key =" input. UTF-8 </prop> <prop key = "output. encoding "> UTF-8 </prop> </props> </property> </ Bean> <! -- VelocityViewResolver view configuration --> <! -- <Bean id = "viewResolver" class = "org. springframework. web. servlet. view. velocity. VelocityViewResolver"> --> <! -- <Property name = "suffix" value = ". vm"/> --> <! -- <Property name = "prefix" value = ""/> --> <! -- <Property name = "contentType" value = "text/html; charset = UTF-8"/> --> <! -- </Bean> --> <! -- VelocityLayoutViewResolver --> <bean id = "viewResolver" class = "org. springframework. web. servlet. view. velocity. velocityLayoutViewResolver "> <property name =" suffix "value = ". vm "/> <property name =" prefix "value =" "/> <property name =" contentType "value =" text/html; charset = UTF-8 "/> <property name =" layoutUrl "value =" layout/layout. vm "/> </bean>

4. pom. xml

 <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>4.10</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-context</artifactId>      <version>4.3.6.RELEASE</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-web</artifactId>      <version>4.3.6.RELEASE</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-context-support</artifactId>      <version>4.3.6.RELEASE</version>    </dependency>    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-webmvc</artifactId>      <version>4.3.6.RELEASE</version>    </dependency>    <dependency>      <groupId>org.apache.velocity</groupId>      <artifactId>velocity</artifactId>      <version>1.6.2</version>    </dependency>    <dependency>      <groupId>org.apache.velocity</groupId>      <artifactId>velocity-tools</artifactId>      <version>2.0</version>    </dependency>  </dependencies>

The nested content of my layout. vm. view page is rendered in $ screen_content.

Index. vm

Layout. vm rendering result

 

Summary

1. pom does not automatically load the spring-context.jar package when downloading the spring-context-support.jar package and I cannot find the configLocation attribute when compiling the velocityconfig velocitypolicer class in springmvc-servlet. After adding the spring-context jar package, OK.

2. Now in velocity. the encoding format has been configured in the properties file, and the encoding method is still configured in velocityconfig. If not configured, a spring is thrown. vm-related exceptions. I don't know what this design is.

 

References

Http://shishi11.iteye.com/blog/869290

 

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.