一:基础知识

1.mvc

  model

  view

  control

2.模型

  是应用程序的主体部分,模型表示业务数据与业务逻辑。

  一个模型可以为多个视图提供数据

  提高了代码的可重用性

3.视图

  用户看到的可以交互的界面。

  向用户显示相关的数据

  可以接受用户的输入

  不进行任何实际的业务处理

4.控制器

  接受用户的输入并调用模型与视图去完成用户的需求。

  控制器接受请求决定调用哪个模型组件去处理请求,然后决定调用哪个视图显示模型处理返回的数据。

  

二:程序示例

1.目录结构

  

2.test.jsp

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="listAllStrudents">List All Students</a>
</body>
</html>

3.ListAllStrudents.java

 package mvc;

 import java.io.IOException;
import java.util.Arrays;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Servlet implementation class ListAllStrudents
*/
@WebServlet("/listAllStrudents")
public class ListAllStrudents extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
StudentDao studentDao=new StudentDao();
List<Student> students=studentDao.getAll();
request.setAttribute("students", students);
request.getRequestDispatcher("/students.jsp").forward(request, response);
} }

4.students.jsp

 <%@page import="mvc.Student"%>
<%@page import="java.util.List"%>
<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
List<Student> stus=(List<Student>)request.getAttribute("students");
%>
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<th>Flow_ID</th>
<th>Type</th>
<th>IdCard</th>
<th>ExcamCard</th>
<th>StudentName</th>
<th>Location</th>
<th>Grade</th>
<th>删除</th>
</tr>
<%
for(Student student :stus){
%>
<tr>
<td><%=student.getFlowId() %></td>
<td><%=student.getType() %></td>
<td><%=student.getIdCard()%></td>
<td><%=student.getExamCard() %></td>
<td><%=student.getStudentName() %></td>
<td><%=student.getLocation() %></td>
<td><%=student.getGraded() %></td>
<td><a href="deleteStudentServlet?flowId=<%=student.getFlowId()%>">Delete</a></td>
</tr>
<%
}
%>
</table> </body>
</html>

5.DeleteStudentServlet

 package 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; @WebServlet("/deleteStudentServlet")
public class DeleteStudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L; @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String flowId=request.getParameter("flowId");
StudentDao studentDao=new StudentDao();
studentDao.deleteByFlowId(Integer.parseInt(flowId));
request.getRequestDispatcher("/success.jsp").forward(request, response);
}
}

6.Student.java

 package mvc;

 public class Student {
private Integer flowId;
private int type;
private String idCard;
private String examCard;
private String studentName;
private String location;
private int graded;
/**
* constructor
*/
public Student() { }
public Student(Integer flowId, int type, String idCard, String examCard, String studentName, String location,
int graded) {
super();
this.flowId = flowId;
this.type = type;
this.idCard = idCard;
this.examCard = examCard;
this.studentName = studentName;
this.location = location;
this.graded = graded;
}
/**
* set and get function
* @return
*/
public Integer getFlowId() {
return flowId;
}
public void setFlowId(Integer flowId) {
this.flowId = flowId;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
public String getExamCard() {
return examCard;
}
public void setExamCard(String examCard) {
this.examCard = examCard;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public int getGraded() {
return graded;
}
public void setGraded(int graded) {
this.graded = graded;
} }

7.StudentDao.java

 package mvc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class StudentDao {
public void deleteByFlowId(Integer flowId) {
Connection connection=null;
PreparedStatement preparedStatement=null;
List<Student> students=new ArrayList<>();
try {
String driverClass="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/test";
String user="root";
String password="123456";
Class.forName(driverClass);
connection=DriverManager.getConnection(url, user, password);
String sql="DELETE FROM tb2 WHERE flow_id=?";
preparedStatement=connection.prepareStatement(sql);
preparedStatement.setInt(1, flowId);
preparedStatement.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(connection!=null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(preparedStatement!=null) {
preparedStatement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
} }
public List<Student> getAll() {
Connection connection=null;
PreparedStatement preparedStatement=null;
ResultSet resultSet=null;
List<Student> students=new ArrayList<>();
try {
String driverClass="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/test";
String user="root";
String password="123456";
Class.forName(driverClass);
connection=DriverManager.getConnection(url, user, password);
String sql="SELECT flow_id,type,id_card,exam_card,student_name,location,grade from tb2";
preparedStatement=connection.prepareStatement(sql);
resultSet=preparedStatement.executeQuery();
while(resultSet.next()) {
int flowId=resultSet.getInt(1);
int type=resultSet.getInt(2);
String idCard=resultSet.getString(3);
String examCard=resultSet.getString(4);
String studentName=resultSet.getString(5);
String location=resultSet.getString(6);
int graded=resultSet.getInt(7);
Student student=new Student(flowId, type, idCard, examCard, studentName, location, graded);
students.add(student);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(connection!=null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(preparedStatement!=null) {
preparedStatement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(resultSet!=null) {
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
} return students;
}
}

8.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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
删除操作成功!<br><br>
<a href="listAllStrudents">List All Students</a>
</body>
</html>

9.运行效果

  

MVC设计模式一的更多相关文章

  1. AngularJS_01之基础概述、设计原则及MVC设计模式

    1.AngularJS: 开源的JS框架,用来开发单一页面应用,以及数据操作频繁的场景:2.设计原则: ①YAGNI原则:You Aren't Gonna Need It! 不要写不需要的代码! ②K ...

  2. 谈谈JAVA工程狮面试中经常遇到的面试题目------什么是MVC设计模式

    作为一名java工程狮,大家肯定经历过很多面试,但每次几乎都会被问到什么是MVC设计模式,你是怎么理解MVC的类似这样的一系列关于MVC的问题. [出现频率] [关键考点] MVC的含义 MVC的结构 ...

  3. Java Web开发中MVC设计模式简介

    一.有关Java Web与MVC设计模式 学习过基本Java Web开发的人都已经了解了如何编写基本的Servlet,如何编写jsp及如何更新浏览器中显示的内容.但是我们之前自己编写的应用一般存在无条 ...

  4. MVC设计模式与三层架构

    三层架构分别是:表示层(Web层).业务逻辑层(BLL层)和数据访问层(DAL层). (1)表示层负责: a.从用户端收集信息 b.将用户信息发送到业务服务层做处理 c.从业务服务层接收处理结果 d. ...

  5. 传智播客JavaWeb day07、day08-自定义标签(传统标签和简单标签)、mvc设计模式、用户注册登录注销

    第七天的课程主要是讲了自定义标签.简单介绍了mvc设计模式.然后做了案例 1. 自定义标签 1.1 为什么要有自定义标签 前面所说的EL.JSTL等技术都是为了提高jsp的可读性.可维护性.方便性而取 ...

  6. mvc设计模式和mvc框架的区别

    Spring中的新名称也太多了吧!IOC/DI/MVC/AOP/DAO/ORM... 对于刚刚接触spring的我来说确实晕了头!可是一但你完全掌握了一个概念,那么它就会死心塌地的为你服务了.这可比女 ...

  7. MVC设计模式((javaWEB)在数据库连接池下,实现对数据库中的数据增删改查操作)

    设计功能的实现: ----没有业务层,直接由Servlet调用DAO,所以也没有事务操作,所以从DAO中直接获取connection对象 ----采用MVC设计模式 ----采用到的技术 .MVC设计 ...

  8. MVC设计模式(持续更新中)

    MVC设计模式--->英文全称为: model(模型)  View (视图)  Controller(控制)   MVC是一种设计思想.这种思想强调实现模型(Model).视图(View)和控制 ...

  9. iOS中MVC设计模式

    在组织大型项目的代码文件时,我们常用MVC的思想.MVC的概念讲起来非常简单,就和对象(object)一样.但是理解和应用起来却非常困难.今天我们就简单总结一下MVC设计理念. MVC(Model V ...

  10. java MVC设计模式

    MVC(Model View Control)模型-视图-控制器 一.MVC与模板概念的理解 MVC本来是存在于Desktop程序中的,M是指数据模型,V是指用户界面,C则是控制器.使用MVC的目的是 ...

随机推荐

  1. Intellij IDEA设置及快捷键使用总结

    1. IDEA内存优化 先看看你机器本身的配置而配置. \IntelliJ IDEA 8\bin\idea.exe.vmoptions -------------------------------- ...

  2. Java并发编程原理与实战一:聊聊并发

    一.大纲 •你真的了解并发吗 •多线程和并发 •多线程和多进程 •线程一定快吗 •学习并发的四个阶段 •学习目标 •适合人群 •荐书   二.学习并发的四个阶段 •熟练掌握API,能够完成并发编程 • ...

  3. Dialog插件artDialog

    经本人测试,发现没有layer好用,因为artDialog不能拖拽.不过除了拖拽,其他还蛮简洁的,在自定义上的灵活性也还可以.下面是我自己写的测试demo. <!DOCTYPE html> ...

  4. springmvc转springboot过程中访问jsp报Whitelabel Error Page错误

    前言: 虽然springboot内嵌了一个tomcat,但是这个内嵌的tomcat不支持jsp页面,所以需要引入其他包 解决: maven引入以下包即可 <dependency> < ...

  5. Swiper点击后自动轮播停止情况

    用户操作swiper之后,是否禁止autoplay.默认为true:停止. 如果设置为false,用户操作swiper之后自动切换不会停止,每次都会重新启动autoplay. 操作包括触碰,拖动,点击 ...

  6. swiper隐藏再显示出现点击不了情况

    //初始化swiper var swiper = new Swiper('.swiper-container', { pagination: '.swiper-pagination', nextBut ...

  7. Python基础入门(一)

    1.在线教程 2.Python下载地址,安装步骤,就是next.next... 3.配置环境变量(win8) 电脑 --> 属性 --> 高级系统设置 --> 环境变量,找到系统变量 ...

  8. MongoDB以Windows Service运行

    以Administrator身份打开CMD并输入以下命令 cd D:\Developer\MongoDB\mongodb-win32-x86_64-2.4.6\binD:mongod --logpat ...

  9. spring-boot-单元测试参数数

    简单案例 @RunWith(Parameterized.class) public class ParameterTest { // 2.声明变量存放预期值和测试数据 private String f ...

  10. 金蝶K3WISE常用数据表

    K3Wise 14.2 清空密码update t_User set FSID=') F ", ,P T #8 *P!D &D 80!N &@ <0 C '+''''+' ...