3层架构中的表示层

时间:2013-06-10 22:26:37

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

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

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

我想知道,如果表示层将是一个带有自己的GUI的独立应用程序,而不是基于浏览器的

,表示层将如何与逻辑层进行通信

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

1 个答案:

答案 0 :(得分:3)

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

  • 处理视图的文件(JSP,Facelets等)。
  • 控制用户和视图之间交互的文件(来自Spring MVC的Servlets,@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)

来自评论:

  

还不清楚我的应用程序的业务逻辑层将如何与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服务的实现者)

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

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

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
    }
}

正如您所注意到的,在将演示文稿与业务逻辑层进行通信时,没有 magic 。当然,这个骨架可以通过使用其他技术来增强,但它只是为了说明主要思想。

注意:Web服务的框架是从Creating a Simple Web Service and Client with JAX-WS改编而来的。

相关问题