0
点赞
收藏
分享

微信扫一扫

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


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

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

1、关于MVC框架模型

  • MVC是三个单词的缩写:
  • ​M​​,Model(模型);
  • ​V​​,View( 视图 ),
  • ​C​​,Control(控制)。
  • Model层:实现系统的业务逻辑(javaBean)
    View层:负责与用户交互,即在界面上展示数据对象给用户(html,jsp)
    Control层:Model与View之间沟通的桥梁,它可以分派用户的请求并选择恰当的视图以用于显示,同时它也可以解释用户的输入并将它们映射为模型层可执行的操作(Servlet)

[

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

2、MVC设计登录案例

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

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

2.1设计登录案例交互图

2.1.1明确各个模型:

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

2.2项目结构图一览

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

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>

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

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


举报

相关推荐

0 条评论