BAEL-1275 Introduction to ActiveWeb (#3638)

* BAEL-1275 Introduction to active web first commit

* BAEL-1275 Added the module to parent pom

* BAEL-1275 Introduction to ActiveWeb
This commit is contained in:
Dhrubajyoti Bhattacharjee 2018-02-24 12:48:53 +05:30 committed by Grzegorz Piwowarek
parent 2399b4d817
commit d7aecc83de
18 changed files with 277 additions and 11 deletions

View File

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung</groupId>
@ -22,10 +21,15 @@
<mysql.connector.java.version>5.1.45</mysql.connector.java.version>
<sun.tools.version>1.7.0</sun.tools.version>
<jackson.version>1.8.2</jackson.version>
<junit.version>4.11</junit.version>
<junit.version>4.12</junit.version>
</properties>
<build>
<resources>
<resource>
<directory>src/main/webapp/WEB-INF</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
@ -85,7 +89,14 @@
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
<dependency>
<groupId>org.javalite</groupId>
<artifactId>activeweb-testing</artifactId>
<version>1.15</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

View File

@ -3,7 +3,15 @@ package app.config;
import org.javalite.activeweb.AppContext;
import org.javalite.activeweb.Bootstrap;
import com.google.inject.Guice;
import com.google.inject.Injector;
import app.services.ArticleServiceModule;
public class AppBootstrap extends Bootstrap {
public void init(AppContext context) {
}
public Injector getInjector() {
return Guice.createInjector(new ArticleServiceModule());
}
}

View File

@ -0,0 +1,28 @@
package app.controllers;
import javax.inject.Inject;
import org.javalite.activeweb.AppController;
import app.services.ArticleService;
public class ArticleController extends AppController {
@Inject
private ArticleService articleService;
public void index() {
view("articles", articleService.getArticles());
}
public void search() {
String keyword = param("key");
if (null != keyword) {
assign("article", articleService.search(keyword));
} else {
render("/common/error");
}
}
}

View File

@ -0,0 +1,11 @@
package app.controllers;
import org.javalite.activeweb.AppController;
public class HomeController extends AppController {
public void index() {
render("index");
}
}

View File

@ -0,0 +1,50 @@
package app.models;
public class Article {
private String title;
private String author;
private String words;
private String date;
public Article(String title, String author, String words, String date) {
super();
this.title = title;
this.author = author;
this.words = words;
this.date = date;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getWords() {
return words;
}
public void setWords(String words) {
this.words = words;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}

View File

@ -0,0 +1,13 @@
package app.services;
import java.util.List;
import app.models.Article;
public interface ArticleService {
List<Article> getArticles();
Article search(String keyword);
}

View File

@ -0,0 +1,34 @@
package app.services;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import app.models.Article;
public class ArticleServiceImpl implements ArticleService {
public List<Article> getArticles() {
return fetchArticles();
}
public Article search(String keyword) {
Article ar = new Article("Article with " + keyword, "baeldung", "1250", Instant.now()
.toString());
return ar;
}
private List<Article> fetchArticles() {
Article ar1 = new Article("Introduction to ActiveWeb", "baeldung", "1650", Instant.now()
.toString());
Article ar = new Article("Introduction to Mule", "baeldung", "1650", Instant.now()
.toString());
List<Article> articles = new ArrayList<Article>();
articles.add(ar);
articles.add(ar1);
return articles;
}
}

View File

@ -0,0 +1,12 @@
package app.services;
import com.google.inject.AbstractModule;
public class ArticleServiceModule extends AbstractModule {
@Override
protected void configure() {
bind(ArticleService.class).to(ArticleServiceImpl.class)
.asEagerSingleton();
}
}

View File

@ -0,0 +1,18 @@
<@content for="title">Articles</@content>
<table>
<tr>
<td>Title</td>
<td>Author</td>
<td>Words #</td>
<td>Date Published</td>
</tr>
<#list articles as article>
<tr>
<td>${article.title}</td>
<td>${article.author}</td>
<td>${article.words}</td>
<td>${article.date}</td>
</tr>
</#list>
</table>

View File

@ -0,0 +1,17 @@
<@content for="title">Search</@content>
<table>
<tr>
<td>Title</td>
<td>Author</td>
<td>Words #</td>
<td>Date Published</td>
</tr>
<tr>
<td>${article.title}</td>
<td>${article.author}</td>
<td>${article.words}</td>
<td>${article.date}</td>
</tr>
</table>

View File

@ -0,0 +1,3 @@
<@content for="title">Simple Web App</@content>
<h2>Application error</h2>

View File

@ -0,0 +1,3 @@
<@content for="title">Simple Web App</@content>
<h2>Baeldung ActiveWeb Demo Application</h2>

View File

@ -0,0 +1,16 @@
<#setting url_escaping_charset='ISO-8859-1'>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<div class="main">
<#include "header.ftl" >
<div class="content">
${page_content}
</div>
<#include "footer.ftl" >
</div>
</body>
</html>

View File

@ -0,0 +1,3 @@
<div class='footer'>
<p>2018 Baeldung. No Rights Reserved.</p>
</div>

View File

@ -0,0 +1,4 @@
<div class="header">
<h1><a href="${context_path}">Baeldung ActiveWeb Demo</a></h1>
</div>

View File

@ -6,6 +6,10 @@
<filter>
<filter-name>dispatcher</filter-name>
<filter-class>org.javalite.activeweb.RequestDispatcher</filter-class>
<init-param>
<param-name>root_controller</param-name>
<param-value>home</param-value>
</init-param>
<init-param>
<param-name>exclusions</param-name>
<param-value>css,images,js,ico</param-value>

View File

@ -0,0 +1,31 @@
package app.controllers;
import org.javalite.activeweb.ControllerSpec;
import org.junit.Before;
import org.junit.Test;
import com.google.inject.Guice;
import app.services.ArticleServiceModule;
public class ArticleControllerSpec extends ControllerSpec {
@Before
public void before() {
setInjector(Guice.createInjector(new ArticleServiceModule()));
}
@Test
public void whenReturnedArticlesThenCorrect() {
request().get("index");
a(responseContent()).shouldContain("<td>Introduction to Mule</td>");
}
@Test
public void givenKeywordWhenFoundArticleThenCorrect() {
request().param("key", "Java")
.get("search");
a(responseContent()).shouldContain("<td>Article with Java</td>");
}
}

12
pom.xml
View File

@ -29,7 +29,7 @@
<modules>
<module>parent-boot-5</module>
<module>asm</module>
<module>asm</module>
<module>atomix</module>
<module>apache-cayenne</module>
<module>aws</module>
@ -90,7 +90,7 @@
<!-- <module>persistence-modules/java-cassandra</module> -->
<module>vavr</module>
<module>java-lite</module>
<module>java-rmi</module>
<module>java-rmi</module>
<module>java-vavr-stream</module>
<module>javax-servlets</module>
<module>javaxval</module>
@ -109,7 +109,7 @@
<module>libraries</module>
<module>libraries-data</module>
<module>linkrest</module>
<module>linkrest</module>
<module>logging-modules/log-mdc</module>
<module>logging-modules/log4j</module>
<module>logging-modules/log4j2</module>
@ -250,9 +250,9 @@
<module>spring-reactor</module>
<module>spring-vertx</module>
<module>spring-jinq</module>
<module>spring-rest-embedded-tomcat</module>
<module>testing-modules/testing</module>
<module>testing-modules/testng</module>
@ -279,7 +279,7 @@
<module>saas</module>
<module>deeplearning4j</module>
<module>lucene</module>
<module>vraptor</module>
<module>vraptor</module>
<module>persistence-modules/java-cockroachdb</module>
</modules>