2

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

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

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

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

4

1 回答 1

3

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

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

除此之外,您还可以拥有业务逻辑层(通常是服务类)和数据访问层(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)

来自评论:

仍然不清楚我的应用程序的业务逻辑层将如何与 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 服务的框架改编自这里使用 JAX-WS 创建简单的 Web 服务和客户端

于 2013-06-10T22:38:35.837 回答