Skip to content

Commit 4576a1e

Browse files
christian-raedelslavisa-baeldung
authored andcommitted
BAEL-325: Refactored to use multi-module structure.
1 parent 2d678d6 commit 4576a1e

54 files changed

Lines changed: 433 additions & 114 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎patterns/front-controller/pom.xml‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?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">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<artifactId>front-controller</artifactId>
8+
<packaging>war</packaging>
9+
10+
<parent>
11+
<artifactId>patterns-parent</artifactId>
12+
<groupId>com.baeldung.patterns</groupId>
13+
<version>1.0.0-SNAPSHOT</version>
14+
<relativePath>..</relativePath>
15+
</parent>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>javax.servlet</groupId>
20+
<artifactId>javax.servlet-api</artifactId>
21+
</dependency>
22+
</dependencies>
23+
24+
<build>
25+
<plugins>
26+
<plugin>
27+
<groupId>org.apache.maven.plugins</groupId>
28+
<artifactId>maven-compiler-plugin</artifactId>
29+
</plugin>
30+
<plugin>
31+
<groupId>org.apache.maven.plugins</groupId>
32+
<artifactId>maven-war-plugin</artifactId>
33+
</plugin>
34+
<plugin>
35+
<groupId>org.eclipse.jetty</groupId>
36+
<artifactId>jetty-maven-plugin</artifactId>
37+
<configuration>
38+
<webApp>
39+
<contextPath>/front-controller</contextPath>
40+
</webApp>
41+
</configuration>
42+
</plugin>
43+
</plugins>
44+
</build>
45+
</project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.patterns.front.controller;
2+
3+
import com.baeldung.patterns.front.controller.commands.FrontCommand;
4+
import com.baeldung.patterns.front.controller.commands.UnknownCommand;
5+
6+
import javax.servlet.ServletException;
7+
import javax.servlet.http.HttpServlet;
8+
import javax.servlet.http.HttpServletRequest;
9+
import javax.servlet.http.HttpServletResponse;
10+
import java.io.IOException;
11+
12+
public class FrontControllerServlet extends HttpServlet {
13+
@Override
14+
protected void doGet(
15+
HttpServletRequest request,
16+
HttpServletResponse response
17+
) throws ServletException, IOException {
18+
FrontCommand command = getCommand(request);
19+
command.init(getServletContext(), request, response);
20+
command.process();
21+
}
22+
23+
private FrontCommand getCommand(HttpServletRequest request) {
24+
try {
25+
Class type = Class.forName(
26+
String.format(
27+
"com.baeldung.patterns.front.controller.commands.%sCommand",
28+
request.getParameter("command")
29+
)
30+
);
31+
return (FrontCommand) type
32+
.asSubclass(FrontCommand.class)
33+
.newInstance();
34+
} catch (Exception e) {
35+
return new UnknownCommand();
36+
}
37+
}
38+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.baeldung.patterns.front.controller.commands;
2+
3+
import javax.servlet.RequestDispatcher;
4+
import javax.servlet.ServletContext;
5+
import javax.servlet.ServletException;
6+
import javax.servlet.http.HttpServletRequest;
7+
import javax.servlet.http.HttpServletResponse;
8+
import java.io.IOException;
9+
10+
public abstract class FrontCommand {
11+
protected ServletContext context;
12+
protected HttpServletRequest request;
13+
protected HttpServletResponse response;
14+
15+
public void init(
16+
ServletContext servletContext,
17+
HttpServletRequest servletRequest,
18+
HttpServletResponse servletResponse
19+
) {
20+
this.context = servletContext;
21+
this.request = servletRequest;
22+
this.response = servletResponse;
23+
}
24+
25+
public abstract void process() throws ServletException, IOException;
26+
27+
protected void forward(String target) throws ServletException, IOException {
28+
target = String.format("/WEB-INF/jsp/%s.jsp", target);
29+
RequestDispatcher dispatcher = context.getRequestDispatcher(target);
30+
dispatcher.forward(request, response);
31+
}
32+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.patterns.front.controller.commands;
2+
3+
import com.baeldung.patterns.front.controller.data.Book;
4+
import com.baeldung.patterns.front.controller.data.BookshelfImpl;
5+
6+
import javax.servlet.ServletException;
7+
import java.io.IOException;
8+
9+
public class SearchCommand extends FrontCommand {
10+
@Override
11+
public void process() throws ServletException, IOException {
12+
Book book = new BookshelfImpl().getInstance()
13+
.findByTitle(request.getParameter("title"));
14+
if (book != null) {
15+
request.setAttribute("book", book);
16+
forward("book-found");
17+
} else {
18+
forward("book-notfound");
19+
}
20+
}
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.patterns.front.controller.commands;
2+
3+
import javax.servlet.ServletException;
4+
import java.io.IOException;
5+
6+
public class UnknownCommand extends FrontCommand {
7+
@Override
8+
public void process() throws ServletException, IOException {
9+
forward("unknown");
10+
}
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.patterns.front.controller.data;
2+
3+
public interface Book {
4+
String getAuthor();
5+
6+
void setAuthor(String author);
7+
8+
String getTitle();
9+
10+
void setTitle(String title);
11+
12+
Double getPrice();
13+
14+
void setPrice(Double price);
15+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.patterns.front.controller.data;
2+
3+
public class BookImpl implements Book {
4+
private String author;
5+
private String title;
6+
private Double price;
7+
8+
public BookImpl() {
9+
}
10+
11+
public BookImpl(String author, String title, Double price) {
12+
this.author = author;
13+
this.title = title;
14+
this.price = price;
15+
}
16+
17+
@Override
18+
public String getAuthor() {
19+
return author;
20+
}
21+
22+
@Override
23+
public void setAuthor(String author) {
24+
this.author = author;
25+
}
26+
27+
@Override
28+
public String getTitle() {
29+
return title;
30+
}
31+
32+
@Override
33+
public void setTitle(String title) {
34+
this.title = title;
35+
}
36+
37+
@Override
38+
public Double getPrice() {
39+
return price;
40+
}
41+
42+
@Override
43+
public void setPrice(Double price) {
44+
this.price = price;
45+
}
46+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.patterns.front.controller.data;
2+
3+
public interface Bookshelf {
4+
5+
default void init() {
6+
add(new BookImpl("Wilson, Robert Anton & Shea, Robert", "Illuminati", 9.99));
7+
add(new BookImpl("Fowler, Martin", "Patterns of Enterprise Application Architecture", 27.88));
8+
}
9+
10+
Bookshelf getInstance();
11+
12+
<E extends Book> boolean add(E book);
13+
14+
Book findByTitle(String title);
15+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.patterns.front.controller.data;
2+
3+
import java.util.ArrayList;
4+
5+
public class BookshelfImpl extends ArrayList<Book> implements Bookshelf {
6+
private static Bookshelf INSTANCE;
7+
8+
@Override
9+
public Bookshelf getInstance() {
10+
if (INSTANCE == null) {
11+
INSTANCE = new BookshelfImpl();
12+
INSTANCE.init();
13+
}
14+
return INSTANCE;
15+
}
16+
17+
@Override
18+
public Book findByTitle(String title) {
19+
return this.stream()
20+
.filter(book -> book.getTitle().toLowerCase().contains(title.toLowerCase()))
21+
.findFirst()
22+
.orElse(null);
23+
}
24+
}
9.27 KB
Loading

0 commit comments

Comments
 (0)