Maven spring velocity integration tutorial, with code, mavenvelocity

Source: Internet
Author: User

Maven spring velocity integration tutorial, with code, mavenvelocity

1. Create a maven project and select a webapp template.

2. Add spring dependency in pom. xml.

3. Add the velocity dependency to pom. xml.

<dependency>    <groupId>org.apache.velocity</groupId>    <artifactId>velocity</artifactId>    <version>1.7</version></dependency>

4. Use spring's DispatcherServlet in web. xml and select the corresponding suffix name.

<servlet>    <servlet-name>layout</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet    </servlet-class>    <load-on-startup>1</load-on-startup></servlet><servlet-mapping>    <servlet-name>layout</servlet-name>    <url-pattern>*.htm</url-pattern>    <url-pattern>*.vm</url-pattern></servlet-mapping>

5. Use the velocity ing in the servlet configuration file.

<! -- View --> <bean id = "velocityConfig" class = "org. springframework. web. servlet. view. velocity. velocitycycler "> <property name =" resourceLoaderPath "value ="/views/velocity/"/> <! -- Configure the velocity page path --> <! -- Velocity configuration properties --> <property name = "configLocation" value = "classpath: velocity. properties"/> <! -- Velocity configuration path --> </bean> <bean id = "viewResolver" class = "org. springframework. web. servlet. view. velocity. velocityViewResolver "> <property name =" suffix "value = ". vm "/> <! -- <Property name = "suffix" value = ". htm"/> --> </bean>

6. Create a velocity/views folder in the webapp path, which corresponds to the view position in the previous step, and create a hellowrold example.

7. Add the corresponding mapping code in src/main/java.

@Controller@RequestMapping(value = "/velocity")public class VelocityDemo {    Logger logger = LoggerFactory.getLogger(VelocityDemo.class);    @RequestMapping(value = "/hellosite")    public  String index(Model model)            throws Exception {         model.addAttribute("name", "highkgao");           logger.info(model.toString());         return "hellosite";            }}

8. Enable and use tomcat to access

Http: /localhost: 8080/velocityDemo/velocity/hellosite. vm

The result is:

Hello World VM! Highkgao

Demo project download link:

Http://download.csdn.net/detail/highkgao1988/9063501

  

  

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.