一,MVC將代碼分為三個部分,分別為視圖(jsp),模型(javaBean),控制部分(servlet);
視圖基本為 jsp 文件,主要內容為界面的html代碼,負責顯示界面;
模型為 javaBean ,負責與數據庫交互;
控制部分為 servlet 充當,負責處理業務邏輯與頁面切換。
二,MVC包括 Model1 和 Model2 兩個模型;
1. Model1 模型程序流程如下圖:
Model1 中界面顯示部分與界面跳轉,業務邏輯都由 jsp 負責,導致 jsp 中充滿大量java腳本代碼,
代碼重復率高,可用性低,程序功能的微小的修改往往引起大量的修改,優點是容易掌控。
2. Model2 模型程序流程如下圖:
Model2 模型中分層更加明顯,jsp 負責頁面顯示,javaBean負責數據庫操作與業務邏輯,servlet
則是核心的調度部分,負責調用 javaBean 來處理業務邏輯,同時負責界面調度切換。優點是程序
執行更加清晰,方便後期功能的添加與修改。
三,基於MVC模型的用戶登陸實例:
1.界面部分(login.jsp登陸界面,login_success.jsp登陸成功界面,login_failure.jsp登錄失敗界面)
login.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>用戶登錄</title> </head> <body> <form action="../loginConf" method="post"> <center> <table> <tr> <td>用戶名:</td> <td><input type="text" name="uname" /></td> </tr> <tr> <td>密 碼:</td> <td><input type="password" name="password" /></td> </tr> <tr> <td colspan="2"><input type="submit" value="提交" /> <input type="reset" value="重置" /></td> </tr> </table> </center> </form> </body> </html> View Code效果圖:
login_success.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>登陸成功</title> </head> <body> <% if(session.getAttribute("login")!=null&&session.getAttribute("login").equals("true")) { %> <center> <h2>登陸成功</h2> </center> <% } else { %> <jsp:forward page="login.jsp"></jsp:forward> <% } %> </body> </html> View Code效果圖:
login_failure.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>登錄失敗</title> </head> <body> <center> <h2>登錄失敗</h2> </center> </body> </html> View Code效果圖:
2.servlet(controller)
loginConf.java
package com.javaweb.mvc; import java.io.IOException; 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 javax.servlet.http.HttpSession; /** * Servlet implementation class loginConf */ @WebServlet("/loginConf") public class loginConf extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public loginConf() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //設置編碼格式 response.setContentType("text/html; charset=utf-8"); request.setCharacterEncoding("utf-8"); //獲取參數 String uname = request.getParameter("uname"); String password = request.getParameter("password"); //獲取session HttpSession session = request.getSession(); //業務邏輯判斷 loginCheck lc = new loginCheck(); if(lc.isLogin(uname, password)) { session.setAttribute("login", "true"); request.getRequestDispatcher("MVC/login_success.jsp").forward(request, response); } else { request.getRequestDispatcher("MVC/login_failure.jsp").forward(request, response); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } } View Code
3.javaBean(業務邏輯Model)
package com.javaweb.mvc; public class loginCheck { public boolean isLogin(String uname,String password) { if(uname.equals("wei")&&password.equals("123")) { return true; } else { return false; } } } View Code