【问题标题】:Presentation layer in 3-tier architecture三层架构中的表示层
【发布时间】:2013-06-06 16:42:16
【问题描述】:

我的问题是关于在 3 层架构中实现表示层的各种方法

当我们谈论 3 层 Web 应用程序时,假设表示层是面向浏览器的,因此通过 HTTP 协议与逻辑层通信

我想知道表示层将如何与逻辑层通信,以防表示层将成为具有自己的 GUI 的独立应用程序,而不是基于浏览器的应用程序

例如,Java servlet 从我们的浏览器获取 HTTP 请求,但是如果我想设计一个特定的桌面应用程序来与 servlet 通信呢?我的应用程序将如何与逻辑层通信?使用哪种协议?

【问题讨论】:

    标签: java servlets client-server 3-tier server-side-scripting


    【解决方案1】:

    我猜你误解了这些问题。您可以说在这种情况下,表示层分为 2 个小层:

    • 处理视图的文件(JSP、Facelets 等)。
    • 控制用户与视图之间交互的文件(Servlet、来自 Spring MVC 的 @Controller、来自 JSF 的 @ManagedBean 等)。

    除此之外,您还可以拥有业务逻辑层(通常是服务类)和数据访问层(DAO 或任何您觉得更好的称呼)。

    如果从创建 GUI 桌面应用程序的角度来看,您的演示文稿将具有类似的结构:

    • 处理视图的类
    • 处理用户和视图之间交互的控制器类。

    在这种情况下,这些类通常相同,但请注意,它们是为了演示目的并且应该与您的业​​务逻辑层。

    如果我想设计一个特定的桌面应用程序来与 servlet 通信怎么办?

    您可能是指使用 Web 服务的客户端应用程序。 Web 服务(由 XML、JSON 或纯文本使用)可以是服务层的一部分,应该在业务逻辑层或应用程序的表示层中使用,具体取决于 Web 服务返回的内容。不过,我会发现更好地从业务逻辑层使用 Web 服务层并让表示层处理其目的:仅表示逻辑

    举个例子:

    Presentation layer
          |
          | <<communicates>>
          |
          v
    Business logic layer
          |
          | <<communicates>>
          |
          v
    Web Service Layer
          |
    ( the cloud ) <<communicates data using XML, JSON, etc...>>
          |
          v
    Web Server that resolves the Web Service call
          |
          | <<communicates>>
          |
          v
    WS Business logic layer
          |
          | <<communicates>>
          |
          v
    WS Data access layer
          |
          | <<communicates>>
          |
          v
    Data Sources (database, files, etc)
    

    来自 cmets:

    仍然不清楚我的应用程序的业务逻辑层将如何与 Web 服务层进行通信。

    从将使用 Web 服务的 Web 应用程序项目中发布一个非常简单的骨架示例。

    Servlet 类(改编自 StackOverflow Servlet wiki)(部分演示文稿)

    @WebServlet("/hello")
    public class AServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
            //Displaying the view.
            request.getRequestDispatcher("/WEB-INF/hello.jsp").forward(request, response);
        }
    
        @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        Person person = new Person(name, Integer.parseInt(age));
        PersonBL personBL = new PersonBL();
        personBL.savePerson(person);
    }
    

    PersonBL 类(业务逻辑层的一部分)

    public class PersonBL {
        //omitting configurations and all non-code related stuff for explanation purposes
        @WebServiceRef(wsdlLocation="http://localhost:8080/helloservice/hello?wsdl")
        private static PersonWebService personWS;
    
        public void savePerson(Person person) {
            //since is business logic layer, it can hold validations for the data
            //before calling the web service
            //for explanation purposes, no business logic will be added
            //...
            //here it will contain the logic to call the web service
            PersonPort personPort = personWS.getPersonPort();
            personPort.savePerson(person);
        }
    }
    

    现在,发布 Web 服务的框架:

    PersonPort 类(Web Service 的实现者)

    @WebService
    public class PersonPort {
        @WebMethod
        public void savePerson(Person person) {
            PersonWSBL personWSBL = new PersonWSBL();
            personWSBL.savePerson(person);
        }
    }
    

    PersonWSBL 类(Web Service 中的业务逻辑层)

    public class PersonWSBL {
        public void savePerson(Person person) {
             //it can contain business rules to apply before executing the operation
             //for example purposes, there won't be any rules to apply
             //...
             PersonDAO personDAO = new PersonDAO();
             personDAO.savePerson(person);
        }
    }
    

    PersonDAO 类(数据访问层)

    public class PersonDAO {
        public void savePerson(Person person) {
            //logic to save the person in database or somewhere else
        }
    }
    

    如您所见,将表示与业务逻辑层进行通信并没有魔法。当然,这个骨架可以通过使用其他技术来增强,但这只是为了说明主要思想。

    注意:Web Service 的骨架改编自这里Creating a Simple Web Service and Client with JAX-WS

    【讨论】:

    • 感谢您的回复。我想我需要上述方案中表示层和业务逻辑层之间的通信方式。这是如何实现的?
    • @mangusta 这取决于您的应用程序设计。您可以将此场景视为两个应用程序:第一个充当表示应用程序,将具有表示层和它自己的业务逻辑层(GUI、Web 应用程序、移动应用程序,只要它的目的并不重要)是演示文稿)。然后,您将从此应用程序的业务逻辑层与 Web 服务应用程序进行通信。请注意,表示层和业务逻辑层之间的通信将取决于您选择的技术。
    • 好的,我明白了。仍然不清楚我的应用程序的业务逻辑层将如何与 Web 服务层进行通信。你的意思是,即使我们要使用桌面应用程序,我们也会通过 HTTP 与服务器部分进行通信?
    • @mangusta 答案已更新,显示了建议示例的骨架。
    • :) 所以你假设 servlet 是应用程序表示层的一部分,尽管它有点误导,因为 servlet 位于服务器上,而不是用户机器上:)
    猜你喜欢
    • 1970-01-01
    • 2017-05-17
    • 2023-03-25
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    相关资源
    最近更新 更多