MVC(Model-View-Controller,模型 - 视图 - 控制器)是一种设计模式,用于将应用程序的业务逻辑、数据和用户界面拆散。在 Java Web 开发中,MVC 设计模式有助于实现代码的模块化和可维护性。以下是 MVC 各组件的简要介绍:
- Model(模型):示意数据和业务逻辑。通常包含数据拜访对象(DAO)和 JavaBean(如实体类、POJO)。
- View(视图):示意用户界面。通常为 JSP 页面、HTML、CSS 和 JavaScript 等。
- Controller(控制器):在模型和视图之间传递数据和指令。通常用 Servlet 实现。
8.4.1. 示例:应用 MVC 设计模式构建 Java Web 应用程序
以下是一个简略的 Java Web 应用程序示例,该应用程序应用 MVC 设计模式实现一个用户登录性能。
- 创立一个名为
LoginMVC
的 Web 应用程序目录构造:
LoginMVC/
|-- WEB-INF/
| |-- classes/
| | |-- controller/
| | | |-- LoginController.class
| | |-- model/
| | | |-- UserDao.class
| | | |-- User.class
| |-- lib/
| |-- web.xml
|-- login.jsp
|-- welcome.jsp
- 编写
User
类(模型):
public class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {return username;}
public void setUsername(String username) {this.username = username;}
public String getPassword() {return password;}
public void setPassword(String password) {this.password = password;}
}
- 编写
UserDao
类(模型):
import java.util.HashMap;
import java.util.Map;
public class UserDao {private static final Map<String, String> userData = new HashMap<>();
static {userData.put("admin", "password");
userData.put("user", "1234");
}
public User getUserByUsername(String username) {String password = userData.get(username);
if (password != null) {return new User(username, password);
}
return null;
}
}
- 编写
LoginController
类(控制器):
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "LoginController", urlPatterns = "/login")
public class LoginController extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String username = request.getParameter("username");
String password = request.getParameter("password");
UserDao userDao = new UserDao();
User user = userDao.getUserByUsername(username);
if (user != null && password.equals(user.getPassword())) {request.setAttribute("user", user);
request.getRequestDispatcher("welcome.jsp").forward(request, response);
} else {request.setAttribute("errorMessage", "Invalid username or password");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
}
- 编写
login.jsp
文件(视图):
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form action="login" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
<br>
<input type="submit" value="Login">
</form>
<p style="color:red;"><%= request.getAttribute("errorMessage") %></p>
</body>
</html>
- 编写
welcome.jsp
文件(视图):
jsp
Copy
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>Welcome, <%= ((User) request.getAttribute("user")).getUsername() %>!</h1>
</body>
</html>
- 编写
web.xml
文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0">
<servlet>
<servlet-name>LoginController</servlet-name>
<servlet-class>controller.LoginController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginController</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
当初,当用户拜访 login.jsp
页面并输出用户名和明码时,表单将提交给 LoginController
。控制器查问UserDao
以获取用户数据,并依据验证后果将用户重定向到 welcome.jsp
页面或显示谬误音讯。
通过应用 MVC 设计模式,咱们将应用程序的不同局部拆散,使其更易于保护和扩大。
举荐浏览:
https://mp.weixin.qq.com/s/dV2JzXfgjDdCmWRmE0glDA
https://mp.weixin.qq.com/s/an83QZOWXHqll3SGPYTL5g