我的问题是关于在三层架构中实现表示层的各种方法
当我们谈论 3 层 Web 应用程序时,假设表示层是面向浏览器的,因此通过 HTTP 协议(protocol)与逻辑层进行通信
我想知道,如果表示层将成为一个具有自己的 GUI 的独立应用程序,而不是基于浏览器的应用程序,那么表示层将如何与逻辑层进行通信
例如,Java servlet 从浏览器获取 HTTP 请求,但是如果我想设计一个特定的桌面应用程序来与 servlet 通信怎么办?我的应用程序将如何与逻辑层通信?使用哪种协议(protocol)?
最佳答案
我猜你误解了这些问题。在这种情况下,您可以说表示层分为 2 个小层:
- 处理 View 的文件(JSP、Facelets 等)。
- 控制用户和 View 之间交互的文件(Servlet、Spring MVC 中的
@Controller
、JSF 中的@ManagedBean
等)。
除此之外,您还可以拥有业务逻辑层(通常是服务类)和数据访问层(DAO 或任何您认为更好的称呼)。
如果您从创建 GUI 桌面应用程序的角度来看,您的演示文稿将具有类似的结构:
- 处理 View 的类
- 处理用户和 View 之间交互的 Controller 类。
在这种情况下,这些类通常相同,但请注意,它们用于演示目的并且应该与您的业务逻辑层。
what about if I want to design a specific desktop application to communicate with servlets?
您可能指的是使用 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)
来自评论:
Still it's unclear how business logic layer of my application is going to communicate with web service layer.
发布一个来自将使用 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
}
}
正如您所注意到的,将表示与业务逻辑层进行通信并没有什么魔法。当然,这个框架可以通过使用其他技术来增强,但这只是为了说明主要思想。
注意:Web 服务的框架改编自此处 Creating a Simple Web Service and Client with JAX-WS .
关于java - 三层架构中的表示层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17033829/