JavaWeb--MVC设计模式

MVC是Model-View-Controller的简称,即模型-视图-控制器。

MVC是一种设计模式,它把应用程序分成三个核心模块:模型、视图、控制器,它们各自处理自己的任务。

 

 

关于 MVC:
  M: Model. Dao
  V: View. JSP, 在页面上填写 Java 代码实现显示
  C: Controller. Serlvet:
    受理请求
    获取请求参数
    调用 DAO 方法
    可能会把 DAO 方法的返回值放入 request 中
    转发(或重定向)页面
什么时候转发,什么时候重定向 ? 若目标的响应页面不需要从 request 中读取任何值,则可以使用重定向。(还可以防止表单的重复提交)
不足:
  使用数据库连接池,DBUtils,JDBCUtils 工具类,DAO 基类
  一个请求一个 Serlvet 不好!一个模块使用一个 Serlvet,即多个请求可以使用一个 Servlet
  在页面上加入 jQuery 提示

1.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <servlet>
        <!--<description></description>
        <display-name>ListAllStudentsServlet</display-name>-->
        <servlet-name>ListAllStudentsServlet</servlet-name>
        <servlet-class>Javaweb_MVCTest.ListAllStudentsServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>ListAllStudentsServlet</servlet-name>
        <url-pattern>/listAllStudents</url-pattern>
    </servlet-mapping>
</web-app>

  2.写test.jsp

 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: Skye
 4   Date: 2017/12/7
 5   Time: 15:26
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 9 <html>
10 <head>
11     <title>Title</title>
12 </head>
13 <body>
14     <a href="listAllStudents">List All Students</a>  //超链接,里面的listAllStudents指向web.xml中
15                                                        的listAllStudents,即ListAllStudentsServlet的Java文件
16 </body> 
17 </html>

3.编写Student类

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

4.建立数据库连接,读取数据(编写StudentDAO)

 1 package Javaweb_MVCTest;
 2 
 3 import java.sql.*;
 4 import java.util.ArrayList;
 5 import java.util.List;
 6 
 7 public class StudentDAO {
 8 
 9     public List<Student> getAll(){
10 
11         List<Student> students = new ArrayList<>();
12 
13         Connection connection = null;
14         PreparedStatement preparedStatement = null;
15         ResultSet resultSet = null;
16 
17         try{
18             String driverClass = "com.mysql.jdbc.Driver";
19             String url = "jdbc:mysql://localhost:3306/db_person";
20             String user = "root";
21             String password = "1234";
22 
23             Class.forName(driverClass);
24             connection = DriverManager.getConnection(url, user, password);
25 
26             String sql = "SELECT flow_id, type, id_card, exam_card, student_name, location, grade FROM examstudent";
27 
28             preparedStatement = connection.prepareStatement(sql);
29 
30            // preparedStatement.executeUpdate();
31             resultSet = preparedStatement.executeQuery();
32 
33             while(resultSet.next()){
34                 int flowId = resultSet.getInt(1);
35                 int type = resultSet.getInt(2);
36                 String idCard = resultSet.getString(3);
37                 String examCard = resultSet.getString(4);
38                 String studentName = resultSet.getString(5);
39                 String location = resultSet.getString(6);
40                 int grade = resultSet.getInt(7);
41 
42                 Student student = new Student(flowId, type, idCard, examCard, studentName, location, grade);
43 
44                 students.add(student);
45 
46             }
47 
48         } catch (Exception e) {
49             e.printStackTrace();
50         }finally{
51             if(resultSet != null){
52                 try {
53                     resultSet.close();
54                 } catch (SQLException e1) {
55                     e1.printStackTrace();
56                 }
57             }
58             if(preparedStatement != null){
59                 try {
60                     preparedStatement.close();
61                 } catch (SQLException e1) {
62                     e1.printStackTrace();
63                 }
64             }
65             if(connection != null){
66                 try {
67                     connection.close();
68                 } catch (SQLException e1) {
69                     e1.printStackTrace();
70                 }
71             }
72         }
73         return students;
74     }
75 
76 }

5.调用4的结果(ListAllStudentsServlet)

 1 package Javaweb_MVCTest;
 2 
 3 import javax.servlet.ServletException;
 4 import javax.servlet.http.HttpServlet;
 5 import javax.servlet.http.HttpServletRequest;
 6 import javax.servlet.http.HttpServletResponse;
 7 import java.io.IOException;
 8 import java.util.Arrays;
 9 import java.util.List;
10 
11 public class ListAllStudentsServlet extends HttpServlet {
12 
13     private static final long serialVersionUID = 1L;
14     @Override
15     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
16             throws ServletException, IOException {
17         //super.doGet(req, resp);
18 
19         StudentDAO studentDAO = new StudentDAO();
20         List<Student> students = studentDAO.getAll();
21 
22         req.setAttribute("students", students);//把students这个对象保存在req作用域中,在转发进入的页面就能直接获取
23 
24         req.getRequestDispatcher("/students.jsp").forward(req, resp);//请求转发
25     }
26 }

 

 

 

  

posted @ 2017-12-07 10:53  SkyeAngel  阅读(359)  评论(0编辑  收藏  举报