BAEL-604 Introduction to Apache velocity (#1179)
* BAEL-604 Introduction to apache velocity * BAEL-604 Introduction to apache velocity
This commit is contained in:
parent
99963ee271
commit
947fddab54
|
@ -0,0 +1,101 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<artifactId>apache-velocity</artifactId>
|
||||
|
||||
<packaging>war</packaging>
|
||||
<name>apache-velocity</name>
|
||||
|
||||
<properties>
|
||||
<jdk.version>1.8</jdk.version>
|
||||
<jstl.version>1.2</jstl.version>
|
||||
<junit.version>4.11</junit.version>
|
||||
<logback.version>1.0.13</logback.version>
|
||||
<jcl-over-slf4j.version>1.7.5</jcl-over-slf4j.version>
|
||||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||
<org.apache.httpcomponents.version>4.5.2</org.apache.httpcomponents.version>
|
||||
<velocity-version>1.7</velocity-version>
|
||||
<velocity-tools-version>2.0</velocity-tools-version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.velocity</groupId>
|
||||
<artifactId>velocity</artifactId>
|
||||
<version>${velocity-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.velocity</groupId>
|
||||
<artifactId>velocity-tools</artifactId>
|
||||
<version>${velocity-tools-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>jcl-over-slf4j</artifactId>
|
||||
<version>${jcl-over-slf4j.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<version>${logback.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>${org.apache.httpcomponents.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<finalName>apache-velocity</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${jdk.version}</source>
|
||||
<target>${jdk.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<version>${maven-war-plugin.version}</version>
|
||||
<configuration>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
</excludes>
|
||||
<systemPropertyVariables>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.apache.velocity.model;
|
||||
|
||||
public class Product {
|
||||
|
||||
private String name;
|
||||
private double price;
|
||||
|
||||
public Product(String name, double price) {
|
||||
this.name = name;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Product{" + "name='" + name + '\'' + ", price=" + price + '}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.apache.velocity.service;
|
||||
|
||||
import com.baeldung.apache.velocity.model.Product;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class ProductService {
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(ProductService.class);
|
||||
|
||||
public List<Product> getProducts() {
|
||||
logger.debug("Product service returning list of products");
|
||||
|
||||
return Arrays.asList(new Product("Laptop", 31000.00), new Product("Mobile", 16000.00),
|
||||
new Product("Tablet", 15000.00), new Product("Camera", 23000.00));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.apache.velocity.servlet;
|
||||
|
||||
import com.baeldung.apache.velocity.model.Product;
|
||||
import com.baeldung.apache.velocity.service.ProductService;
|
||||
import org.apache.velocity.Template;
|
||||
import org.apache.velocity.context.Context;
|
||||
import org.apache.velocity.tools.view.VelocityLayoutServlet;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.List;
|
||||
|
||||
public class LayoutServlet extends VelocityLayoutServlet {
|
||||
ProductService service = new ProductService();
|
||||
|
||||
@Override
|
||||
public Template handleRequest(HttpServletRequest request, HttpServletResponse response, Context context) {
|
||||
|
||||
Logger logger= LoggerFactory.getLogger(LayoutServlet.class);
|
||||
|
||||
List<Product> products = service.getProducts();
|
||||
|
||||
context.put("products", products);
|
||||
|
||||
Template template = null;
|
||||
|
||||
try {
|
||||
template = getTemplate("templates/layoutdemo.vm");
|
||||
|
||||
response.setHeader("Template Returned", "Success");
|
||||
} catch (Exception e) {
|
||||
logger.error("Error while reading the template ",e);
|
||||
}
|
||||
|
||||
return template;
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.apache.velocity.servlet;
|
||||
|
||||
import com.baeldung.apache.velocity.model.Product;
|
||||
import com.baeldung.apache.velocity.service.ProductService;
|
||||
import org.apache.velocity.Template;
|
||||
import org.apache.velocity.context.Context;
|
||||
import org.apache.velocity.tools.view.VelocityViewServlet;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.List;
|
||||
|
||||
public class ProductServlet extends VelocityViewServlet {
|
||||
|
||||
ProductService service = new ProductService();
|
||||
|
||||
@Override
|
||||
public Template handleRequest(HttpServletRequest request, HttpServletResponse response, Context context) {
|
||||
|
||||
Logger logger= LoggerFactory.getLogger(ProductServlet.class);
|
||||
|
||||
List<Product> products = service.getProducts();
|
||||
|
||||
context.put("products", products);
|
||||
|
||||
Template template = null;
|
||||
|
||||
try {
|
||||
template = getTemplate("templates/index.vm");
|
||||
response.setHeader("Template Returned", "Success");
|
||||
} catch (Exception e) {
|
||||
logger.error("Error while reading the template ", e);
|
||||
}
|
||||
|
||||
return template;
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<layout class="ch.qos.logback.classic.PatternLayout">
|
||||
|
||||
<Pattern>
|
||||
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
|
||||
</Pattern>
|
||||
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<logger name="com.baeldung.apache.velocity.service" level="debug"
|
||||
additivity="false">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</logger>
|
||||
|
||||
<root level="error">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
|
||||
</configuration>
|
|
@ -0,0 +1,4 @@
|
|||
resource.loader=webapp
|
||||
webapp.resource.loader.class=org.apache.velocity.tools.view.WebappResourceLoader
|
||||
webapp.resource.loader.path = .
|
||||
webapp.resource.loader.cache = true
|
|
@ -0,0 +1,49 @@
|
|||
<!DOCTYPE web-app PUBLIC
|
||||
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
|
||||
"http://java.sun.com/dtd/web-app_2_3.dtd" >
|
||||
|
||||
<web-app>
|
||||
<display-name>apache-velocity</display-name>
|
||||
<servlet>
|
||||
<servlet-name>ProductServlet</servlet-name>
|
||||
<servlet-class>com.baeldung.apache.velocity.servlet.ProductServlet</servlet-class>
|
||||
</servlet>
|
||||
|
||||
<servlet>
|
||||
<servlet-name>LayoutServlet</servlet-name>
|
||||
<servlet-class>com.baeldung.apache.velocity.servlet.LayoutServlet</servlet-class>
|
||||
</servlet>
|
||||
<servlet>
|
||||
<servlet-name>velocityLayout</servlet-name>
|
||||
<servlet-class>org.apache.velocity.tools.view.VelocityLayoutServlet</servlet-class>
|
||||
|
||||
<init-param>
|
||||
<param-name>org.apache.velocity.properties</param-name>
|
||||
<param-value>/WEB-INF/velocity.properties</param-value>
|
||||
</init-param>
|
||||
</servlet>
|
||||
<servlet-mapping>
|
||||
<servlet-name>ProductServlet</servlet-name>
|
||||
<url-pattern>/</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
<servlet-mapping>
|
||||
<servlet-name>LayoutServlet</servlet-name>
|
||||
<url-pattern>/layout</url-pattern>
|
||||
</servlet-mapping>
|
||||
<servlet-mapping>
|
||||
<servlet-name>velocityLayout</servlet-name>
|
||||
<url-pattern>*.vm</url-pattern>
|
||||
</servlet-mapping>
|
||||
<!-- session timeout -->
|
||||
<session-config>
|
||||
<session-timeout>
|
||||
30
|
||||
</session-timeout>
|
||||
</session-config>
|
||||
|
||||
<!-- welcome file -->
|
||||
<welcome-file-list>
|
||||
<welcome-file>index.html</welcome-file>
|
||||
</welcome-file-list>
|
||||
</web-app>
|
|
@ -0,0 +1,4 @@
|
|||
<div
|
||||
style="background: #63B175; text-align: center; padding: 5px; margin-top: 10px;">
|
||||
@Copyright baeldung.com
|
||||
</div>
|
|
@ -0,0 +1,5 @@
|
|||
<div style="background: #63B175; height: 80px; padding: 5px;">
|
||||
<div style="float: left">
|
||||
<h1> Layout Demo Page</h1>
|
||||
</div>
|
||||
</div>
|
|
@ -0,0 +1,22 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>Velocity</title>
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
#parse("/fragments/header.vm")
|
||||
</div>
|
||||
|
||||
|
||||
<div>
|
||||
|
||||
<!-- View index.vm is inserted here -->
|
||||
$screen_content
|
||||
|
||||
</div>
|
||||
|
||||
<div>
|
||||
#parse("/fragments/footer.vm")
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,63 @@
|
|||
<HTML>
|
||||
<HEAD>
|
||||
<TITLE>Online Electronic Store</TITLE>
|
||||
<style>
|
||||
body {background-color: powderblue;}
|
||||
h1 {color: blue;}
|
||||
p {color: red;}
|
||||
table.gridtable {
|
||||
font-family: verdana,arial,sans-serif;
|
||||
font-size:11px;
|
||||
color:#333333;
|
||||
border-width: 1px;
|
||||
border-color: #666666;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
table.gridtable th {
|
||||
border-width: 1px;
|
||||
padding: 8px;
|
||||
border-style: solid;
|
||||
border-color: #666666;
|
||||
background-color: #dedede;
|
||||
}
|
||||
table.gridtable td {
|
||||
border-width: 1px;
|
||||
padding: 8px;
|
||||
border-style: solid;
|
||||
border-color: #666666;
|
||||
background-color: #ffffff;
|
||||
}
|
||||
|
||||
</style>
|
||||
|
||||
</HEAD>
|
||||
<BODY>
|
||||
<CENTER>
|
||||
<h1>Today's Offers</h1>
|
||||
<BR/>
|
||||
<BR/>
|
||||
<h2>$products.size() Products on Sale!</h2>
|
||||
<BR/>
|
||||
We are proud to offer these fine products
|
||||
at these amazing prices.
|
||||
<BR/>
|
||||
<BR/>
|
||||
#set( $count = 1 )
|
||||
<TABLE class="gridtable">
|
||||
<TR>
|
||||
<TH>Serial #</TH><TH>Product Name</TH><TH>Price</TH>
|
||||
</TR>
|
||||
#foreach( $product in $products )
|
||||
<TR>
|
||||
<TD>$count)</TD>
|
||||
<TD>$product.getName()</TD>
|
||||
<TD>$product.getPrice()</TD>
|
||||
</TR>
|
||||
#set( $count = $count + 1 )
|
||||
#end
|
||||
</TABLE>
|
||||
<BR/>
|
||||
</CENTER>
|
||||
|
||||
</BODY>
|
||||
</HTML>
|
|
@ -0,0 +1,27 @@
|
|||
#set( $layout = "layout.vm" )
|
||||
<CENTER>
|
||||
<h1>Today's Offers</h1>
|
||||
<BR/>
|
||||
<BR/>
|
||||
<h2>$products.size() Products on Sale!</h2>
|
||||
<BR/>
|
||||
We are proud to offer these fine products
|
||||
at these amazing prices.
|
||||
<BR/>
|
||||
<BR/>
|
||||
#set( $count = 1 )
|
||||
<TABLE class="gridtable">
|
||||
<TR>
|
||||
<TH>Serial #</TH><TH>Product Name</TH><TH>Price</TH>
|
||||
</TR>
|
||||
#foreach( $product in $products )
|
||||
<TR>
|
||||
<TD>$count)</TD>
|
||||
<TD>$product.getName()</TD>
|
||||
<TD>$product.getPrice()</TD>
|
||||
</TR>
|
||||
#set( $count = $count + 1 )
|
||||
#end
|
||||
</TABLE>
|
||||
<BR/>
|
||||
</CENTER>
|
|
@ -0,0 +1,26 @@
|
|||
package com.baeldung.apache.velocity.servlet;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
|
||||
public class LayoutServletLiveTest {
|
||||
|
||||
@Test
|
||||
public void whenRequestUsingHttpClient_thenCorrectResponse() throws Exception {
|
||||
|
||||
HttpClient client = new DefaultHttpClient();
|
||||
HttpGet method= new HttpGet("http://localhost:8080/layout");
|
||||
|
||||
HttpResponse httpResponse = client.execute(method);
|
||||
|
||||
assertEquals("Success", httpResponse.getHeaders("Template Returned")[0].getValue());
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.apache.velocity.servlet;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ProductServletLiveTest {
|
||||
|
||||
@Test
|
||||
public void whenRequestUsingHttpClient_thenCorrectResponse() throws Exception {
|
||||
|
||||
HttpClient client = new DefaultHttpClient();
|
||||
HttpGet method= new HttpGet("http://localhost:8080/");
|
||||
|
||||
HttpResponse httpResponse = client.execute(method);
|
||||
|
||||
assertEquals("Success", httpResponse.getHeaders("Template Returned")[0].getValue());
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue