例子
本例子通過簡單登錄,瞭解MVC設計模式的原理
跟三層一樣存在依賴,MVC中Model層依賴Controller層,controller依賴View層
什麼是依賴?
簡單說依賴就是一個類需要另一個類的屬性或者方法才能正常的運行。
比如:M層中存在一個JavaBean對象,這個對象是通過Controller層創建並且傳進去的;
又比如:Controller層需要獲得View層用戶輸入進來的數據
像這樣的叫依賴。
M:Model 模型層,用於與數據庫打交道
V:View 視圖層,與用戶打交道
C:Controller 控制層,承上啓下的作用,接受V層的用戶數據,把用戶數據傳入M層;接受M層的返回結果,把M層的返回結果返回給V層
一、MVC理解
二、步驟
1、View視圖層(登錄界面)
視圖層通過表單提交給 Servlet ,請求方式爲 post
<%@ 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="LoginServlet" method = "post">
用戶名:<input type="text" name="uname"><br/>
密碼:<input type="password" name="upwd"><br/>
<input type="submit" value="登錄">
</form>
</body>
</html>
2、Model層(數據庫訪問)
這裏需要注意的是:我的數據庫是雲ip地址,並不是本機的ip地址
如果你們是本機數據庫,ip地址改爲 127.0.0.1
本機數據庫需要打開服務.
package org.lanqiao.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.lanqiao.entity.Login;
import com.mysql.jdbc.Driver;
import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;
//模型層:用於處理登錄(查詢數據)
public class LoginDao {
static String url="jdbc:mysql://120.76.156.19:3306/STD2017";
static String user="STD2017";
static String password="STD20171QAZ";
//1 : 成功
//0:賬號密碼錯誤
//-1:異常
public static int login(Login login) {
int result = -1;
Connection connection = null;
PreparedStatement pstmt = null;
ResultSet rs =null;
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url, user, password);
String sql = "select count(*) from login where uname=? and upwd=?";
pstmt = connection.prepareStatement(sql);//預處理
pstmt.setString(1, login.getUname());
pstmt.setString(2, login.getUpwd());
rs = pstmt.executeQuery();
if(rs.next()) {
result = rs.getInt(1);
}
if(result > 0) {
return 1;//成功
}else {
return 0;//賬號密碼錯誤
}
}catch(ClassNotFoundException e){
e.printStackTrace();
return -1;//異常
}catch ( SQLException e) {
e.printStackTrace();
return -1;
}catch (Exception e) {
e.printStackTrace();
return -1;
}finally {
try {
if(rs!=null) rs.close();
if(pstmt!=null) pstmt.close();
if(connection!=null) connection.close();
}catch( SQLException e) {
e.printStackTrace();
}
}
}
}
(1)、JavaBean(對應數據庫表結構)
package org.lanqiao.entity;
public class Login {
private int uid;
private String uname;
private String upwd;
public Login() {
}
public Login( String uname, String upwd) {
this.uname = uname;
this.upwd = upwd;
}
public Login(int uid, String uname, String upwd) {
this.uid = uid;
this.uname = uname;
this.upwd = upwd;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
}
3、Controller層(其實就是Servlet)
從 View 層接受到的數據組裝成 JavaBean ,然後發送給 Model 層進行數據庫查詢
package org.lanqiao.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.lanqiao.dao.LoginDao;
import org.lanqiao.entity.Login;
//控制層:接受view請求,並分發給model處理
public class LoginServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//處理登錄請求,調模型層的Login
//從view層 接受用戶的賬號密碼
request.setCharacterEncoding("utf-8");
String name = request.getParameter("uname");
String pwd = request.getParameter("upwd");
//把賬號密碼合二爲一成javabean
Login login = new Login(name,pwd);
//把對象傳入model層
//調用模型層的登錄功能
int result = LoginDao.login(login);
//成功重定向到成功頁面, 失敗則回到登錄頁面
if(result == 1) {
response.sendRedirect("welcome.jsp");
}else {
response.sendRedirect("login.jsp");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
4、登錄成功頁面
<%@ 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>Insert title here</title>
</head>
<body>
登錄成功。
</body>
</html>