MVC原理及案例分析
关于MVC
MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑。MVC被独特的发展起来用于映射传统的输入、处理和输出功能在一个逻辑的图形化用户界面的结构中。MVC开始是存在于桌面程序中的,M是指业务模型,V是指用户界面,C则是控制器,使用MVC的目的是将M和V的实现代码分离,从而使同一个程序可以使用不同的表现形式。
以简单的登录功能为案例,简单分析MVC的具体实现
项目执行的流程
项目文件中各个层次的作用
具体代码实现:
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="LoginServlet" method="post">用户名:<input type="text" name="uname"><br/>密码: <input type="password" name="upwd"><br/><input type="submit" value="登录"></form>
</body>
</html>
Welcome.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>Insert title here</title>
</head>
<body>登陆成功!!
</body>
</html>
LoginServlet.java
package org.zxm.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.zxm.Dao.LoginDao;
import org.zxm.entity.Login;//控制器层,用于接收view层的请求,并分发给Model处理
public class LoginServlet extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//post请求方式设置编码request.setCharacterEncoding("UTF-8");//接受用户名和密码String name = request.getParameter("uname");String pwd = request.getParameter("upwd");//封装Login login = new Login(name,pwd);//将用户名和密码传递到登录功能里面(LoginDao)//调用模型层的登录功能int result = LoginDao.login(login);if(result > 0) {//重定向response.sendRedirect("Welcome.jsp");}else {response.sendRedirect("login.jsp");}}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}
LoginDao.java
/*** */
package org.zxm.Dao;import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import org.zxm.entity.Login;/*** @author 11441*模型层,用于处理登录(查询数据库)*/
public class LoginDao {//用于登录的login方法//在方法参数中传入login类型public static int login(Login login) {//登录成功与否的标识//boolean flag = false;int flag = -1; //-1系统异常 0:用户名或密码有误 1:登陆成功int result = -1;Connection connection = null;PreparedStatement prepareStatement = null;ResultSet rs = null;try {//加载驱动Class.forName("com.mysql.jdbc.Driver");//获取连接connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test1", "root", "root");//访问数据库String sql = "select count(*) from login where username = ? and pwd = ?";prepareStatement = connection.prepareStatement(sql);prepareStatement.setString(1, login.getUsername());prepareStatement.setString(2, login.getPwd());rs = prepareStatement.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(prepareStatement != null) prepareStatement.close();if(connection != null) connection.close();} catch (SQLException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}} }
}
Login.java
/*** */
package org.zxm.entity;/*** @author 11441**/
public class Login {private int id;private String username;private String pwd;public Login(String username, String pwd) {this.username = username;this.pwd = pwd;}public Login() {}public Login(int id, String username, String pwd) {this.id = id;this.username = username;this.pwd = pwd;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPwd() {return pwd;}public void setPwd(String pwd) {this.pwd = pwd;}
}
总结:
MVC模式的优点:
- 按照M:Model V:View C:Controller进行分层,有利于代码的维护,提高了代码重用率,更好的实现开发中的分工
- 有益于后期代码的维护,降低了成本
MVC模式的缺点:
- 增加了复杂性
- 运行效率降低