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:
parent
2399b4d817
commit
d7aecc83de
@ -1,6 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
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">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
@ -22,10 +21,15 @@
|
|||||||
<mysql.connector.java.version>5.1.45</mysql.connector.java.version>
|
<mysql.connector.java.version>5.1.45</mysql.connector.java.version>
|
||||||
<sun.tools.version>1.7.0</sun.tools.version>
|
<sun.tools.version>1.7.0</sun.tools.version>
|
||||||
<jackson.version>1.8.2</jackson.version>
|
<jackson.version>1.8.2</jackson.version>
|
||||||
<junit.version>4.11</junit.version>
|
<junit.version>4.12</junit.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/webapp/WEB-INF</directory>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.eclipse.jetty</groupId>
|
<groupId>org.eclipse.jetty</groupId>
|
||||||
@ -86,6 +90,13 @@
|
|||||||
<systemPath>${java.home}/../lib/tools.jar</systemPath>
|
<systemPath>${java.home}/../lib/tools.jar</systemPath>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.javalite</groupId>
|
||||||
|
<artifactId>activeweb-testing</artifactId>
|
||||||
|
<version>1.15</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
|
@ -3,7 +3,15 @@ package app.config;
|
|||||||
import org.javalite.activeweb.AppContext;
|
import org.javalite.activeweb.AppContext;
|
||||||
import org.javalite.activeweb.Bootstrap;
|
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 class AppBootstrap extends Bootstrap {
|
||||||
public void init(AppContext context) {
|
public void init(AppContext context) {
|
||||||
}
|
}
|
||||||
|
public Injector getInjector() {
|
||||||
|
return Guice.createInjector(new ArticleServiceModule());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
28
java-lite/src/main/java/app/controllers/ArticleController.java
Executable file
28
java-lite/src/main/java/app/controllers/ArticleController.java
Executable 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");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
11
java-lite/src/main/java/app/controllers/HomeController.java
Executable file
11
java-lite/src/main/java/app/controllers/HomeController.java
Executable file
@ -0,0 +1,11 @@
|
|||||||
|
package app.controllers;
|
||||||
|
|
||||||
|
import org.javalite.activeweb.AppController;
|
||||||
|
|
||||||
|
public class HomeController extends AppController {
|
||||||
|
|
||||||
|
public void index() {
|
||||||
|
render("index");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
50
java-lite/src/main/java/app/models/Article.java
Executable file
50
java-lite/src/main/java/app/models/Article.java
Executable 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
13
java-lite/src/main/java/app/services/ArticleService.java
Executable file
13
java-lite/src/main/java/app/services/ArticleService.java
Executable 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);
|
||||||
|
|
||||||
|
}
|
34
java-lite/src/main/java/app/services/ArticleServiceImpl.java
Executable file
34
java-lite/src/main/java/app/services/ArticleServiceImpl.java
Executable 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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
12
java-lite/src/main/java/app/services/ArticleServiceModule.java
Executable file
12
java-lite/src/main/java/app/services/ArticleServiceModule.java
Executable 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();
|
||||||
|
}
|
||||||
|
}
|
18
java-lite/src/main/webapp/WEB-INF/views/article/index.ftl
Executable file
18
java-lite/src/main/webapp/WEB-INF/views/article/index.ftl
Executable 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>
|
17
java-lite/src/main/webapp/WEB-INF/views/article/search.ftl
Executable file
17
java-lite/src/main/webapp/WEB-INF/views/article/search.ftl
Executable 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>
|
3
java-lite/src/main/webapp/WEB-INF/views/common/error.ftl
Executable file
3
java-lite/src/main/webapp/WEB-INF/views/common/error.ftl
Executable file
@ -0,0 +1,3 @@
|
|||||||
|
<@content for="title">Simple Web App</@content>
|
||||||
|
|
||||||
|
<h2>Application error</h2>
|
3
java-lite/src/main/webapp/WEB-INF/views/home/index.ftl
Executable file
3
java-lite/src/main/webapp/WEB-INF/views/home/index.ftl
Executable file
@ -0,0 +1,3 @@
|
|||||||
|
<@content for="title">Simple Web App</@content>
|
||||||
|
|
||||||
|
<h2>Baeldung ActiveWeb Demo Application</h2>
|
16
java-lite/src/main/webapp/WEB-INF/views/layouts/default_layout.ftl
Executable file
16
java-lite/src/main/webapp/WEB-INF/views/layouts/default_layout.ftl
Executable 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>
|
3
java-lite/src/main/webapp/WEB-INF/views/layouts/footer.ftl
Executable file
3
java-lite/src/main/webapp/WEB-INF/views/layouts/footer.ftl
Executable file
@ -0,0 +1,3 @@
|
|||||||
|
<div class='footer'>
|
||||||
|
<p>2018 Baeldung. No Rights Reserved.</p>
|
||||||
|
</div>
|
4
java-lite/src/main/webapp/WEB-INF/views/layouts/header.ftl
Executable file
4
java-lite/src/main/webapp/WEB-INF/views/layouts/header.ftl
Executable file
@ -0,0 +1,4 @@
|
|||||||
|
<div class="header">
|
||||||
|
<h1><a href="${context_path}">Baeldung ActiveWeb Demo</a></h1>
|
||||||
|
</div>
|
||||||
|
|
@ -6,6 +6,10 @@
|
|||||||
<filter>
|
<filter>
|
||||||
<filter-name>dispatcher</filter-name>
|
<filter-name>dispatcher</filter-name>
|
||||||
<filter-class>org.javalite.activeweb.RequestDispatcher</filter-class>
|
<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>
|
<init-param>
|
||||||
<param-name>exclusions</param-name>
|
<param-name>exclusions</param-name>
|
||||||
<param-value>css,images,js,ico</param-value>
|
<param-value>css,images,js,ico</param-value>
|
||||||
|
31
java-lite/src/test/java/app/controllers/ArticleControllerSpec.java
Executable file
31
java-lite/src/test/java/app/controllers/ArticleControllerSpec.java
Executable 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>");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user