13.spring MVC and Velocity Tutorial
13.spring MVC and Velocity Tutorial
13.spring MVC and Velocity Tutorial
1- Introduction
2- Create Maven Project
3- Configure maven
4- Configure Spring
5- Views
6- Java Classes
7- Run Application
1- Introduction
The document is based on:
Velocity is an open source software project hosted by the Apache Software Foundation.
It is released under the Apache License.
Thus, in essence, Velocity can be used on View for displaying data. Velocity is a
language of template.
Note: Starting from version 4.3, Spring does not support Velocity, and the
Velocity will be be removed from Spring from version 5 (In the future).
Here is a conversation or quarrel between Velocity (Apache) developers and
Spring ones revolving around the reason why Velocity is not supported on the
Spring framework.
https://jira.spring.io/browse/SPR-13795
Do not worry with the error message when Project has been created. The reason is that
you have not declared Servlet library.
Note:
Eclipse 4.4 (Luna) create Maven project structure may be wrong. You need to check.
3- Configure maven
pom.xml
?
1 <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
3 http://maven.apache.org/maven-v4_0_0.xsd">
4
5
6 <modelVersion>4.0.0</modelVersion>
7 <groupId>org.o7planning</groupId>
8 <artifactId>SpringMVCVelocity</artifactId>
<packaging>war</packaging>
9 <version>0.0.1-SNAPSHOT</version>
10 <name>SpringMVCVelocity Maven Webapp</name>
11 <url>http://maven.apache.org</url>
12 <dependencies>
<dependency>
13
<groupId>junit</groupId>
14 <artifactId>junit</artifactId>
15 <version>3.8.1</version>
16 <scope>test</scope>
17 </dependency>
18
19
20
<!-- Spring framework START -->
21 <!--
22 http://mvnrepository.com/artifact/org.springframework/spring-core --
23 >
24 <dependency>
25 <groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
26 <version>4.1.4.RELEASE</version>
27 </dependency>
28
29 <!--
30 http://mvnrepository.com/artifact/org.springframework/spring-web -->
<dependency>
31 <groupId>org.springframework</groupId>
32 <artifactId>spring-web</artifactId>
33 <version>4.1.4.RELEASE</version>
34 </dependency>
35
<!--
36 http://mvnrepository.com/artifact/org.springframework/spring-webmvc
37 -->
38 <dependency>
39 <groupId>org.springframework</groupId>
40 <artifactId>spring-webmvc</artifactId>
<version>4.1.4.RELEASE</version>
41 </dependency>
42
43
44 <!-- contains: VelocityEngineFactory -->
45 <!--
46 http://mvnrepository.com/artifact/org.springframework/spring-
context-support -->
47 <dependency>
48 <groupId>org.springframework</groupId>
49 <artifactId>spring-context-support</artifactId>
50 <version>4.1.4.RELEASE</version>
51 </dependency>
<!-- Spring framework END -->
52
53
54 <!-- Servlet API -->
55 <!--
56 http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --
57 >
58 <dependency>
<groupId>javax.servlet</groupId>
59 <artifactId>javax.servlet-api</artifactId>
60 <version>3.1.0</version>
61 <scope>provided</scope>
62 </dependency>
63
64
65
66
<!--
67 http://mvnrepository.com/artifact/org.apache.velocity/velocity -->
68 <dependency>
69 <groupId>org.apache.velocity</groupId>
70 <artifactId>velocity</artifactId>
71 <version>1.7</version>
</dependency>
72
73
74 <!--
75 http://mvnrepository.com/artifact/org.apache.velocity/velocity-tools
-->
76 <dependency>
77 <groupId>org.apache.velocity</groupId>
78 <artifactId>velocity-tools</artifactId>
79 <version>2.0</version>
</dependency>
80
81
82
</dependencies>
83
84
85 <build>
86 <finalName>SpringMVCVelocity</finalName>
87 <plugins>
88
89 <!-- Config: Maven Tomcat Plugin -->
90 <!--
http://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat7-
91 maven-plugin -->
92 <plugin>
93 <groupId>org.apache.tomcat.maven</groupId>
94 <artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
95 <!-- Config: contextPath and Port (Default -
96 /SpringMVCVelocity : 8080) -->
97 <!--
98 <configuration>
99 <path>/</path>
<port>8899</port>
100 </configuration>
101 -->
102 </plugin>
103 </plugins>
104 </build>
105
106</project>
107
108
4- Configure Spring
web.xml
?
1 <web-app id="WebApp_ID" version="2.4"
2 xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
4 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
5
6 <display-name>Archetype Created Web Application</display-name>
7
8
9 <servlet>
10 <servlet-name>spring-mvc</servlet-name>
<servlet-class>
11 org.springframework.web.servlet.DispatcherServlet
12 </servlet-class>
13 <load-on-startup>1</load-on-startup>
14 </servlet>
15
16 <servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
17 <url-pattern>/</url-pattern>
18 </servlet-mapping>
19
20 <!-- Other XML Configuration -->
21 <!-- Load by Spring ContextLoaderListener -->
22 <context-param>
<param-name>contextConfigLocation</param-name>
23 <param-value>
24 /WEB-INF/root-context.xml,
25 /WEB-INF/spring-mvc-servlet.xml
26 </param-value>
27 </context-param>
28
29 <!-- Spring ContextLoaderListener -->
30 <listener>
31 <listener-
32class>org.springframework.web.context.ContextLoaderListener</listener
33-class>
</listener>
34
35</web-app>
36
37
38
spring-mvc-servlet.xml
?
<?xml version="1.0" encoding="UTF-8"?>
1 <beans xmlns="http://www.springframework.org/schema/beans"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
3 xmlns:mvc="http://www.springframework.org/schema/mvc"
4 xsi:schemaLocation="http://www.springframework.org/schema/beans
5 http://www.springframework.org/schema/beans/spring-beans.xsd
6 http://www.springframework.org/schema/context
7 http://www.springframework.org/schema/context/spring-context-
4.1.xsd
8 http://www.springframework.org/schema/mvc
9 http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
1
0
1 <!-- Register the annotated components in the container eg :
1 annotated controllers -->
<context:component-scan base-
1 package="org.o7planning.tutorial.springmvcvelocity.*" />
2
1 <!-- Tell the container that we are going to use annotations -->
3 <context:annotation-config />
1
4
1
5 <bean id="velocityConfig"
1 class="org.springframework.web.servlet.view.velocity.VelocityCon
figurer">
6 <property name="resourceLoaderPath">
1 <value>/</value>
7 </property>
</bean>
1
8
1 <bean id="viewResolver"
9 class="org.springframework.web.servlet.view.velocity.VelocityLay
outViewResolver">
2 <property name="cache" value="true" />
0 <property name="prefix" value="/WEB-INF/views/" />
2 <property name="layoutUrl" value="/WEB-INF/layouts/layout.vm" />
1 <property name="suffix" value=".vm" />
</bean>
2
2 </beans>
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0
3
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
root-context.xml
?
1<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://www.springframework.org/schema/beans
4 http://www.springframework.org/schema/beans/spring-beans.xsd">
5
<!-- Empty -->
6
7</beans>
8
9
5- Views
/WEB-INF/fragments/_header.vm
?
1<div style="background: #E0E0E0; height: 80px; padding: 5px;">
2 <div style="float: left">
<h1>My Site</h1>
3 </div>
4 <div style="float: right; padding: 10px;">
5 Search <input name="search">
6 </div>
7</div>
8
/WEB-INF/fragments/_footer.vm
?
1<div
style="background: #E0E0E0; text-align: center; padding: 5px;
2margin-top: 10px;">
3 @Copyright mysite.com
4</div>
/WEB-INF/layouts/layout.vm
?
1
2 <html>
3 <head>
4 <title>Spring & Velocity</title>
5 </head>
6 <body>
<div>
7 #parse("/WEB-INF/fragments/_header.vm")
8 </div>
9
10
11 <div>
12
13 <!-- View abc.vm is inserted here -->
14 <!-- Noi dung View abc.vm se duoc tren tai day -->
$screen_content
15
16 </div>
17
18 <div>
19 #parse("/WEB-INF/fragments/_footer.vm")
20 </div>
21</body>
22</html>
23
The $screen_content variable is used by the VelocityLayoutViewResolver to include
the result of an other VM in the layout
/WEB-INF/views/index.vm
?
1 <h1>Index</h1>
2
<h2>Department list</h2>
3 <table border="1">
4 <tr>
5 <th>Dept No</th>
6 <th>Dept Name</th>
7 </tr>
#foreach($dept in $departments)
8 <tr>
9 <td>$dept.deptNo</td>
10 <td>$dept.deptName</td>
11 </tr>
#end
12</table>
13
14
15
6- Java Classes
Department.java
?
1 package org.o7planning.tutorial.springmvcvelocity.model;
2
3 public class Department {
4
private Integer deptId;
5 private String deptNo;
6 private String deptName;
7 private String location;
8
9 public Department() {
10 }
11
public Department(Integer deptId, String deptName, String location)
12{
13 this.deptId = deptId;
14 this.deptNo = "D" + deptId;
15 this.deptName = deptName;
16 this.location = location;
}
17
18 public Integer getDeptId() {
19 return deptId;
20 }
21
22 public void setDeptId(Integer deptId) {
this.deptId = deptId;
23 }
24
25 public String getDeptNo() {
26 return deptNo;
27 }
28
29 public void setDeptNo(String deptNo) {
this.deptNo = deptNo;
30 }
31
32 public String getDeptName() {
33 return deptName;
34 }
35
36 public void setDeptName(String deptName) {
this.deptName = deptName;
37 }
38
39 public String getLocation() {
40 return location;
41 }
42
public void setLocation(String location) {
43 this.location = location;
44 }
45}
46
47
48
49
50
51
DepartmentService.java
?
package org.o7planning.tutorial.springmvcvelocity.model;
1
2 import java.util.ArrayList;
3 import java.util.List;
4
5 import org.springframework.stereotype.Service;
6
7 @Service
public class DepartmentService {
8
9 public List<Department> listDepartment() {
10 List<Department> list = new ArrayList<Department>();
11
list.add(new Department(1, "Operations", "Chicago"));
12 list.add(new Department(2, "HR", "Hanoi"));
13 return list;
14 }
15}
16
17
18
The classes annotated by @Controller or @Service be regarded as the Bean, and Spring
will automatically inject dependencies in the Field annotated by @Autowired
annotation.
MainController.java
?
1
2 package org.o7planning.tutorial.springmvcvelocity.controller;
3
4 import java.util.List;
5
import org.o7planning.tutorial.springmvcvelocity.model.Department;
6 import
7 org.o7planning.tutorial.springmvcvelocity.model.DepartmentService;
8 import org.springframework.beans.factory.annotation.Autowired;
9 import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
10import org.springframework.web.bind.annotation.RequestMapping;
11import org.springframework.web.bind.annotation.RequestMethod;
12
13@Controller
14public class MainController {
15
16 @Autowired
private DepartmentService deptService;
17
18 @RequestMapping(value = { "/", "/welcome" }, method =
19RequestMethod.GET)
20 public String welcomePage(Model model) {
21 List<Department> list = deptService.listDepartment();
model.addAttribute("departments", list);
22 return "index";
23 }
24}
25
7- Run Application
In the first, before running the application you need to build the entire project.
Right-click the project and select:
Run configurations:
Enter: