MVC设计模式一

一:基础知识

1.mvc

  model

  view

  control

2.模型

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

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

  提高了代码的可重用性

3.视图

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

  向用户显示相关的数据

  可以接受用户的输入

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

4.控制器

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

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

  

二:程序示例

1.目录结构

  

2.test.jsp

 1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 2     pageEncoding="ISO-8859-1"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <a href="listAllStrudents">List All Students</a>
11 </body>
12 </html>

3.ListAllStrudents.java

 1 package mvc;
 2 
 3 import java.io.IOException;
 4 import java.util.Arrays;
 5 import java.util.List;
 6 
 7 import javax.servlet.ServletException;
 8 import javax.servlet.annotation.WebServlet;
 9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 
13 /**
14  * Servlet implementation class ListAllStrudents
15  */
16 @WebServlet("/listAllStrudents")
17 public class ListAllStrudents extends HttpServlet {
18     private static final long serialVersionUID = 1L;
19        
20     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
21         StudentDao studentDao=new StudentDao();
22         List<Student> students=studentDao.getAll();
23         request.setAttribute("students", students);
24         request.getRequestDispatcher("/students.jsp").forward(request, response);
25     }
26 
27 }

4.students.jsp

 1 <%@page import="mvc.Student"%>
 2 <%@page import="java.util.List"%>
 3 <%@ page language="java" contentType="text/html; charset=utf-8"
 4     pageEncoding="utf-8"%>
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 6 <html>
 7 <head>
 8 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 9 <title>Insert title here</title>
10 </head>
11 <body>
12     <%
13         List<Student> stus=(List<Student>)request.getAttribute("students");
14     %>
15     <table border="1" cellpadding="10" cellspacing="0">
16         <tr>
17             <th>Flow_ID</th>
18             <th>Type</th>
19             <th>IdCard</th>
20             <th>ExcamCard</th>
21             <th>StudentName</th>
22             <th>Location</th>
23             <th>Grade</th>    
24             <th>删除</th>            
25         </tr>
26         <%
27             for(Student student :stus){
28         %>
29             <tr>
30                 <td><%=student.getFlowId() %></td>
31                 <td><%=student.getType() %></td>
32                 <td><%=student.getIdCard()%></td>
33                 <td><%=student.getExamCard() %></td>
34                 <td><%=student.getStudentName() %></td>
35                 <td><%=student.getLocation() %></td>
36                 <td><%=student.getGraded() %></td>
37                 <td><a href="deleteStudentServlet?flowId=<%=student.getFlowId()%>">Delete</a></td>
38             </tr>
39         <%
40             }
41         %>
42     </table>
43         
44 </body>
45 </html>

5.DeleteStudentServlet

 1 package mvc;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.annotation.WebServlet;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 
11 @WebServlet("/deleteStudentServlet")
12 public class DeleteStudentServlet extends HttpServlet {
13     private static final long serialVersionUID = 1L;
14     
15     @Override
16     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
17         String flowId=request.getParameter("flowId");
18         StudentDao studentDao=new StudentDao();
19         studentDao.deleteByFlowId(Integer.parseInt(flowId));
20         request.getRequestDispatcher("/success.jsp").forward(request, response);
21     }
22 }

6.Student.java

 1 package mvc;
 2 
 3 public class Student {
 4     private Integer flowId;
 5     private int type;
 6     private String idCard;
 7     private String examCard;
 8     private String studentName;
 9     private String location;
10     private int graded;
11     /**
12      * constructor
13      */
14     public Student() {
15         
16     }
17     public Student(Integer flowId, int type, String idCard, String examCard, String studentName, String location,
18             int graded) {
19         super();
20         this.flowId = flowId;
21         this.type = type;
22         this.idCard = idCard;
23         this.examCard = examCard;
24         this.studentName = studentName;
25         this.location = location;
26         this.graded = graded;
27     }
28     /**
29      * set and get function
30      * @return
31      */
32     public Integer getFlowId() {
33         return flowId;
34     }
35     public void setFlowId(Integer flowId) {
36         this.flowId = flowId;
37     }
38     public int getType() {
39         return type;
40     }
41     public void setType(int type) {
42         this.type = type;
43     }
44     public String getIdCard() {
45         return idCard;
46     }
47     public void setIdCard(String idCard) {
48         this.idCard = idCard;
49     }
50     public String getExamCard() {
51         return examCard;
52     }
53     public void setExamCard(String examCard) {
54         this.examCard = examCard;
55     }
56     public String getStudentName() {
57         return studentName;
58     }
59     public void setStudentName(String studentName) {
60         this.studentName = studentName;
61     }
62     public String getLocation() {
63         return location;
64     }
65     public void setLocation(String location) {
66         this.location = location;
67     }
68     public int getGraded() {
69         return graded;
70     }
71     public void setGraded(int graded) {
72         this.graded = graded;
73     }
74     
75     
76 }

7.StudentDao.java

 1 package mvc;
 2 import java.sql.Connection;
 3 import java.sql.DriverManager;
 4 import java.sql.PreparedStatement;
 5 import java.sql.ResultSet;
 6 import java.sql.SQLException;
 7 import java.util.ArrayList;
 8 import java.util.List;
 9 public class StudentDao {
10     public void deleteByFlowId(Integer flowId) {
11         Connection connection=null;
12         PreparedStatement preparedStatement=null;
13         List<Student> students=new ArrayList<>();
14         try {
15             String driverClass="com.mysql.jdbc.Driver";
16             String url="jdbc:mysql://localhost:3306/test";
17             String user="root";
18             String password="123456";
19             Class.forName(driverClass);                    
20             connection=DriverManager.getConnection(url, user, password);
21             String sql="DELETE FROM tb2 WHERE flow_id=?";
22             preparedStatement=connection.prepareStatement(sql);
23             preparedStatement.setInt(1, flowId);
24             preparedStatement.executeUpdate();
25         } catch (Exception e) {
26             e.printStackTrace();
27         }finally {
28             try {
29                 if(connection!=null) {
30                     connection.close();
31                 }
32             } catch (SQLException e) {
33                 e.printStackTrace();
34             }
35             try {
36                 if(preparedStatement!=null) {
37                     preparedStatement.close();
38                 }
39             } catch (SQLException e) {
40                 e.printStackTrace();
41             }
42         }
43         
44     }
45     public List<Student> getAll() {
46         Connection connection=null;
47         PreparedStatement preparedStatement=null;
48         ResultSet resultSet=null;
49         List<Student> students=new ArrayList<>();
50         try {
51             String driverClass="com.mysql.jdbc.Driver";
52             String url="jdbc:mysql://localhost:3306/test";
53             String user="root";
54             String password="123456";
55             Class.forName(driverClass);                    
56             connection=DriverManager.getConnection(url, user, password);
57             String sql="SELECT flow_id,type,id_card,exam_card,student_name,location,grade from tb2";
58             preparedStatement=connection.prepareStatement(sql);
59             resultSet=preparedStatement.executeQuery();
60             while(resultSet.next()) {
61                 int flowId=resultSet.getInt(1);
62                 int type=resultSet.getInt(2);
63                 String idCard=resultSet.getString(3);
64                 String examCard=resultSet.getString(4);
65                 String studentName=resultSet.getString(5);
66                 String location=resultSet.getString(6);
67                 int graded=resultSet.getInt(7);
68                 Student student=new Student(flowId, type, idCard, examCard, studentName, location, graded);
69                 students.add(student);                
70             }
71         } catch (Exception e) {
72             e.printStackTrace();
73         }finally {
74             try {
75                 if(connection!=null) {
76                     connection.close();
77                 }
78             } catch (SQLException e) {
79                 e.printStackTrace();
80             }
81             try {
82                 if(preparedStatement!=null) {
83                     preparedStatement.close();
84                 }
85             } catch (SQLException e) {
86                 e.printStackTrace();
87             }
88             try {
89                 if(resultSet!=null) {
90                     resultSet.close();
91                 }
92             } catch (SQLException e) {
93                 e.printStackTrace();
94             }
95         }
96         
97         return students;
98     }
99 }

8.success.jsp

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     删除操作成功!<br><br>
11     <a href="listAllStrudents">List All Students</a>
12 </body>
13 </html>

9.运行效果

  

原文地址:https://www.cnblogs.com/juncaoit/p/7434840.html