Skip to content

Commit d7aecc8

Browse files
dhruba619pivovarit
authored andcommitted
BAEL-1275 Introduction to ActiveWeb (eugenp#3638)
* BAEL-1275 Introduction to active web first commit * BAEL-1275 Added the module to parent pom * BAEL-1275 Introduction to ActiveWeb
1 parent 2399b4d commit d7aecc8

18 files changed

Lines changed: 277 additions & 11 deletions

File tree

java-lite/pom.xml

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
54
<modelVersion>4.0.0</modelVersion>
65

76
<groupId>org.baeldung</groupId>
@@ -22,10 +21,15 @@
2221
<mysql.connector.java.version>5.1.45</mysql.connector.java.version>
2322
<sun.tools.version>1.7.0</sun.tools.version>
2423
<jackson.version>1.8.2</jackson.version>
25-
<junit.version>4.11</junit.version>
24+
<junit.version>4.12</junit.version>
2625
</properties>
2726

2827
<build>
28+
<resources>
29+
<resource>
30+
<directory>src/main/webapp/WEB-INF</directory>
31+
</resource>
32+
</resources>
2933
<plugins>
3034
<plugin>
3135
<groupId>org.eclipse.jetty</groupId>
@@ -85,7 +89,14 @@
8589
<scope>system</scope>
8690
<systemPath>${java.home}/../lib/tools.jar</systemPath>
8791
</dependency>
88-
92+
93+
<dependency>
94+
<groupId>org.javalite</groupId>
95+
<artifactId>activeweb-testing</artifactId>
96+
<version>1.15</version>
97+
<scope>test</scope>
98+
</dependency>
99+
89100
<dependency>
90101
<groupId>junit</groupId>
91102
<artifactId>junit</artifactId>

java-lite/src/main/java/app/config/AppBootstrap.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33
import org.javalite.activeweb.AppContext;
44
import org.javalite.activeweb.Bootstrap;
55

6+
import com.google.inject.Guice;
7+
import com.google.inject.Injector;
8+
9+
import app.services.ArticleServiceModule;
10+
611
public class AppBootstrap extends Bootstrap {
712
public void init(AppContext context) {
813
}
14+
public Injector getInjector() {
15+
return Guice.createInjector(new ArticleServiceModule());
16+
}
917
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package app.controllers;
2+
3+
import javax.inject.Inject;
4+
5+
import org.javalite.activeweb.AppController;
6+
7+
import app.services.ArticleService;
8+
9+
public class ArticleController extends AppController {
10+
11+
@Inject
12+
private ArticleService articleService;
13+
14+
public void index() {
15+
view("articles", articleService.getArticles());
16+
}
17+
18+
public void search() {
19+
20+
String keyword = param("key");
21+
if (null != keyword) {
22+
assign("article", articleService.search(keyword));
23+
} else {
24+
render("/common/error");
25+
}
26+
27+
}
28+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package app.controllers;
2+
3+
import org.javalite.activeweb.AppController;
4+
5+
public class HomeController extends AppController {
6+
7+
public void index() {
8+
render("index");
9+
}
10+
11+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package app.models;
2+
3+
public class Article {
4+
5+
private String title;
6+
private String author;
7+
private String words;
8+
private String date;
9+
10+
public Article(String title, String author, String words, String date) {
11+
super();
12+
this.title = title;
13+
this.author = author;
14+
this.words = words;
15+
this.date = date;
16+
}
17+
18+
public String getTitle() {
19+
return title;
20+
}
21+
22+
public void setTitle(String title) {
23+
this.title = title;
24+
}
25+
26+
public String getAuthor() {
27+
return author;
28+
}
29+
30+
public void setAuthor(String author) {
31+
this.author = author;
32+
}
33+
34+
public String getWords() {
35+
return words;
36+
}
37+
38+
public void setWords(String words) {
39+
this.words = words;
40+
}
41+
42+
public String getDate() {
43+
return date;
44+
}
45+
46+
public void setDate(String date) {
47+
this.date = date;
48+
}
49+
50+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package app.services;
2+
3+
import java.util.List;
4+
5+
import app.models.Article;
6+
7+
public interface ArticleService {
8+
9+
List<Article> getArticles();
10+
11+
Article search(String keyword);
12+
13+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package app.services;
2+
3+
import java.time.Instant;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
import app.models.Article;
8+
9+
public class ArticleServiceImpl implements ArticleService {
10+
11+
public List<Article> getArticles() {
12+
return fetchArticles();
13+
}
14+
15+
public Article search(String keyword) {
16+
Article ar = new Article("Article with " + keyword, "baeldung", "1250", Instant.now()
17+
.toString());
18+
return ar;
19+
}
20+
21+
private List<Article> fetchArticles() {
22+
Article ar1 = new Article("Introduction to ActiveWeb", "baeldung", "1650", Instant.now()
23+
.toString());
24+
25+
Article ar = new Article("Introduction to Mule", "baeldung", "1650", Instant.now()
26+
.toString());
27+
List<Article> articles = new ArrayList<Article>();
28+
articles.add(ar);
29+
articles.add(ar1);
30+
return articles;
31+
32+
}
33+
34+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package app.services;
2+
3+
import com.google.inject.AbstractModule;
4+
5+
public class ArticleServiceModule extends AbstractModule {
6+
7+
@Override
8+
protected void configure() {
9+
bind(ArticleService.class).to(ArticleServiceImpl.class)
10+
.asEagerSingleton();
11+
}
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<@content for="title">Articles</@content>
2+
3+
<table>
4+
<tr>
5+
<td>Title</td>
6+
<td>Author</td>
7+
<td>Words #</td>
8+
<td>Date Published</td>
9+
</tr>
10+
<#list articles as article>
11+
<tr>
12+
<td>${article.title}</td>
13+
<td>${article.author}</td>
14+
<td>${article.words}</td>
15+
<td>${article.date}</td>
16+
</tr>
17+
</#list>
18+
</table>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<@content for="title">Search</@content>
2+
3+
<table>
4+
<tr>
5+
<td>Title</td>
6+
<td>Author</td>
7+
<td>Words #</td>
8+
<td>Date Published</td>
9+
</tr>
10+
<tr>
11+
<td>${article.title}</td>
12+
<td>${article.author}</td>
13+
<td>${article.words}</td>
14+
<td>${article.date}</td>
15+
</tr>
16+
17+
</table>

0 commit comments

Comments
 (0)