码迷,mamicode.com
首页 > Web开发 > 详细

MVC设计模式-查询与删除

时间:2020-04-18 11:40:37      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:import   一个   模块   响应   ack   type   date()   vax   xtend   

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设计模式-查询与删除

标签:import   一个   模块   响应   ack   type   date()   vax   xtend   

原文地址:https://www.cnblogs.com/afangfang/p/12724669.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!