BAEL-305 - front controller pattern source
This commit is contained in:
parent
4c561c3e2e
commit
3d7ccfd56d
|
@ -0,0 +1,43 @@
|
|||
<?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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>front-controller-pattern</artifactId>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
<artifactId>enterprise-patterns-parent</artifactId>
|
||||
<groupId>com.baeldung.enterprise.patterns</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>3.1.0</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-maven-plugin</artifactId>
|
||||
<version>9.4.0.M1</version>
|
||||
<configuration>
|
||||
<webApp>
|
||||
<contextPath>/front-controller</contextPath>
|
||||
</webApp>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,45 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller;
|
||||
|
||||
import com.baeldung.enterprise.patterns.front.controller.commands.FrontCommand;
|
||||
import com.baeldung.enterprise.patterns.front.controller.commands.UnknownCommand;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
public class FrontControllerServlet extends HttpServlet {
|
||||
@Override
|
||||
protected void doGet(
|
||||
HttpServletRequest request,
|
||||
HttpServletResponse response
|
||||
) throws ServletException, IOException {
|
||||
FrontCommand command = getCommand(request);
|
||||
command.init(getServletContext(), request, response);
|
||||
command.process();
|
||||
}
|
||||
|
||||
private FrontCommand getCommand(HttpServletRequest request) {
|
||||
try {
|
||||
return (FrontCommand) getCommandClass(request)
|
||||
.asSubclass(FrontCommand.class)
|
||||
.newInstance();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to get command!", e);
|
||||
}
|
||||
}
|
||||
|
||||
private Class getCommandClass(HttpServletRequest request) {
|
||||
try {
|
||||
return Class.forName(
|
||||
String.format(
|
||||
"com.baeldung.enterprise.patterns.front.controller.commands.%sCommand",
|
||||
request.getParameter("command")
|
||||
)
|
||||
);
|
||||
} catch (ClassNotFoundException e) {
|
||||
return UnknownCommand.class;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.commands;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
public abstract class FrontCommand {
|
||||
protected ServletContext context;
|
||||
protected HttpServletRequest request;
|
||||
protected HttpServletResponse response;
|
||||
|
||||
public void init(
|
||||
ServletContext servletContext,
|
||||
HttpServletRequest servletRequest,
|
||||
HttpServletResponse servletResponse
|
||||
) {
|
||||
this.context = servletContext;
|
||||
this.request = servletRequest;
|
||||
this.response = servletResponse;
|
||||
}
|
||||
|
||||
public abstract void process() throws ServletException, IOException;
|
||||
|
||||
protected void forward(String target) throws ServletException, IOException {
|
||||
target = String.format("/WEB-INF/jsp/%s.jsp", target);
|
||||
RequestDispatcher dispatcher = context.getRequestDispatcher(target);
|
||||
dispatcher.forward(request, response);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.commands;
|
||||
|
||||
import com.baeldung.enterprise.patterns.front.controller.data.Book;
|
||||
import com.baeldung.enterprise.patterns.front.controller.data.BookshelfImpl;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import java.io.IOException;
|
||||
|
||||
public class SearchCommand extends FrontCommand {
|
||||
@Override
|
||||
public void process() throws ServletException, IOException {
|
||||
Book book = new BookshelfImpl().getInstance()
|
||||
.findByTitle(request.getParameter("title"));
|
||||
if (book != null) {
|
||||
request.setAttribute("book", book);
|
||||
forward("book-found");
|
||||
} else {
|
||||
forward("book-notfound");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.commands;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import java.io.IOException;
|
||||
|
||||
public class UnknownCommand extends FrontCommand {
|
||||
@Override
|
||||
public void process() throws ServletException, IOException {
|
||||
forward("unknown");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.data;
|
||||
|
||||
public class Book {
|
||||
private String author;
|
||||
private String title;
|
||||
private Double price;
|
||||
|
||||
public Book() {
|
||||
}
|
||||
|
||||
public Book(String author, String title, Double price) {
|
||||
this.author = author;
|
||||
this.title = title;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.data;
|
||||
|
||||
public interface Bookshelf {
|
||||
|
||||
default void init() {
|
||||
add(new Book("Wilson, Robert Anton & Shea, Robert", "Illuminati", 9.99));
|
||||
add(new Book("Fowler, Martin", "Patterns of Enterprise Application Architecture", 27.88));
|
||||
}
|
||||
|
||||
Bookshelf getInstance();
|
||||
|
||||
<E extends Book> boolean add(E book);
|
||||
|
||||
Book findByTitle(String title);
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.enterprise.patterns.front.controller.data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class BookshelfImpl extends ArrayList<Book> implements Bookshelf {
|
||||
private static Bookshelf INSTANCE;
|
||||
|
||||
@Override
|
||||
public Bookshelf getInstance() {
|
||||
if (INSTANCE == null) {
|
||||
INSTANCE = new BookshelfImpl();
|
||||
INSTANCE.init();
|
||||
}
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Book findByTitle(String title) {
|
||||
return this.stream()
|
||||
.filter(book -> book.getTitle().toLowerCase().contains(title.toLowerCase()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Bookshelf: Title found</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>Our Bookshelf contains this title:</p>
|
||||
<h2>${book.getTitle()}</h2>
|
||||
<p>Author: ${book.getAuthor()}</p>
|
||||
<input type="submit" value="Buy it: ${book.getPrice()}$">
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,10 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Bookshelf: Title not found</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>Our Bookshelf doesn't contains this title:</p>
|
||||
<h2>${param.get("title")}</h2>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,9 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Bookshelf: Command unknown</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>Sorry, this command is not known!</p>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,15 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
|
||||
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
|
||||
version="3.1">
|
||||
<servlet>
|
||||
<servlet-name>front-controller</servlet-name>
|
||||
<servlet-class>com.baeldung.enterprise.patterns.front.controller.FrontControllerServlet</servlet-class>
|
||||
</servlet>
|
||||
<servlet-mapping>
|
||||
<servlet-name>front-controller</servlet-name>
|
||||
<url-pattern>/</url-pattern>
|
||||
</servlet-mapping>
|
||||
</web-app>
|
|
@ -0,0 +1,35 @@
|
|||
<?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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.baeldung.enterprise.patterns</groupId>
|
||||
<artifactId>enterprise-patterns-parent</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>front-controller-pattern</module>
|
||||
</modules>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.5.1</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
</project>
|
Loading…
Reference in New Issue