From 947fddab543f05bd73396b876d309d9491cd1437 Mon Sep 17 00:00:00 2001 From: baljeet20 Date: Wed, 15 Feb 2017 20:34:39 +0530 Subject: [PATCH] BAEL-604 Introduction to Apache velocity (#1179) * BAEL-604 Introduction to apache velocity * BAEL-604 Introduction to apache velocity --- apache-velocity/pom.xml | 101 ++++++++++++++++++ .../apache/velocity/model/Product.java | 33 ++++++ .../velocity/service/ProductService.java | 20 ++++ .../velocity/servlet/LayoutServlet.java | 41 +++++++ .../velocity/servlet/ProductServlet.java | 40 +++++++ .../src/main/resources/logback.xml | 23 ++++ .../main/webapp/WEB-INF/velocity.properties | 4 + .../src/main/webapp/WEB-INF/web.xml | 49 +++++++++ .../src/main/webapp/fragments/footer.vm | 4 + .../src/main/webapp/fragments/header.vm | 5 + .../src/main/webapp/layout/Default.vm | 22 ++++ .../src/main/webapp/templates/index.vm | 63 +++++++++++ .../src/main/webapp/templates/layoutdemo.vm | 27 +++++ .../servlet/LayoutServletLiveTest.java | 26 +++++ .../servlet/ProductServletLiveTest.java | 24 +++++ pom.xml | 2 + 16 files changed, 484 insertions(+) create mode 100644 apache-velocity/pom.xml create mode 100644 apache-velocity/src/main/java/com/baeldung/apache/velocity/model/Product.java create mode 100644 apache-velocity/src/main/java/com/baeldung/apache/velocity/service/ProductService.java create mode 100644 apache-velocity/src/main/java/com/baeldung/apache/velocity/servlet/LayoutServlet.java create mode 100644 apache-velocity/src/main/java/com/baeldung/apache/velocity/servlet/ProductServlet.java create mode 100644 apache-velocity/src/main/resources/logback.xml create mode 100644 apache-velocity/src/main/webapp/WEB-INF/velocity.properties create mode 100644 apache-velocity/src/main/webapp/WEB-INF/web.xml create mode 100644 apache-velocity/src/main/webapp/fragments/footer.vm create mode 100644 apache-velocity/src/main/webapp/fragments/header.vm create mode 100644 apache-velocity/src/main/webapp/layout/Default.vm create mode 100644 apache-velocity/src/main/webapp/templates/index.vm create mode 100644 apache-velocity/src/main/webapp/templates/layoutdemo.vm create mode 100644 apache-velocity/src/test/java/com/baeldung/apache/velocity/servlet/LayoutServletLiveTest.java create mode 100644 apache-velocity/src/test/java/com/baeldung/apache/velocity/servlet/ProductServletLiveTest.java diff --git a/apache-velocity/pom.xml b/apache-velocity/pom.xml new file mode 100644 index 0000000000..08f0e96a58 --- /dev/null +++ b/apache-velocity/pom.xml @@ -0,0 +1,101 @@ + + + 4.0.0 + com.baeldung + 0.1-SNAPSHOT + apache-velocity + + war + apache-velocity + + + 1.8 + 1.2 + 4.11 + 1.0.13 + 1.7.5 + 3.6.0 + 2.6 + 2.19.1 + 4.5.2 + 1.7 + 2.0 + + + + + junit + junit + ${junit.version} + test + + + org.apache.velocity + velocity + ${velocity-version} + + + org.apache.velocity + velocity-tools + ${velocity-tools-version} + + + org.slf4j + jcl-over-slf4j + ${jcl-over-slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + org.apache.httpcomponents + httpclient + ${org.apache.httpcomponents.version} + test + + + + apache-velocity + + + src/main/resources + true + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${jdk.version} + ${jdk.version} + + + + org.apache.maven.plugins + maven-war-plugin + ${maven-war-plugin.version} + + false + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + **/*LiveTest.java + + + + + + + + diff --git a/apache-velocity/src/main/java/com/baeldung/apache/velocity/model/Product.java b/apache-velocity/src/main/java/com/baeldung/apache/velocity/model/Product.java new file mode 100644 index 0000000000..c215223181 --- /dev/null +++ b/apache-velocity/src/main/java/com/baeldung/apache/velocity/model/Product.java @@ -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 + '}'; + } +} diff --git a/apache-velocity/src/main/java/com/baeldung/apache/velocity/service/ProductService.java b/apache-velocity/src/main/java/com/baeldung/apache/velocity/service/ProductService.java new file mode 100644 index 0000000000..0a623d4d65 --- /dev/null +++ b/apache-velocity/src/main/java/com/baeldung/apache/velocity/service/ProductService.java @@ -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 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)); + } +} diff --git a/apache-velocity/src/main/java/com/baeldung/apache/velocity/servlet/LayoutServlet.java b/apache-velocity/src/main/java/com/baeldung/apache/velocity/servlet/LayoutServlet.java new file mode 100644 index 0000000000..d4208a3880 --- /dev/null +++ b/apache-velocity/src/main/java/com/baeldung/apache/velocity/servlet/LayoutServlet.java @@ -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 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; + + } + +} diff --git a/apache-velocity/src/main/java/com/baeldung/apache/velocity/servlet/ProductServlet.java b/apache-velocity/src/main/java/com/baeldung/apache/velocity/servlet/ProductServlet.java new file mode 100644 index 0000000000..60c9169fce --- /dev/null +++ b/apache-velocity/src/main/java/com/baeldung/apache/velocity/servlet/ProductServlet.java @@ -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 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; + + } +} diff --git a/apache-velocity/src/main/resources/logback.xml b/apache-velocity/src/main/resources/logback.xml new file mode 100644 index 0000000000..70a420a57a --- /dev/null +++ b/apache-velocity/src/main/resources/logback.xml @@ -0,0 +1,23 @@ + + + + + + + + %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + + + + + + + \ No newline at end of file diff --git a/apache-velocity/src/main/webapp/WEB-INF/velocity.properties b/apache-velocity/src/main/webapp/WEB-INF/velocity.properties new file mode 100644 index 0000000000..00e0b7e410 --- /dev/null +++ b/apache-velocity/src/main/webapp/WEB-INF/velocity.properties @@ -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 \ No newline at end of file diff --git a/apache-velocity/src/main/webapp/WEB-INF/web.xml b/apache-velocity/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..95b41b36dd --- /dev/null +++ b/apache-velocity/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,49 @@ + + + + apache-velocity + + ProductServlet + com.baeldung.apache.velocity.servlet.ProductServlet + + + + LayoutServlet + com.baeldung.apache.velocity.servlet.LayoutServlet + + + velocityLayout + org.apache.velocity.tools.view.VelocityLayoutServlet + + + org.apache.velocity.properties + /WEB-INF/velocity.properties + + + + ProductServlet + / + + + + LayoutServlet + /layout + + + velocityLayout + *.vm + + + + + 30 + + + + + + index.html + + diff --git a/apache-velocity/src/main/webapp/fragments/footer.vm b/apache-velocity/src/main/webapp/fragments/footer.vm new file mode 100644 index 0000000000..41bb36ce5e --- /dev/null +++ b/apache-velocity/src/main/webapp/fragments/footer.vm @@ -0,0 +1,4 @@ +
+ @Copyright baeldung.com +
\ No newline at end of file diff --git a/apache-velocity/src/main/webapp/fragments/header.vm b/apache-velocity/src/main/webapp/fragments/header.vm new file mode 100644 index 0000000000..96700d3baf --- /dev/null +++ b/apache-velocity/src/main/webapp/fragments/header.vm @@ -0,0 +1,5 @@ +
+
+

Layout Demo Page

+
+
\ No newline at end of file diff --git a/apache-velocity/src/main/webapp/layout/Default.vm b/apache-velocity/src/main/webapp/layout/Default.vm new file mode 100644 index 0000000000..39a8b277a5 --- /dev/null +++ b/apache-velocity/src/main/webapp/layout/Default.vm @@ -0,0 +1,22 @@ + + + Velocity + + +
+ #parse("/fragments/header.vm") +
+ + +
+ + + $screen_content + +
+ +
+ #parse("/fragments/footer.vm") +
+ + \ No newline at end of file diff --git a/apache-velocity/src/main/webapp/templates/index.vm b/apache-velocity/src/main/webapp/templates/index.vm new file mode 100644 index 0000000000..0ca07caf42 --- /dev/null +++ b/apache-velocity/src/main/webapp/templates/index.vm @@ -0,0 +1,63 @@ + + + Online Electronic Store + + + + +
+

Today's Offers

+
+
+

$products.size() Products on Sale!

+
+ We are proud to offer these fine products + at these amazing prices. +
+
+ #set( $count = 1 ) + + + + + #foreach( $product in $products ) + + + + + + #set( $count = $count + 1 ) + #end +
Serial #Product NamePrice
$count)$product.getName()$product.getPrice()
+
+
+ + + diff --git a/apache-velocity/src/main/webapp/templates/layoutdemo.vm b/apache-velocity/src/main/webapp/templates/layoutdemo.vm new file mode 100644 index 0000000000..0626b655c9 --- /dev/null +++ b/apache-velocity/src/main/webapp/templates/layoutdemo.vm @@ -0,0 +1,27 @@ +#set( $layout = "layout.vm" ) +
+

Today's Offers

+
+
+

$products.size() Products on Sale!

+
+ We are proud to offer these fine products + at these amazing prices. +
+
+ #set( $count = 1 ) + + + + + #foreach( $product in $products ) + + + + + + #set( $count = $count + 1 ) + #end +
Serial #Product NamePrice
$count)$product.getName()$product.getPrice()
+
+
\ No newline at end of file diff --git a/apache-velocity/src/test/java/com/baeldung/apache/velocity/servlet/LayoutServletLiveTest.java b/apache-velocity/src/test/java/com/baeldung/apache/velocity/servlet/LayoutServletLiveTest.java new file mode 100644 index 0000000000..f1f166b119 --- /dev/null +++ b/apache-velocity/src/test/java/com/baeldung/apache/velocity/servlet/LayoutServletLiveTest.java @@ -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()); + + } + +} diff --git a/apache-velocity/src/test/java/com/baeldung/apache/velocity/servlet/ProductServletLiveTest.java b/apache-velocity/src/test/java/com/baeldung/apache/velocity/servlet/ProductServletLiveTest.java new file mode 100644 index 0000000000..397e575d4d --- /dev/null +++ b/apache-velocity/src/test/java/com/baeldung/apache/velocity/servlet/ProductServletLiveTest.java @@ -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()); + + } +} diff --git a/pom.xml b/pom.xml index 9eb4e78dbf..ecb07e987b 100644 --- a/pom.xml +++ b/pom.xml @@ -192,6 +192,8 @@ xstream struts2 + apache-velocity +