MVC(Model-View-Controller,模型-视图-控制器)是一种设计模式,用于将应用程序的业务逻辑、数据和用户界面分离。在Java Web开发中,MVC设计模式有助于实现代码的模块化和可维护性。以下是MVC各组件的简要介绍:
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设计模式,我们将应用程序的不同部分分离,使其更易于维护和扩展。