MVC设计模式-查询与删除
MVC是Model-View-Controller的简称,即模型-视图-控制器。
MVC是一种设计模式,它把应用程序分成三个核心模块:
模型:模型是应用程序的主体部分,模型表示业务数据和业务逻辑。
一个模型能为多个视图提供数据。
视图: 视图是用户看到并与之交互的界面,作用如下:
视图向用户显示相关的数据。
接受用户的输入。
不进行任何实际的业务处理。
控制器: 控制器接受用户的输入并调用模型和视图去完成用户的需求。
控制器接收请求并决定调用哪个模型组件去处理请求,
然后决定调用哪个视图来显示模型处理返回的数据。
粗糙的MVC设计模式- 查询与删除


1.创建examstudent表

Model模型
StudentDao.java
package com.aff.javaweb.mvc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List; import com.aff.bean.Student;
import com.aff.util.JDBCUtilsC3P0; public class StudentDao {
public void deleteByFlowId(Integer flowId) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = JDBCUtilsC3P0.getConnection();
String sql = "delete from examstudent where flow_Id = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1, flowId);
ps.executeUpdate();
} catch (Exception e) {
} finally {
JDBCUtilsC3P0.closeResource(conn, ps, null);
}
} public List<Student> getAll() { List<Student> students = new ArrayList<>();
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JDBCUtilsC3P0.getConnection();
String sql = "select flow_id flowId,Type, id_card idCard, examCard, student_name studentName, Location, Grade from examstudent";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
int flowId = rs.getInt(1);
int type = rs.getInt(2);
String idCard = rs.getString(3);
String examCard = rs.getString(4);
String studentName = rs.getString(5);
String location = rs.getString(6);
int grade = rs.getInt(7); Student student = new Student(flowId, type, idCard, examCard, studentName, location, grade);
students.add(student);
}
} catch (Exception e) {
} finally {
JDBCUtilsC3P0.closeResource(conn, ps, rs);
}
return students;
}
}
Controller 控制器
ListAllStudentsServlet.java
package com.aff.javaweb.mvc; import java.io.IOException;
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; import com.aff.bean.Student; @WebServlet("/listAllStudents")
public class ListAllStudentsServlet 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);
}
}
DeleteStudentServlet.java
package com.aff.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; @WebServlet("/deleteStudent")
public class DeleteStudentServlet extends HttpServlet {
private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String flowId = request.getParameter("flowId"); StudentDao studentDao = new StudentDao();
studentDao.deleteByFlowId(Integer.parseInt(flowId));// 把传来的string类型强转为int类型 request.getRequestDispatcher("/success.jsp").forward(request, response);
} }
VIEW 视图
test
<%@ 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>
<a href="listAllStudents">ListAllStudents</a>
</body>
</html>
students.jsp
<%@page import="com.aff.bean.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=UTF-8">
<title>Insert title here</title>
</head>
<body> <%
List<Student> stus = (List<Student>) request.getAttribute("students");
%> <table border="1" cellpadding="10" cellspacing="0">
<tr>
<td>FlowId</td>
<td>Type</td>
<td>IdCard</td>
<td>ExamCard</td>
<td>StudentName</td>
<td>Location</td>
<td>Grade</td>
<td >Delete</td>
</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.getGrade()%></td>
<td ><a href="deleteStudent?flowId=<%=student.getFlowId()%>">Delete</a></td> </tr> <%
}
%> </table> </body>
</html>
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>Insert title here</title>
</head>
<body>
<h3>Success Page</h3>
<a href="listAllStudents">List All Students</a>
</body>
</html>
javabean
Student.java
package com.aff.bean;
public class Student {
private Integer flowId;
private int type;
private String idCard;
private String examCard;
private String studentName;
private String location;
private int grade;
public Student() {
super();
}
public Student(Integer flowId, int type, String idCard, String examCard, String studentName, String location,
int grade) {
super();
this.flowId = flowId;
this.type = type;
this.idCard = idCard;
this.examCard = examCard;
this.studentName = studentName;
this.location = location;
this.grade = grade;
}
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 getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
@Override
public String toString() {
return "Student [flowId=" + flowId + ", type=" + type + ", idCard=" + idCard + ", examCard=" + examCard
+ ", studentName=" + studentName + ", location=" + location + ", grade=" + grade + "]";
}
}
小结:
关于 MVC:
M: Model. Dao
V: View. JSP, 在页面上填写 Java 代码实现显示
C: Controller. Serlvet:
受理请求
获取请求参数
调用 DAO 方法
可能会把 DAO 方法的返回值放入 request 中
转发(或重定向)页面
什么时候转发,什么时候重定向 ? 若目标的响应页面不需要从 request 中读取任何值,则可以使用重定向。(还可以防止表单的重复提交)
不足:
使用DBUtils,JDBCUtils 工具类,DAO 基类
一个请求一个 Serlvet 不好!一个模块使用一个 Serlvet,即多个请求可以使用一个 Servlet
在页面上加入 jQuery 提示
MVC设计模式-查询与删除的更多相关文章
- JavaWeb笔记三、MVC 设计模式
一.通过 MVC 进行查询和删除操作 1. 准备一个数据表(examstudent) 2. 创建一个 查询 页面(test.jsp) 通过连接进入 Servlet(listAllStudents.ja ...
- [原创]java WEB学习笔记19:初识MVC 设计模式:查询,删除 练习(理解思想),小结 ,问题
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
- MVC设计模式((javaWEB)在数据库连接池下,实现对数据库中的数据增删改查操作)
设计功能的实现: ----没有业务层,直接由Servlet调用DAO,所以也没有事务操作,所以从DAO中直接获取connection对象 ----采用MVC设计模式 ----采用到的技术 .MVC设计 ...
- JavaWeb:基于MVC设计模式的一个小案例(一)
(未经允许,请勿转载,谢谢.) 本案例的处理过程: 客户端发送一个请求给服务器,服务器把这个请求给Servlet,Servlet 获取请求信息,根据请求信息的情况去调用 model (在这里是一个普通 ...
- 【原创翻译】认识MVC设计模式:web应用开发的基础(实际编码篇)
原文地址:http://www.larryullman.com/2009/10/15/understanding-mvc-part-3/ 全系列INDEX [原创翻译]认识MVC设计模式:web应用开 ...
- 第80节:Java中的MVC设计模式
第80节:Java中的MVC设计模式 前言 了解java中的mvc模式.复习以及回顾! 事务,设置自动连接提交关闭. setAutoCommit(false); conn.commit(); conn ...
- SpringMVC的工作原理及MVC设计模式
SpringMVC的工作原理: 1.当用户在浏览器中点击一个链接或者提交一个表单时,那么就会产生一个请求(request).这个请求会携带用户请求的信息,离开浏览器. 2.这个请求会首先到达Sprin ...
- ASP.NET MVC 5 - 查询Details和Delete方法
在这部分教程中,接下来我们将讨论自动生成的Details和Delete方法. 查询Details和Delete方法 打开Movie控制器并查看Details方法. public ActionResul ...
- 谈谈JAVA工程狮面试中经常遇到的面试题目------什么是MVC设计模式
作为一名java工程狮,大家肯定经历过很多面试,但每次几乎都会被问到什么是MVC设计模式,你是怎么理解MVC的类似这样的一系列关于MVC的问题. [出现频率] [关键考点] MVC的含义 MVC的结构 ...
随机推荐
- css实现水平垂直居中的几种方式
梳理下平时常用css水平垂直居中方式- 使用flex布局 HTML <div class="box"> <div class="child"& ...
- B站弹幕系统架构——GOIM解读
架构图 说明: 1.logic启动http服务器, 接受http请求,用于将数据推送到kafka以及获取在线用户信息,websocket身份校验 2.comet组件起动webdocket/tcp服务, ...
- java权限设计思考
1.粗粒度权限设计与细粒度权限设计 粗粒度(Coarse-graind) 表示类别级,即仅考虑对象的类别(the type of object),不考 ...
- Python爬虫(一)爬百度贴吧
简单的GET请求: # python2 import urllib2 response = urllib2.urlopen('http://www.baidu.com') html = respons ...
- 1年之后的拿高工资的资本,Java线程
只要开启线程,都会开启一块对应的栈内存,然后进行同步执行. -- 谈斌 线程是CPU用来处理程序的资源,线程的执行是抢占式的. 线程开启方式: 创建一个类,继承Thread类.重写 run(), 并在 ...
- K - Leapin' Lizards HDU - 2732 网络流
题目链接:https://vjudge.net/contest/299467#problem/K 这个题目从数据范围来看可以发现是网络流,怎么建图呢?这个其实不是特别难,主要是读题难. 这个建图就是把 ...
- spring mvc从后台往前台传参数的三种方式
第一种:使用Model对象(常用) 第一步:使用model对象往前台传递数据 第二步:在jsp中接收从后台传递过来的参数 第二种:使用HttpServletRequest对象 第一步:使用HttpSe ...
- SpringCloudStream学习(二)RabbitMQ中的交换机跟工作模式
知识储备: 交换机: RabbitMQ中有4中交换机,分别是 (FANOUT)扇形交换机: 扇形交换机是最基本的交换机类型,它所能做的事情非常简单---广播消息.扇形交换机会把能接收到的消息全部发 ...
- USACO 3.1 Contact
http://www.nocow.cn/index.php/Translate:USACO/contact 题目大意:给一个只含0和1的序列,统计每个子序列的重复次数,并按次数递减来输出 考虑子序列时 ...
- [hdu4358]树状数组
思路:用一个数组记录最近k次的出现位置,然后在其附近更新答案.具体见代码: #pragma comment(linker, "/STACK:10240000,10240000") ...