欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MVC模式小教程

程序员文章站 2024-02-26 18:53:16
...

٩(๑>◡<๑)۶今天给大家介绍个新伙伴——MVC模式
一、它是谁?

  1. MVC 模式代表 Model-View-Controller(模型-视图-控制器) 模式。这种模式用于应用程序的分层开发。
    Model(模型) - 模型代表一个存取数据的对象或 JAVA POJO。它也可以带有逻辑,在数据变化时更新控制器。
    View(视图) - 视图代表模型包含的数据的可视化。
    Controller(控制器) - 控制器作用于模型和视图上。它控制数据流向模型对象,并在数据变化时更新视图。它使视图与模型分离开。

二、它在哪?
MVC模式小教程
三、它该怎么办?

package com.News.entity;

import java.sql.Date;

public class News {
    private int news_id;
    private String news_title;
    private Date news_date;
    private String author;
    private String content;
    private String type;
    private String discussion;
    private String picture;
    private String movie;
    public int getNews_id() {
        return news_id;
    }
    public void setNews_id(int news_id) {
        this.news_id = news_id;
    }
    public String getNews_title() {
        return news_title;
    }
    public void setNews_title(String news_title) {
        this.news_title = news_title;
    }
    public Date getNews_date() {
        return news_date;
    }
    public void setNews_date(Date news_date) {
        this.news_date = news_date;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getDiscussion() {
        return discussion;
    }
    public void setDiscussion(String discussion) {
        this.discussion = discussion;
    }
    public String getPicture() {
        return picture;
    }
    public void setPicture(String picture) {
        this.picture = picture;
    }
    public String getMovie() {
        return movie;
    }
    public void setMovie(String movie) {
        this.movie = movie;
    }


}
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <title>登录</title>
        <link rel="stylesheet" type="text/css" href="css/login.css">
    </head>
    <body>
        <div id="header">
            <div id="header-content">       
                <div id="header-content2">
                    <ul>
                        <li><a href="news.html">新闻</a></li>
                        <li><a href="scj.html">收藏夹</a></li>
                    </ul>
                </div><!--中间结束-->

                <div id="header-content3">
                    <a href="login.jsp"?method = 2>登录</a>/
                    <a href="zc.jsp"method = 1>注册</a>/
                    <a>用户名</a>
                </div>
            </div>
        </div><!--导航栏结束-->

    <div id="bbBox">
        <div id="bigBox">
            <h1>登 录</h1>
            <form action="UserServlet" method="post">
                <span id="s1"></span><p id="p1"><label>用户名:</label><input type="text" name="username" id="userId" placeholder="请输入用户名"/></p>
                <span id="s2"></span><p id="p2"><label>密码:</label><input type="password" name="password" id="passWord" placeholder="请输入密码" /></p>
                <input type="submit" value="登录" id="thesubmit"/>
                <input type="reset" id="res"/>
            </form>
        </div>
    </div>

        <script type="text/javascript" src="js/login.js"></script>
    </body>
</html>
package com.News.controller;

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

import com.News.dao.UserDao;
import com.News.entity.Users;
import com.News.service.UserService;

public class UserServlet extends HttpServlet {

    /**
         * Constructor of the object.
         */
    public UserServlet() {
        super();
    }

    /**
         * Destruction of the servlet. <br>
         */
    public void destroy() {
        super.destroy(); // Just puts "destroy" string in log
        // Put your code here
    }

    /**
         * The doGet method of the servlet. <br>
         *
         * This method is called when a form has its tag value method equals to get.
         * 
         * @param request the request send by the client to the server
         * @param response the response send by the server to the client
         * @throws ServletException if an error occurred
         * @throws IOException if an error occurred
         */
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    /**
         * The doPost method of the servlet. <br>
         *
         * This method is called when a form has its tag value method equals to post.
         * 
         * @param request the request send by the client to the server
         * @param response the response send by the server to the client
         * @throws ServletException if an error occurred
         * @throws IOException if an error occurred
         */
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        int method = Integer.parseInt(request.getParameter("method"));
        switch (method) {
        case 1:
            this.register(request, response);
            break;
        case 2:
            this.login(request, response);
            break;

        }

    }
    public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        UserDao uDao = new UserDao();
        Users u = new Users();
        uDao.Insert(u);
    }
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String password_true = UserService.Login(username);
        String mark = UserService.getMark(username);
        if(!password.equals(password_true)){
            System.out.println("Error!");
            request.getRequestDispatcher("").forward(request, response);
        }else{
            System.out.println("True!");
            if(mark.equals("vip"))
            request.getRequestDispatcher("").forward(request, response);
        }
    }
    /**
         * Initialization of the servlet. <br>
         *
         * @throws ServletException if an error occurs
         */
    public void init() throws ServletException {
        // Put your code here
    }

}

四、优秀的它

  1. 一个模型提供不同的多个视图表现形式,也能够为一个模型创建新的视图而无须重写模型。
  2. 模型可复用。
  3. 维护成本低,扩展性高
相关标签: MVC模式