写点什么

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

  • 2022 年 4 月 29 日
  • 本文字数:2098 字

    阅读完需:约 7 分钟

[](()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") == nu 《一线大厂 Java 面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 ll)


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 {


...


}


On the urlPattern, the value is “/myapp/_". As previously shown in the preceding code


在 urlPattern 上,值为“/myapp/_”。如前面的代码所示


snippet, this URL pattern (”/myapp/_") establishes that all requests to the myapp URI are


这个 URL 模式(“/myapp/_”)确定对 myapp URI 的所有请求都是


sent to our controller. For example, http://ip:port/context/myapp/myfuncionality


发送给我们的控制器。例如,http://ip:端口/context/myapp/myfunctionality


is sent to our controller.


发送到我们的控制器。


When we implement this pattern, it is very important to pay attention to the use of


当我们实现这个模式时,注意使用


attributes on servlets, because all the class attributes on a servlet are shared with all threads


servlet 上的属性,因为 servlet 上的所有类属性都与所有线程共享


or all requests.


或所有请求。


All GET requests or POST requests are sent to the processRequest method, which


所有 GET 请求或 POST 请求都被发送到 processRequest 方法,该方法


implements the logic to send the request to the respective command and executes the


实现将请求发送到相应命令的逻辑,并执行


respective logic. After the correct command is set, the respective command is executed and


各自的逻辑。设置正确的命令后,执行相应的命令,并


the page is dispatched. Here, we have the line that executes the command and dispatches


页面已发送。在这里,我们有一个执行命令和分派的行


the request to the correct page:


对正确页面的请求:



//Execute a Command resultPage = command.execute();



Dispatching the request to the corresponding page: //Dispatch to correspondent page. getServletContext().getRequestDispatcher(resultPage) .forward(request, response);

[](()Implementing the commands

Here, we have AbstractCommand, which is an abstract class with one execute method. This is the abstract command, and the execute method is implemented on the subclasses:


这里,我们有 AbstractCommand,它是一个带有一个 execute 方法的抽象类。这是抽象命令,execute 方法在子类上实现:

用户头像

还未添加个人签名 2022.04.13 加入

还未添加个人简介

评论

发布
暂无评论
Java8设计模式最佳实战-设计模式概述(第七天学习记录)_Java_爱好编程进阶_InfoQ写作社区