我猜你误解了这些问题。您可以说在这种情况下,表示层分为 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。