mvc实用架构设计 mvc设计模式例子

您所在的位置:网站首页 mvc是设计模式还是架构 mvc实用架构设计 mvc设计模式例子

mvc实用架构设计 mvc设计模式例子

2023-06-15 22:42| 来源: 网络整理| 查看: 265

MVC案例

首先我们回顾一下MVC的执行过程:

MVC设计模式的模块组成是由:模型、视图和控制器组成。其中

模型:代表应用程序状态和业务逻辑。

视图:提供可交互的客户的界面,向客户显示模型数据。

控制器:根据客户的请求来操纵模型,并把结果经由视图展现给客户。

而MVC的工作模式如下图所示:

mvc实用架构设计 mvc设计模式例子_mvc实用架构设计

 

其中控制器Controller也就是Servlet。

我们回顾了之前所学的MVC的执行过程后,想必是对MVC的功能是了解清楚了。那我们言归正传,做个小案例来测试一下。(此次的案例将会用到JavaBean和Servlet的知识。)

首先,我们先写出登录页,并给用户名和密码赋予name值,method你可以使用get提交方式,也可使用post提交方式,在此我就用post提交方式来演示,而action则是用的:/你所创建的项目名称/你所创建Servlet的名称:

用户名: 密码:

其次,我们创建loginServlet,“Login login = new Login(uname,upwd)与 int result = LoginDao.login(login)“则是把用户名和密码封装成一个实体类(JavaBean)。[封装业务逻辑的模型对应一个功能,封装数据JavaBean(实体类)对应一张表(数据库的表)]

loginServlet:

//解决中文乱码 request.setCharacterEncoding("UTF-8"); /** *response.setCharacterEncoding("UTF-8"); *response.setContentType("text/html;"); *此二者写其一即可 **/ response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); //处理登录,获取表单数据 String uname = request.getParameter("uname"); String upwd = request.getParameter("upwd"); Login login = new Login(uname,upwd); //调用模型层的登录功能 int result = LoginDao.login(login); if (result > 0) { response.sendRedirect("index.jsp"); }else{ response.sendRedirect("login.jsp"); }

 

LoginDao:

//模型层,用于处理登录操作(访问数据库,查询数据) public static int login(Login login){ // 连接数据库 Connection con = null; PreparedStatement pstmt = null; ResultSet rs = null; //指定数据指针,limit为0时也不能执行 int result = -1; /** * -1:系统异常 * 0:用户名或密码有误 * 1:登录成功 */ int flag = -1; try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user?characterEncoding=utf8&useSSL=false&serverTiemzone=UTC", "root", "123456"); String sql = "select * from user where username = ? and userpass = ?"; pstmt = con.prepareStatement(sql); pstmt.setString(1,login.getUsername()); pstmt.setString(2,login.getUserpass()); rs = pstmt.executeQuery(); if (rs.next()){ //拿到数据库中的第一条数据,相当于limit 1; result = rs.getInt(1); } //登录成功 if (result > 0){ return 1; } else { return 0; } } catch (ClassNotFoundException e) { e.printStackTrace(); /** * 此时的登录失败是系统异常所导致的登录失败 * 所以登录失败分为两种,一种为用户名或密码错误; * 另外一种则为之系统异常。 */ return -1; } catch (SQLException throwables) { throwables.printStackTrace(); return -1; } finally { // 关闭数据库连接对象 try { if(rs != null) { rs.close(); } if(pstmt != null) { pstmt.close(); } if(con != null) { con.close(); } }catch (Exception e) { System.out.println("JDBC-Close Error!"); } } }

Login:

private int id; private String username; private String userpass; //无参构造 public Login() { } //因id不用手动添加,故不用写id public Login(String username, String userpass) { this.username = username; this.userpass = userpass; } //有参构造 public Login(int id, String username, String userpass) { this.id = id; this.username = username; this.userpass = userpass; } 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 getUserpass() { return userpass; } public void setUserpass(String userpass) { this.userpass = userpass; }

最终执行过程如下图所示:

mvc实用架构设计 mvc设计模式例子_用户名_02



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3