Skip to content

Commit 1f5f1c6

Browse files
Ashearapivovarit
authored andcommitted
Test added for mvc - velocity (eugenp#510)
* dom4j * added more parsers * StaxParser * Jaxb binding * Jaxb binding * Finish article * apply some changes * Organize imports * remove dependency * Tutorial spring-mvc with velocity templates * velocity with spring mvc * tests
1 parent 15abd8e commit 1f5f1c6

16 files changed

Lines changed: 516 additions & 16 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
<module>redis</module>
8383

8484
<module>mutation-testing</module>
85+
<module>spring-mvc-velocity</module>
8586
</modules>
8687

8788
</project>

‎spring-mvc-velocity/pom.xml‎

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<version>0.1-SNAPSHOT</version>
6+
<artifactId>spring-mvc-velocity</artifactId>
7+
8+
<name>spring-mvc-velocity</name>
9+
<packaging>war</packaging>
10+
11+
<dependencies>
12+
13+
<!-- Spring -->
14+
15+
<dependency>
16+
<groupId>org.springframework</groupId>
17+
<artifactId>spring-web</artifactId>
18+
<version>${org.springframework.version}</version>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.springframework</groupId>
22+
<artifactId>spring-webmvc</artifactId>
23+
<version>${org.springframework.version}</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.springframework</groupId>
27+
<artifactId>spring-core</artifactId>
28+
<version>${org.springframework.version}</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework</groupId>
32+
<artifactId>spring-context-support</artifactId>
33+
<version>${org.springframework.version}</version>
34+
</dependency>
35+
36+
<!-- web -->
37+
38+
<dependency>
39+
<groupId>javax.servlet</groupId>
40+
<artifactId>javax.servlet-api</artifactId>
41+
<version>3.1.0</version>
42+
<scope>provided</scope>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>org.apache.velocity</groupId>
47+
<artifactId>velocity</artifactId>
48+
<version>1.7</version>
49+
</dependency>
50+
51+
<dependency>
52+
<groupId>org.apache.velocity</groupId>
53+
<artifactId>velocity-tools</artifactId>
54+
<version>2.0</version>
55+
</dependency>
56+
57+
<!-- test scoped -->
58+
59+
<dependency>
60+
<groupId>junit</groupId>
61+
<artifactId>junit</artifactId>
62+
<version>${junit.version}</version>
63+
<scope>test</scope>
64+
</dependency>
65+
66+
<dependency>
67+
<groupId>org.hamcrest</groupId>
68+
<artifactId>hamcrest-core</artifactId>
69+
<version>${org.hamcrest.version}</version>
70+
<scope>test</scope>
71+
</dependency>
72+
<dependency>
73+
<groupId>org.hamcrest</groupId>
74+
<artifactId>hamcrest-library</artifactId>
75+
<version>${org.hamcrest.version}</version>
76+
<scope>test</scope>
77+
</dependency>
78+
79+
<dependency>
80+
<groupId>org.mockito</groupId>
81+
<artifactId>mockito-core</artifactId>
82+
<version>${mockito.version}</version>
83+
<scope>test</scope>
84+
</dependency>
85+
86+
<dependency>
87+
<groupId>org.powermock</groupId>
88+
<artifactId>powermock-module-junit4</artifactId>
89+
<version>${powermock.version}</version>
90+
<scope>test</scope>
91+
</dependency>
92+
<dependency>
93+
<groupId>org.powermock</groupId>
94+
<artifactId>powermock-api-mockito</artifactId>
95+
<version>${powermock.version}</version>
96+
<scope>test</scope>
97+
</dependency>
98+
99+
</dependencies>
100+
101+
<build>
102+
<finalName>spring-mvc-velocity</finalName>
103+
<resources>
104+
<resource>
105+
<directory>src/main/resources</directory>
106+
<filtering>true</filtering>
107+
</resource>
108+
</resources>
109+
110+
<plugins>
111+
<plugin>
112+
<groupId>org.apache.maven.plugins</groupId>
113+
<artifactId>maven-compiler-plugin</artifactId>
114+
<version>${maven-compiler-plugin.version}</version>
115+
<configuration>
116+
<source>1.8</source>
117+
<target>1.8</target>
118+
</configuration>
119+
</plugin>
120+
121+
<plugin>
122+
<groupId>org.apache.maven.plugins</groupId>
123+
<artifactId>maven-war-plugin</artifactId>
124+
<version>${maven-war-plugin.version}</version>
125+
</plugin>
126+
127+
<plugin>
128+
<groupId>org.apache.maven.plugins</groupId>
129+
<artifactId>maven-surefire-plugin</artifactId>
130+
<version>${maven-surefire-plugin.version}</version>
131+
<configuration>
132+
<excludes>
133+
<!-- <exclude>**/*ProductionTest.java</exclude> -->
134+
</excludes>
135+
<systemPropertyVariables>
136+
<!-- <provPersistenceTarget>h2</provPersistenceTarget> -->
137+
</systemPropertyVariables>
138+
</configuration>
139+
</plugin>
140+
141+
</plugins>
142+
143+
</build>
144+
145+
<properties>
146+
<!-- Spring -->
147+
<org.springframework.version>4.1.4.RELEASE</org.springframework.version>
148+
149+
150+
<!-- testing -->
151+
<org.hamcrest.version>1.3</org.hamcrest.version>
152+
<junit.version>4.12</junit.version>
153+
<mockito.version>1.10.19</mockito.version>
154+
<powermock.version>1.6.4</powermock.version>
155+
156+
<httpcore.version>4.4.1</httpcore.version>
157+
<httpclient.version>4.5</httpclient.version>
158+
159+
<rest-assured.version>2.4.1</rest-assured.version>
160+
161+
<!-- Maven plugins -->
162+
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
163+
<maven-war-plugin.version>2.6</maven-war-plugin.version>
164+
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
165+
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>
166+
<cargo-maven2-plugin.version>1.4.18</cargo-maven2-plugin.version>
167+
168+
</properties>
169+
170+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.mvc.velocity.controller;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.ui.Model;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RequestMethod;
10+
11+
import com.baeldung.mvc.velocity.domain.Tutorial;
12+
import com.baeldung.mvc.velocity.service.TutorialsService;
13+
14+
@Controller
15+
public class MainController {
16+
17+
@Autowired
18+
private TutorialsService tutService;
19+
20+
@RequestMapping(value = { "/" }, method = RequestMethod.GET)
21+
public String listTutorialsPage(Model model) {
22+
List<Tutorial> list = tutService.listTutorials();
23+
model.addAttribute("tutorials", list);
24+
return "index";
25+
}
26+
27+
public TutorialsService getTutService() {
28+
return tutService;
29+
}
30+
31+
public void setTutService(TutorialsService tutService) {
32+
this.tutService = tutService;
33+
}
34+
35+
36+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.mvc.velocity.domain;
2+
3+
public class Tutorial {
4+
5+
private Integer tutId;
6+
private String title;
7+
private String description;
8+
private String author;
9+
10+
public Tutorial() {
11+
super();
12+
}
13+
14+
public Tutorial(Integer tutId, String title, String description, String author) {
15+
super();
16+
this.tutId = tutId;
17+
this.title = title;
18+
this.description = description;
19+
this.author = author;
20+
}
21+
22+
public Integer getTutId() {
23+
return tutId;
24+
}
25+
26+
public void setTutId(Integer tutId) {
27+
this.tutId = tutId;
28+
}
29+
30+
public String getTitle() {
31+
return title;
32+
}
33+
34+
public void setTitle(String title) {
35+
this.title = title;
36+
}
37+
38+
public String getDescription() {
39+
return description;
40+
}
41+
42+
public void setDescription(String description) {
43+
this.description = description;
44+
}
45+
46+
public String getAuthor() {
47+
return author;
48+
}
49+
50+
public void setAuthor(String author) {
51+
this.author = author;
52+
}
53+
54+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.mvc.velocity.service;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.springframework.stereotype.Service;
7+
8+
import com.baeldung.mvc.velocity.domain.Tutorial;
9+
10+
@Service
11+
public class TutorialsService {
12+
13+
public List<Tutorial> listTutorials() {
14+
List<Tutorial> list = new ArrayList<Tutorial>();
15+
16+
list.add(new Tutorial(1, "Guava", "Introduction to Guava","GuavaAuthor"));
17+
list.add(new Tutorial(2, "Android", "Introduction to Android","AndroidAuthor"));
18+
return list;
19+
}
20+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div
2+
style="background: #63B175; text-align: center; padding: 5px; margin-top: 10px;">
3+
@Copyright baeldung.com
4+
</div>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div style="background: #63B175; height: 80px; padding: 5px;">
2+
<div style="float: left">
3+
<h1>Our tutorials</h1>
4+
</div>
5+
</div>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<html>
2+
<head>
3+
<title>Spring & Velocity</title>
4+
</head>
5+
<body>
6+
<div>
7+
#parse("/WEB-INF/fragments/header.vm")
8+
</div>
9+
10+
11+
<div>
12+
13+
<!-- View index.vm is inserted here -->
14+
$screen_content
15+
16+
</div>
17+
18+
<div>
19+
#parse("/WEB-INF/fragments/footer.vm")
20+
</div>
21+
</body>
22+
</html>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:context="http://www.springframework.org/schema/context"
5+
xmlns:mvc="http://www.springframework.org/schema/mvc"
6+
xsi:schemaLocation="http://www.springframework.org/schema/beans
7+
http://www.springframework.org/schema/beans/spring-beans.xsd
8+
http://www.springframework.org/schema/context
9+
http://www.springframework.org/schema/context/spring-context-4.1.xsd
10+
http://www.springframework.org/schema/mvc
11+
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
12+
13+
14+
<!-- Register the annotated components in the container eg : annotated controllers -->
15+
<context:component-scan base-package="com.baeldung.mvc.velocity.*" />
16+
17+
<!-- Tell the container that we are going to use annotations -->
18+
<context:annotation-config />
19+
20+
21+
22+
<bean id="velocityConfig"
23+
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
24+
<property name="resourceLoaderPath">
25+
<value>/</value>
26+
</property>
27+
</bean>
28+
29+
30+
<bean id="viewResolver"
31+
class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
32+
<property name="cache" value="true" />
33+
<property name="prefix" value="/WEB-INF/views/" />
34+
<property name="layoutUrl" value="/WEB-INF/layouts/layout.vm" />
35+
<property name="suffix" value=".vm" />
36+
</bean>
37+
38+
</beans>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans
5+
http://www.springframework.org/schema/beans/spring-beans.xsd">
6+
7+
<!-- Empty -->
8+
9+
</beans>

0 commit comments

Comments
 (0)