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领域,一个伴你成长的公众号!
javaweb之MVC设计模式的更多相关文章
- JavaWeb笔记——MVC设计模式和JavaWeb经典三层架
1 MVC设计模式 MVC设计模式 MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器(C ...
- IT兄弟连 JavaWeb教程 MVC设计模式
MVC是Model-View-Controller的简称,即模型-视图-控制器.MVC是一种设计模式,它强制性地把应用程序的数据展示.数据处理和流程控制分开.MVC把应用程序分成3个核心模块:模型.视 ...
- 传智播客JavaWeb day07、day08-自定义标签(传统标签和简单标签)、mvc设计模式、用户注册登录注销
第七天的课程主要是讲了自定义标签.简单介绍了mvc设计模式.然后做了案例 1. 自定义标签 1.1 为什么要有自定义标签 前面所说的EL.JSTL等技术都是为了提高jsp的可读性.可维护性.方便性而取 ...
- MVC设计模式JavaWeb实现
JSP开发模式 jsp开发模式的发展 1.模式1:(适合小型项目的技术的开发) a.第一版本号.纯jsp(封装数据.处理数据,显示数据) b.第二版本号,Jsp+JavaBean. ...
- javaweb三大框架和MVC设计模式
javaweb三大框架和MVC设计模式 转载,原文请见https://blog.csdn.net/sunpeng19960715/article/details/50890705 一.MVC设计模式 ...
- 转载 javaweb三大框架和MVC设计模式 (自己加拉些内容)
javaweb三大框架和MVC设计模式 一.MVC设计模式 1.MVC的概念 首先我们需要知道MVC模式并不是javaweb项目中独有的,MVC是一种软件工程中的一种软件架构模式,把软件系统分为三个基 ...
- MVC设计模式与JavaWEB三层架构
一.MVC设计模式 MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器(Controlle ...
- Javaweb MVC设计模式
Javaweb MVC设计模式 一.Java EE开发流程 二.MVC设计模式 什么是MVC? MVC是Model-View-Controller的简称,即模型-视图-控制器. MVC是一种设计模式, ...
- 浅谈javaweb三大框架和MVC设计模式
一.MVC设计模式 1.MVC的概念 首先我们需要知道MVC模式并不是javaweb项目中独有的,MVC是一种软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(Vie ...
随机推荐
- 树莓派 Raspbian
备注,从右往左分别是:无线鼠标一个, HDMI转VGA接口一个,网线一根,小米充电宝电源线一个.树莓派Pi 3 一台,包括读卡器一个+32G class10 SD卡一块.最后俩个U盘作为备用里面有Ar ...
- P2319 [HNOI2006]超级英雄 题解
[HNOI2006]超级英雄 题目描述 现在电视台有一种节目叫做超级英雄,大概的流程就是每位选手到台上回答主持人的几个问题,然后根据回答问题的多少获得不同数目的奖品或奖金.主持人问题准备了若干道题目, ...
- NOIP前的模板
1.筛\(phi\) \(logn\)求少数\(phi\) inline int phi(R int x){ R int res=x,tmp=x; for(R int i=2;i*i<=x;i+ ...
- HTML5本地存储——IndexedDB二:索引
HTML5本地存储——IndexedDB(二:索引) 在HTML5本地存储——IndexedDB(一:基本使用)中介绍了关于IndexedDB的基本使用方法,很不过瘾,这篇我们来看看indexed ...
- Shell基本知识
Shell是什么 Shell是一个命令行解释器,它为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以使用Shell来启动.挂起.停止甚至编写一些程序. Shell还是一个功 ...
- 虚拟机上使用 opecnv 读取USB摄像头无法显示
使用opecv读取USB摄像头时候,无法显示图像. 设置 首先查看虚拟机Ubuntu检测摄像头是否已正常插入: ls /dev/video* 结果为: 设置虚拟机USB属性: USB的兼容性设置为US ...
- CoreImage 图片处理
CoreImage 是苹果 iOS5新增的一个 OC 框架,提供了强大的图像处理功能, 用于对基于像素的图像进行操作与分析, 提供了很多滤镜(Filter),形成强大的自定义效果 CIImage 类 ...
- Angular material mat-icon 资源参考_Maps
ul,li>ol { margin-bottom: 0 } dt { font-weight: 700 } dd { margin: 0 1.5em 1.5em } img { height: ...
- Codeforces - tag::graphs 大合集 [占坑]
520B 给定初始n和目标m,存在两种操作\(-1\)和\(×2\),要求最少操作次数 无脑解法是BFS,不能解决稍大的规模 当n>m时,输出n-m 否则逆向处理,转换为m到n的最小操作次数,存 ...
- 论文阅读 | Region Proposal by Guided Anchoring
论文阅读 | Region Proposal by Guided Anchoring 相关链接 论文地址:https://arxiv.org/abs/1901.03278 概述 众所周知,anchor ...