GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BAEL-604 Introduction to Apache velocity (#1179) · code-ql-testing/tutorials@947fdda

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apache-velocity
    • pom.xml
    • src
      • main
        • java/com/baeldung/apache/velocity
          • model
            • Product.java
          • service
            • ProductService.java
          • servlet
            • LayoutServlet.java
            • ProductServlet.java
        • resources
          • logback.xml
        • webapp
          • WEB-INF
            • velocity.properties
            • web.xml
          • fragments
            • footer.vm
            • header.vm
          • layout
            • Default.vm
          • templates
            • index.vm
            • layoutdemo.vm
      • test/java/com/baeldung/apache/velocity/servlet
        • LayoutServletLiveTest.java
        • ProductServletLiveTest.java
  • pom.xml
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apache-velocity/pom.xml‎

Copy file name to clipboard
+101Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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/maven-v4_0_0.xsd">
3+
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.baeldung</groupId>
6+
<version>0.1-SNAPSHOT</version>
7+
<artifactId>apache-velocity</artifactId>
8+
9+
<packaging>war</packaging>
10+
<name>apache-velocity</name>
11+
12+
<properties>
13+
<jdk.version>1.8</jdk.version>
14+
<jstl.version>1.2</jstl.version>
15+
<junit.version>4.11</junit.version>
16+
<logback.version>1.0.13</logback.version>
17+
<jcl-over-slf4j.version>1.7.5</jcl-over-slf4j.version>
18+
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
19+
<maven-war-plugin.version>2.6</maven-war-plugin.version>
20+
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
21+
<org.apache.httpcomponents.version>4.5.2</org.apache.httpcomponents.version>
22+
<velocity-version>1.7</velocity-version>
23+
<velocity-tools-version>2.0</velocity-tools-version>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>junit</groupId>
29+
<artifactId>junit</artifactId>
30+
<version>${junit.version}</version>
31+
<scope>test</scope>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.apache.velocity</groupId>
35+
<artifactId>velocity</artifactId>
36+
<version>${velocity-version}</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.apache.velocity</groupId>
40+
<artifactId>velocity-tools</artifactId>
41+
<version>${velocity-tools-version}</version>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.slf4j</groupId>
45+
<artifactId>jcl-over-slf4j</artifactId>
46+
<version>${jcl-over-slf4j.version}</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>ch.qos.logback</groupId>
50+
<artifactId>logback-classic</artifactId>
51+
<version>${logback.version}</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.apache.httpcomponents</groupId>
55+
<artifactId>httpclient</artifactId>
56+
<version>${org.apache.httpcomponents.version}</version>
57+
<scope>test</scope>
58+
</dependency>
59+
</dependencies>
60+
<build>
61+
<finalName>apache-velocity</finalName>
62+
<resources>
63+
<resource>
64+
<directory>src/main/resources</directory>
65+
<filtering>true</filtering>
66+
</resource>
67+
</resources>
68+
<plugins>
69+
<plugin>
70+
<groupId>org.apache.maven.plugins</groupId>
71+
<artifactId>maven-compiler-plugin</artifactId>
72+
<version>${maven-compiler-plugin.version}</version>
73+
<configuration>
74+
<source>${jdk.version}</source>
75+
<target>${jdk.version}</target>
76+
</configuration>
77+
</plugin>
78+
<plugin>
79+
<groupId>org.apache.maven.plugins</groupId>
80+
<artifactId>maven-war-plugin</artifactId>
81+
<version>${maven-war-plugin.version}</version>
82+
<configuration>
83+
<failOnMissingWebXml>false</failOnMissingWebXml>
84+
</configuration>
85+
</plugin>
86+
87+
<plugin>
88+
<groupId>org.apache.maven.plugins</groupId>
89+
<artifactId>maven-surefire-plugin</artifactId>
90+
<version>${maven-surefire-plugin.version}</version>
91+
<configuration>
92+
<excludes>
93+
<exclude>**/*LiveTest.java</exclude>
94+
</excludes>
95+
<systemPropertyVariables>
96+
</systemPropertyVariables>
97+
</configuration>
98+
</plugin>
99+
</plugins>
100+
</build>
101+
</project>
Collapse file

‎apache-velocity/src/main/java/com/baeldung/apache/velocity/model/Product.java‎

Copy file name to clipboard
+33Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.apache.velocity.model;
2+
3+
public class Product {
4+
5+
private String name;
6+
private double price;
7+
8+
public Product(String name, double price) {
9+
this.name = name;
10+
this.price = price;
11+
}
12+
13+
public String getName() {
14+
return name;
15+
}
16+
17+
public void setName(String name) {
18+
this.name = name;
19+
}
20+
21+
public double getPrice() {
22+
return price;
23+
}
24+
25+
public void setPrice(double price) {
26+
this.price = price;
27+
}
28+
29+
@Override
30+
public String toString() {
31+
return "Product{" + "name='" + name + '\'' + ", price=" + price + '}';
32+
}
33+
}
Collapse file

‎apache-velocity/src/main/java/com/baeldung/apache/velocity/service/ProductService.java‎

Copy file name to clipboard
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.apache.velocity.service;
2+
3+
import com.baeldung.apache.velocity.model.Product;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
public class ProductService {
11+
12+
Logger logger = LoggerFactory.getLogger(ProductService.class);
13+
14+
public List<Product> getProducts() {
15+
logger.debug("Product service returning list of products");
16+
17+
return Arrays.asList(new Product("Laptop", 31000.00), new Product("Mobile", 16000.00),
18+
new Product("Tablet", 15000.00), new Product("Camera", 23000.00));
19+
}
20+
}
Collapse file

‎apache-velocity/src/main/java/com/baeldung/apache/velocity/servlet/LayoutServlet.java‎

Copy file name to clipboard
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.apache.velocity.servlet;
2+
3+
import com.baeldung.apache.velocity.model.Product;
4+
import com.baeldung.apache.velocity.service.ProductService;
5+
import org.apache.velocity.Template;
6+
import org.apache.velocity.context.Context;
7+
import org.apache.velocity.tools.view.VelocityLayoutServlet;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
import javax.servlet.http.HttpServletRequest;
12+
import javax.servlet.http.HttpServletResponse;
13+
import java.util.List;
14+
15+
public class LayoutServlet extends VelocityLayoutServlet {
16+
ProductService service = new ProductService();
17+
18+
@Override
19+
public Template handleRequest(HttpServletRequest request, HttpServletResponse response, Context context) {
20+
21+
Logger logger= LoggerFactory.getLogger(LayoutServlet.class);
22+
23+
List<Product> products = service.getProducts();
24+
25+
context.put("products", products);
26+
27+
Template template = null;
28+
29+
try {
30+
template = getTemplate("templates/layoutdemo.vm");
31+
32+
response.setHeader("Template Returned", "Success");
33+
} catch (Exception e) {
34+
logger.error("Error while reading the template ",e);
35+
}
36+
37+
return template;
38+
39+
}
40+
41+
}
Collapse file

‎apache-velocity/src/main/java/com/baeldung/apache/velocity/servlet/ProductServlet.java‎

Copy file name to clipboard
+40Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.apache.velocity.servlet;
2+
3+
import com.baeldung.apache.velocity.model.Product;
4+
import com.baeldung.apache.velocity.service.ProductService;
5+
import org.apache.velocity.Template;
6+
import org.apache.velocity.context.Context;
7+
import org.apache.velocity.tools.view.VelocityViewServlet;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
import javax.servlet.http.HttpServletRequest;
12+
import javax.servlet.http.HttpServletResponse;
13+
import java.util.List;
14+
15+
public class ProductServlet extends VelocityViewServlet {
16+
17+
ProductService service = new ProductService();
18+
19+
@Override
20+
public Template handleRequest(HttpServletRequest request, HttpServletResponse response, Context context) {
21+
22+
Logger logger= LoggerFactory.getLogger(ProductServlet.class);
23+
24+
List<Product> products = service.getProducts();
25+
26+
context.put("products", products);
27+
28+
Template template = null;
29+
30+
try {
31+
template = getTemplate("templates/index.vm");
32+
response.setHeader("Template Returned", "Success");
33+
} catch (Exception e) {
34+
logger.error("Error while reading the template ", e);
35+
}
36+
37+
return template;
38+
39+
}
40+
}
Collapse file

‎apache-velocity/src/main/resources/logback.xml‎

Copy file name to clipboard
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
4+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
5+
<layout class="ch.qos.logback.classic.PatternLayout">
6+
7+
<Pattern>
8+
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
9+
</Pattern>
10+
11+
</layout>
12+
</appender>
13+
14+
<logger name="com.baeldung.apache.velocity.service" level="debug"
15+
additivity="false">
16+
<appender-ref ref="STDOUT" />
17+
</logger>
18+
19+
<root level="error">
20+
<appender-ref ref="STDOUT" />
21+
</root>
22+
23+
</configuration>
Collapse file

‎apache-velocity/src/main/webapp/WEB-INF/velocity.properties‎

Copy file name to clipboard
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
resource.loader=webapp
2+
webapp.resource.loader.class=org.apache.velocity.tools.view.WebappResourceLoader
3+
webapp.resource.loader.path = .
4+
webapp.resource.loader.cache = true
Collapse file

‎apache-velocity/src/main/webapp/WEB-INF/web.xml‎

Copy file name to clipboard
+49Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!DOCTYPE web-app PUBLIC
2+
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
3+
"http://java.sun.com/dtd/web-app_2_3.dtd" >
4+
5+
<web-app>
6+
<display-name>apache-velocity</display-name>
7+
<servlet>
8+
<servlet-name>ProductServlet</servlet-name>
9+
<servlet-class>com.baeldung.apache.velocity.servlet.ProductServlet</servlet-class>
10+
</servlet>
11+
12+
<servlet>
13+
<servlet-name>LayoutServlet</servlet-name>
14+
<servlet-class>com.baeldung.apache.velocity.servlet.LayoutServlet</servlet-class>
15+
</servlet>
16+
<servlet>
17+
<servlet-name>velocityLayout</servlet-name>
18+
<servlet-class>org.apache.velocity.tools.view.VelocityLayoutServlet</servlet-class>
19+
20+
<init-param>
21+
<param-name>org.apache.velocity.properties</param-name>
22+
<param-value>/WEB-INF/velocity.properties</param-value>
23+
</init-param>
24+
</servlet>
25+
<servlet-mapping>
26+
<servlet-name>ProductServlet</servlet-name>
27+
<url-pattern>/</url-pattern>
28+
</servlet-mapping>
29+
30+
<servlet-mapping>
31+
<servlet-name>LayoutServlet</servlet-name>
32+
<url-pattern>/layout</url-pattern>
33+
</servlet-mapping>
34+
<servlet-mapping>
35+
<servlet-name>velocityLayout</servlet-name>
36+
<url-pattern>*.vm</url-pattern>
37+
</servlet-mapping>
38+
<!-- session timeout -->
39+
<session-config>
40+
<session-timeout>
41+
30
42+
</session-timeout>
43+
</session-config>
44+
45+
<!-- welcome file -->
46+
<welcome-file-list>
47+
<welcome-file>index.html</welcome-file>
48+
</welcome-file-list>
49+
</web-app>
Collapse file

‎apache-velocity/src/main/webapp/fragments/footer.vm‎

Copy file name to clipboard
+4Lines 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>
Collapse file

‎apache-velocity/src/main/webapp/fragments/header.vm‎

Copy file name to clipboard
+5Lines 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> Layout Demo Page</h1>
4+
</div>
5+
</div>

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.