Java8设计模式最佳实战-设计模式概述(第七天学习记录)

Explaining the FrontController pattern


In the Java EE world, we commonly work with complex projects that have similar

在javaee世界中,我们通常使用具有相似特性的复杂项目

functionalities and processes. Sometimes, using various controllers to handle a request is a

功能和流程。有时,使用不同的控制器来处理请求是

bad practice because it needs to be configured at multiple endpoints and incurs a large cost

错误的做法,因为它需要在多个端点进行配置,并且会产生很大的成本

of creation and maintenance. Consequently, creating a central point to treat a request is a

创建和维护。因此,创建一个处理请求的中心点是

very good solution, as it creates one point to manage all or a group of requests and then

非常好的解决方案,因为它创建一个点来管理所有或一组请求,然后

sends this request to the correct process. We can then treat all points that are common to all

将此请求发送到正确的进程。然后我们可以处理所有共同点

functionalities and send the request to a process to treat the questions that are not common

功能,并将请求发送到流程以处理不常见的问题

to all but are specific to one functionality. Some configurations, such as session

只有一个功能是特定的。一些配置,如会话

configuration, the maximum size limit of a request, cookie, and header, are common to all

配置,即请求、cookie和头的最大大小限制,对所有人都是通用的

requests and can be configured from a central point.

请求并可以从中心点进行配置。

The FrontController pattern is a pattern that creates a central manager to treat all

FrontController模式是一种创建中央管理器来处理所有问题的模式

requests or a request group of an application and then sends the requests to one specific

请求或应用程序的请求组,然后将请求发送到一个特定的

process, which is generally a command. This pattern is rarely used on common projects

进程,通常是一个命令。这种模式很少用于普通项目

because today we have some ready-made solutions, and implementing this pattern is

因为今天我们有一些现成的解决方案,而实现这种模式是

generally unnecessary. This pattern is used by frameworks such as JSF, Spring MVC, and

一般不需要。这个模式被JSF、springmvc和

struts. The following diagram depicts this pattern:

支持。下图描述了这种模式:

在这里插入图片描述

In the preceding diagram, we have FrontController, AbstractCommand, Command1, and

在前面的图表中,我们有FrontController、AbstractCommand、Command1和

Command2. FrontController receives all requests, treats some common points of the

命令2。FrontController接收所有请求,处理

request, and sends this request to the matching command. AbstractCommand is the

请求,并将此请求发送到匹配的命令。AbstractCommand是

abstract class of command. Command1 and Command2 are the subclasses of command,

命令的抽象类。Command1和Command2是command的子类,

which implement its correspondent logic.

实现了相应的逻辑。

In our case, we will have two pages—a homepage and a login page. If the user is logged in

在我们的例子中,我们将有两个页面-一个主页和一个登录页面。如果用户已登录

at the moment that a request is sent, then the application will launch the login page, and

在发送请求时,应用程序将启动登录页面,并且

then the homepage.

然后是主页。

Implementing FrontController

Here, we have an implementation of MyAppController, which is a FrontController to treat all the requests of an application:

package com.gary.book.chapter01;

import com.rhuan.action.Command.AbstractCommand;

import com.rhuan.action.Command.HomeCommand;

import com.rhuan.action.Command.LoginCommand;

import org.apache.logging.log4j.LogManager;

import org.apache.logging.log4j.Logger;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet(name = “MyAppController”, urlPatterns = “/myapp/*”)

public class MyAppController extends HttpServlet {

private static Logger logger =

LogManager.getLogger(MyAppController.class);

private final String PAGE_ERROR = “/pageError.jsp”;

protected void doPost(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

processRequest(request, response);

}

protected void doGet(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

processRequest(request, response);

}

protected void processRequest(HttpServletRequest

request, HttpServletResponse response)

throws ServletException, java.io.IOException {

String resultPage;

AbstractCommand command = null;

try {

//Create a correspondent Command.

if (request.getSession().getAttribute(“USER”) == null)

command = new LoginCommand();

else command = new HomeCommand();

//Execute the Command that return a page.

resultPage = command.execute();

} catch (Exception e) {

logger.error(e.getMessage());

resultPage = PAGE_ERROR;

}

//Dispatch to correspondent page.

getServletContext().getRequestDispatcher(resultPage)

.forward(request, response);

}

}

In the following code snippet, it is very important to note that urlPattern is used to

在下面的代码片段中,请注意urlPattern用于

define which requests a context will send to our controller. Here’s how we do this:

定义上下文将发送给控制器的请求。我们的方法如下:

//Defining the urlPattern to Front Controller

@WebServlet(name = “MyAppController”, urlPatterns = “/myapp/*”)

public class MyAppController extends HttpServlet {

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!**

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

  • 8
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值