【案例+源码】详解MVC框架模式及其应用

本文深入解析MVC框架的概念及其实现原理,并通过一个登录案例演示如何在实际项目中运用MVC。从模型(Model)、视图(View)到控制器(Controller),详细介绍了各组件的作用与交互方式。

【案例+源码】详解MVC框架模式及其应用

写在开头:
		首先我们需要知道,框架模式、模式、开发模式是三种不同的概念,
但他们的目的都一样:解耦!

1、关于MVC框架模型

  • MVC是三个单词的缩写:

    • M,Model(模型);
    • V,View( 视图 ),
    • C,Control(控制)。
  • Model层:实现系统的业务逻辑(javaBean)

    View层:负责与用户交互,即在界面上展示数据对象给用户(html,jsp)

    Control层:Model与View之间沟通的桥梁,它可以分派用户的请求并选择恰当的视图以用于显示,同时它也可以解释用户的输入并将它们映射为模型层可执行的操作(Servlet)

[

2、MVC设计登录案例

MVC框架模型之间的关系处理:

2.1设计登录案例交互图

2.1.1明确各个模型:

2.2项目结构图一览

2.3、代码部分

2.3.1View视图层代码

login.jsp:

<%--
  Created by IntelliJ IDEA.
  User: 编程小哥令狐
  Date: 2020/10/8
  Time: 22:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<center>
    <form action="UserServlet" method="post">
        <h4>登录</h4>
        <hr/>
        姓名:<input type="text" name="username"/><br/>
        密码:<input type="password" name="password"/><br/>
        <input type="submit" value="提交"/>
    </form>
</center>
</body>
</html>

success.jsp:

<%--
  Created by IntelliJ IDEA.
  User: 编程小哥令狐
  Date: 2020/10/8
  Time: 22:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<center>
    <h3>登陆成功</h3>
    <%--<a href="<%= path %>/login.jsp">后退</a>--%>
</center>
</body>
</html>

unsuccess.jsp:

<%--
  Created by IntelliJ IDEA.
  User: 编程小哥令狐
  Date: 2020/10/8
  Time: 22:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<div style="text-align: center;">
    <h3>登陆失败</h3>
    <%--<a href="<%= path %>/login.jsp">后退</a>--%>

</div>
</body>
</html>

2.3.2Controller控制层代码

UserServlet.java:

package com.mvc.servlet;

import com.mvc.javabean.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userName = req.getParameter("username");
        String password = req.getParameter("password");

        User user = new User();
        if(user.validatorUser(userName, password)){
            resp.sendRedirect("success.jsp");
        }
        else{
            resp.sendRedirect("unsuccess.jsp");
        }

    }
}

2.3.3Model模型层代码:

User.java:

package com.mvc.javabean;

public class User {
    public boolean validatorUser(String username, String password){
        if(username.equals("admin") && password.equals("admin")){
            return true;
        }
        else{
            return false;
        }
    }

}

2.4、Web.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>MVC</display-name>
    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>UserServlet</servlet-name>
        <servlet-class>com.mvc.servlet.UserServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>UserServlet</servlet-name>
        <url-pattern>/UserServlet</url-pattern>
    </servlet-mapping>
</web-app>

我们默认了账号密码为:admin,你输入的数据会和它进行比较,进而做出不同的页面跳转。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodeLinghu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值