使用 Servlet 和 JSP 的 MVC 示例

1.概述

在这篇快速文章中,我们将使用基本的 Servlet 和 JSP 创建一个实现模型视图控制器 (MVC) 设计模式的小型 Web 应用程序。

在我们继续实现之前,我们将探讨一下 MVC 的工作原理及其关键特性。

2.MVC简介

模型-视图-控制器 (MVC) 是软件工程中用于将应用程序逻辑与用户界面分离的模式。顾名思义,MVC 模式分为三层。

Model 定义应用程序的业务层,Controller 管理应用程序的流程,View 定义应用程序的表示层。

尽管 MVC 模式并非特定于 Web 应用程序,但它非常适合这种类型的应用程序。在 Java 上下文中,模型由简单的 Java 类组成,控制器由 servlet 组成,视图由 JSP 页面组成。

以下是该模式的一些关键特征:

  • 它将表示层与业务层分开
  • Controller 执行调用 Model 并向 View 发送数据的动作
  • 该模型甚至不知道它被某些 Web 应用程序或桌面应用程序使用

让我们来看看每一层。

2.1。模型层

这是包含系统业务逻辑的数据层,也代表了应用程序的状态。

它独立于表示层,控制器从模型层获取数据并将其发送到视图层。

2.2. 控制器层

控制器层充当视图和模型之间的接口。它接收来自视图层的请求并处理它们,包括必要的验证。

请求进一步发送到 Model 层进行数据处理,处理完毕后,将数据发送回 Controller,然后显示在 View 上。

2.3. 视图层

该层代表应用程序的输出,通常是某种形式的 UI。表示层用于显示 Controller 获取的 Model 数据。

3. MVC 与 Servlet 和 JSP

为了实现基于 MVC 设计模式的 Web 应用程序,我们将创建StudentStudentService类——它们将充当我们的模型层。

tudentServlet类将充当控制器,对于表示层,我们将创建student-record.jsp页面。

现在,让我们一层一层地编写这些层,并从Student类开始:

public class Student {
    private int id;
    private String firstName;
    private String lastName;
	
    // constructors, getters and setters goes here
}

现在让我们编写我们的StudentService来处理我们的业务逻辑:

public class StudentService {

    public Optional<Student> getStudent(int id) {
        switch (id) {
            case 1:
                return Optional.of(new Student(1, "John", "Doe"));
            case 2:
                return Optional.of(new Student(2, "Jane", "Goodall"));
            case 3:
                return Optional.of(new Student(3, "Max", "Born"));
            default:
                return Optional.empty();
        }
    }
}

现在让我们创建我们的控制器类StudentServlet

@WebServlet(
  name = "StudentServlet", 
  urlPatterns = "/student-record")
public class StudentServlet extends HttpServlet {

    private StudentService studentService = new StudentService();

    private void processRequest(
      HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException {

        String studentID = request.getParameter("id");
        if (studentID != null) {
            int id = Integer.parseInt(studentID);
            studentService.getStudent(id)
              .ifPresent(s -> request.setAttribute("studentRecord", s));
        }

        RequestDispatcher dispatcher = request.getRequestDispatcher(
          "/WEB-INF/jsp/student-record.jsp");
        dispatcher.forward(request, response);
    }

    @Override
    protected void doGet(
      HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException {

        processRequest(request, response);
    }

    @Override
    protected void doPost(
      HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException {

        processRequest(request, response);
    }
}

这个 servlet 是我们 Web 应用程序的控制器。

首先,它从请求中读取参数id 。如果提交了id ,则从业务层获取一个Student对象。

一旦从模型中检索到必要的数据,它就会使用setAttribute()方法将此数据放入请求中。

最后,控制器请求和响应对象转发给 JSP,即应用程序的视图。

接下来,让我们编写我们的表示层student-record.jsp

<html>
    <head>
        <title>Student Record</title>
    </head>
    <body>
    <% 
        if (request.getAttribute("studentRecord") != null) {
            Student student = (Student) request.getAttribute("studentRecord");
    %>
 
    <h1>Student Record</h1>
    <div>ID: <%= student.getId()%></div>
    <div>First Name: <%= student.getFirstName()%></div>
    <div>Last Name: <%= student.getLastName()%></div>
        
    <% 
        } else { 
    %>

    <h1>No student record found.</h1>
         
    <% } %>	
    </body>
</html>

当然,JSP 是应用程序的视图;它从控制器接收它需要的所有信息,它不需要直接与业务层交互。

4。结论

在本教程中,我们了解了 MVC 即模型视图控制器架构,并且我们专注于如何实现一个简单的示例。

像往常一样,这里提供的代码可以在 GitHub 上找到

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值