javaweb之MVC设计模式
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领域,一个伴你成长的公众号!