1.MVC簡介
MVC是Model-View-Controller的簡稱,即模型-視圖-控制器。MVC是一種設計模式,它把應用程序分成三個核心模塊:模型,視圖,控制器,它們各自處理自己的任務。
- 模型(體現在下圖中的POJO和數據庫)是應用程序的主體部分,表示業務數據和業務邏輯。一個模型能為多個視圖提供數據。由於應用於模型的代碼只需要寫一次就可以被多個視圖重用,所以提高了代碼的可重用性。
- 視圖是用戶看到並與之交互的界面,可以向用戶顯示相關的數據,也可以接收用戶的輸入,但是不進行任何實際的業務處理。
- 控制器接收請求,獲取請求參數,調用DAO方法,決定用哪個模型組件去處理請求,然后決定調用哪個視圖來顯示模型處理返回的數據。
MVC模式處理過程邏輯放在servlet中,顯示交給jsp。客戶端發請求到服務器,服務器調用servlet,servlet作為一個控制器,接收請求,根據請求的相關邏輯情況去調用java類的方法,由java類完成業務邏輯跟訪問數據庫的操作,然后servlet根據pojo的返回結果,轉向不同的jsp頁面, jsp完成顯示的功能。
2.MVC案例之查詢
MySql數據庫中的數據內容為:
例如,現有需求需要實現在網頁點擊超鏈接,可以在頁面顯示參加考試的學生的所有信息(學生的考試信息存儲在數據庫中)。設計思路如下圖所示,首先點擊網頁的超鏈接listAllExamStudent,發送get請求到servlet,由服務器調用servlet的doGet方法,在doGet()方法中需要做到:①.調用ExamStudentDao的getAll()方法返回學生的List對象;②.把1得到的List放入request中;③.請求的轉發到student.jsp;
實現代碼:
點擊網頁的超鏈接listAllExamStudent,發送get請求到servlet。searchTest.jsp
<body> <a href="listAllStudent">listAllStudents</a> </body>
listAllStudentServlet.java
package com.javaWebMVCTest; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class listAllStudentServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { studentDao studentdao=new studentDao();
//調用ExamStudentDao的getAll()方法返回學生的List對象; List<student> students=studentdao.getAll();
//把1得到的List放入request中 request.setAttribute("students", students);
//請求的轉發到student.jsp request.getRequestDispatcher("/jspTest/students.jsp").forward(request,response); } }
在web.xml中進行配置:
<servlet> <servlet-name>listAllStudentServlet</servlet-name> <servlet-class>com.javaWebMVCTest.listAllStudentServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>listAllStudentServlet</servlet-name> <url-pattern>/listAllStudent</url-pattern> </servlet-mapping>
<url-pattern>/listAllStudent</url-pattern>映射的地址為searchTest.jsp中超鏈接<a href="listAllStudent">的鏈接地址。
student.java
package com.javaWebMVCTest; public class student { private Integer flow_id; private int Type; private String id_card; private String exam_card; private String student_name; private String Location; private int Grade; public Integer getFlow_id() { return flow_id; } public void setFlow_id(Integer flow_id) { this.flow_id = flow_id; } public int getType() { return Type; } public void setType(int type) { Type = type; } public String getId_card() { return id_card; } public void setId_card(String id_card) { this.id_card = id_card; } public String getExam_card() { return exam_card; } public void setExam_card(String exam_card) { this.exam_card = exam_card; } public String getStudent_name() { return student_name; } public void setStudent_name(String student_name) { this.student_name = student_name; } public String getLocation() { return Location; } public void setLocation(String location) { Location = location; } public int getGrade() { return Grade; } public void setGrade(int grade) { Grade = grade; } public student(Integer flow_id, int type, String id_card, String exam_card, String student_name, String location, int grade) { super(); this.flow_id = flow_id; Type = type; this.id_card = id_card; this.exam_card = exam_card; this.student_name = student_name; Location = location; Grade = grade; } public student(){} }
連接數據庫及查詢的操作:studentDao.java
package com.javaWebMVCTest; 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; import com.javaWebMVCTest.student; public class studentDao { public List<student> getAll(){ List<student> stus=new ArrayList<>(); Connection connection=null; PreparedStatement preparedstament=null; ResultSet resultset=null; try{ String driverClass="com.mysql.jdbc.Driver"; Class.forName(driverClass); System.out.println("數據庫驅動加載成功!"); connection=DriverManager.getConnection("jdbc:mysql:"+"//localhost:3303/students?autoReconnect=true&failOverReadOnly=false","root","0404"); System.out.println("數據庫連接成功!"); String sql="SELECT flow_id,Type,id_card,exam_card,student_name,Location,Grade FROM students"; preparedstament=connection.prepareStatement(sql); resultset=preparedstament.executeQuery(); while (resultset.next()){ int flow_id=resultset.getInt(1); int Type=resultset.getInt(2); String id_card=resultset.getString(3); String exam_card=resultset.getString(4); String student_name=resultset.getString(5); String Location=resultset.getString(6); int Grade=resultset.getInt(7); student students=new student(flow_id,Type,id_card,exam_card,student_name,Location,Grade); stus.add(students); } }catch(Exception e){ e.printStackTrace(); } try{ if (connection!=null){ connection.close(); } }catch(SQLException e){ e.printStackTrace(); } try{ if (preparedstament!=null){ preparedstament.close(); } }catch(SQLException e){ e.printStackTrace(); } try{ if (resultset!=null){ resultset.close(); } }catch(SQLException e){ e.printStackTrace(); } return stus; } }
顯示信息的跳轉頁面:students.jsp
<body> <% List<student> stus=(List<student>)request.getAttribute("students"); %> <table> <tr> <th>flow_id</th> <th>Type</th> <th>id_card</th> <th>exam_card</th> <th>student_name</th> <th>Location</th> <th>Grade</th> </tr> <% for(student stu:stus){ %> <tr> <td><%=stu.getFlow_id() %></td> <td><%=stu.getType() %></td> <td><%=stu.getId_card() %></td> <td><%=stu.getExam_card() %></td> <td><%=stu.getStudent_name() %></td> <td><%=stu.getLocation() %></td> <td><%=stu.getGrade() %></td> </tr> <% } %> </table> </body>
運行后顯示:
wx搜索“程序員考拉”,專注java領域,一個伴你成長的公眾號!