在DRP中终于接触到了MVC,感触是确实这样的架构系统灵活性不少,现在感触最深的就是使用tomcat作为服务器发布比IIS好多了,起码发布很简单,使用起来方便。

首先来简单的学习一下MVC的基础知识,MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑和数据显式分离的方法组织代码,将业务逻辑被聚集到一个部件里面,在界面和用户围绕数据的交互能被改进和个性化定制的同时而不需要重新编写业务逻辑。

概览

MVC被独特的发展起来用于映射传统的输入、处理和输出功能在一个逻辑的图形化用户界面的结构中,MVC是分层的思想的体现,但是区别于三层设计模式(区别以后再和大家分享)。

MVC是一个框架模式,它强制性的使应用程序的输入、处理和输出分开。使用MVC应用程序被分成三个核心部件:模型、视图、控制器。它们各自处理自己的任务。最典型的MVC就是JSP
+ servlet + javabean的模式。

实例解析

UML图:大家熟悉MVC的调用流程逻辑

第一:JSP:由页面指令和HTML组成的查询界面query_condention.jsp,也就是咱们现在的html页和asp页面类似。

<%@ page language="java" contentType="text/html;charset=GBK"%>
<html>
<head>
<title>学生信息</title>
</head>
<body>
<form action="SearchStudentServlet" method="post">
出生日期:<input type="text" name="beginDate">至<input type="text" name="endDate">
<input type="submit" value="查询学生">
</form>
</body>
</html>

第二:控制层 SearchStudentServlet用来接受客户的请求,来处理流程,调用Model(StudentManager.java),转发到要请求的后台服务器的student_list.jsp页面

import java.text.*;
import java.util.*;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*; import com.bjpowernode.exam.model.*;
import com.bjpowernode.exam.manager.*; public class SearchStudentServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { String sBeginDate = request.getParameter("beginDate");
String sEndDate = request.getParameter("endDate"); Date beginDate = new Date();
Date endDate = new Date();
try {
beginDate = new SimpleDateFormat("yyyy-MM-dd").parse(sBeginDate);
endDate = new SimpleDateFormat("yyyy-MM-dd").parse(sEndDate);
}catch(Exception e) {
e.printStackTrace();
} StudentManager studentManager = new StudentManagerImpl();
List<Student> studentList = studentManager.findStudentList(beginDate, endDate); //将学生列表设置到requet范围中
//request.setAttribute("student_list", studentList); //转发,转发是在服务器端转发的,客户端是不知道的
//request.getRequestDispatcher("/student_list.jsp").forward(request, response); //将studentList放到session中
HttpSession session = request.getSession();
session.setAttribute("student_list", studentList); //重定向,不会共享request
//以下写法错误,该 "/"代表了8080端口
//response.sendRedirect("/student_list.jsp");
response.sendRedirect(request.getContextPath() + "/student_list.jsp");
}
}

第三 :student_list.jsp页面接收数据形成html,返回到浏览器,渲染在界面上

<%@ page language="java" contentType="text/html;charset=GBK"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="com.bjpowernode.exam.model.*"%>
<%@ page import="com.bjpowernode.exam.manager.*"%>
<html>
<head>
<title>学生信息</title>
<style type="text/css">
/*表格宽度为1px,实线,黑色*/
table{
border:1px solid black;
border-collapse:collapse;
} td {
border:1px solid black;
border-collapse:collapse;
} </style>
</head>
<body>
<table border="1">
<tr>
<td>学生代码</td>
<td>姓名</td>
<td>性别</td>
<td>出生日期</td>
<td>联系电话</td>
<td>家庭住址</td>
<td>班级名称</td>
<td>年龄</td>
</tr>
<%
//List<Student> studentList = (List)request.getAttribute("student_list");
List<Student> studentList = (List)session.getAttribute("student_list");
for (Iterator<Student> iter=studentList.iterator(); iter.hasNext();) {
Student student = iter.next();
%>
<tr>
<td><%=student.getStudentId()%></td>
<td><%=student.getStudentName()%></td>
<td><%=student.getSex()%></td>
<td><%=new SimpleDateFormat("yyyy-MM-dd").format(student.getBirthday())%></td>
<td><%=student.getContactTel()%></td>
<td><%=student.getAddress()%></td>
<td><%=student.getClasses().getClassesName()%></td>
<%
long b = 1000L*60L*60L*24L*365L;
long a = System.currentTimeMillis() - student.getBirthday().getTime();
%>
<td><%=a/b%></td>
</tr>
<%
}
%>
</table>
</body>
</html>

在View的student_list.jsp页面中是大量的html和java代码的混合,在查询条件界面query_condention.jsp主要是html,因为不涉及后台数据的交互.

第四:xml配置Servlet:



<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>SearchStudentServlet</servlet-name>
<servlet-class>SearchStudentServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SearchStudentServlet</servlet-name>
<url-pattern>/SearchStudentServlet</url-pattern>
</servlet-mapping> </web-app>

第五:显示查询结果

总结

以上query_condention.jsp(输入查询条件)、SearchStudentServlet.java(请求控制Control分)+student_list.jsp(界面显示)的组合相当于三层中的U层,都与界面的显示相关,而StudentManager.java才是进入业务处理相当于三层的B层。故,我们可以简单理解成,MVC就是java基于U层的又一个细化,将界面显示和请求处理做了进一步细化分工。

MVC被独特的发展起来用于映射传统的输入、处理和输出功能在一个逻辑的图形化用户界面的结构中,MVC是分层的思想的体现,但是区别于三层设计模式(区别以后再和大家分享)。



    知识的联系促使学习这块知识难度不大,更加容易的上手,编制知识网显得格外重要啊!

接下来和大家推出《深入浅出Java》系类博客,共同学习、提高,敬请期待!

深入浅出Java MVC(Model View Controller) ---- (JSP + servlet + javabean实例)的更多相关文章

  1. What is the difference between Reactjs and Rxjs?--React is the V (View) in MVC (Model/View/Controller).

    This is really different, React is view library; and Rxjs is reactive programming library for javasc ...

  2. Model View Controller (MVC) Overview

    By Rakesh Chavda on Jul 01, 2015 What is MVC?Model View Controller is a type of user interface archi ...

  3. MVC jsp+servlet+javabean 连接Mysql数据库測试demo

    本文介绍的是怎样使用MVC架构去实现jsp+servlet+javabean连接数据库 首先我们应该了解什么是MVC: MVC包含三个部分 : ①View:由各种JSP页面组成. ②Controlle ...

  4. MVC模式(Model View Controller)下实现数据库的连接,对数据的删,查操作

    MVC模式(Model View Controller): Model:DAO模型 View:JSP  在页面上填写java代码实现显示 Controller:Servlet 重定向和请求的转发: 若 ...

  5. MVC(Model View Controller)框架

    MVC框架 同义词 MVC一般指MVC框架 MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一 ...

  6. Model View Controller(MVC) in PHP

    The model view controller pattern is the most used pattern for today’s world web applications. It ha ...

  7. Jsp+Servlet+JavaBean经典MVC模式理解

    MVC模式目的(实现Web系统的职能分工). 在Java EE中,Jsp+Servlet+JavaBean算是里面经典的模式,是初学者必备的知识技能.M, Model(模型)实现系统的业务逻辑 1.通 ...

  8. (jsp+servlet+javabean )MVC架构

    MVC是三个单词的缩写,这三个单词分别为:模型.视图和控制. 使用的MVC的目的:在于将M和V的实现代码分离,从而使同一个程序可以使用不同的表现形式.比如Windows系统资源管理器文件夹内容的显示方 ...

  9. JSP中使用的模式——JSP+Servlet+JavaBean

    上一篇博文写到模式一:JSP+JavaBean 链接地址:http://wxmimperio.coding.io/?p=155 JSP中两种模式的总结 链接地址:http://wxmimperio.c ...

随机推荐

  1. Vue2学习(3)

    子组件索引 尽管有 props 和 events,但是有时仍然需要在 JavaScript 中直接访问子组件.为此可以使用 ref 为子组件指定一个索引 ID.例如: <div id=" ...

  2. WPF ListBox/ListView/DataGrid 虚拟化时的滚动方式

    ListBox的滚动方式 分为像素滚动和列表项滚动 通过ListBox的附加属性ScrollViewer.CanContentScroll来设置.因此ListBox的默认模板中,含有ScrollVie ...

  3. 利用生产者消费者模型和MQ模型写一个自己的日志系统-并发设计里一定会用到的手段

    一:前言 写这个程序主要是用来理解生产者消费者模型,以及通过这个Demo来理解Redis的单线程取原子任务是怎么实现的和巩固一下并发相关的知识:这个虽然是个Demo,但是只要稍加改下Appender部 ...

  4. sessionStorage 、localStorage 和 cookie

    localStorage 和 sessionStorage HTML5 提供了两种在客户端存储数据的新方法:localStorage 和 sessionStorage: 两者都是仅在客户端(即浏览器) ...

  5. LintCode题解之比较字符串

    使用标记的方式,先遍历一遍B,出现一次就记录一次出现次数,然后遍历A,将记录的B的出现次数消去,最后检查一下记录的标记位是不是都消去了,总共需要检查三次,即进行三次O(n)的遍历. 然后总结出规律如果 ...

  6. MVC和MTV模式

    著名的MVC模式:所谓MVC就是把web应用分为模型(M),控制器(C),视图(V)三层:他们之间以一种插件似的,松耦合的方式连接在一起. 模型负责业务对象与数据库的对象(ORM),视图负责与用户的交 ...

  7. 网络七层OSI模型简介

    0.  网络七层OSI模型(Open System Interconnection)总览: 1.  应用层 2.  表示层 :表示层的作用是使通信的应用程序能够解释交换数据的含义.这些服务包括数据压缩 ...

  8. 利用百度接口进行人脸识别并保存人脸jpg文件

    利用百度接口进行人脸识别,根据返回的人脸location用opencv切割保存. # coding : UTF-8 from aip import AipFace import cv2 import ...

  9. 部署 Helm - 每天5分钟玩转 Docker 容器技术(162)

    本节我们将安装和部署 Helm 客户端和 Tiller 服务器. Helm 客户端 通常,我们将 Helm 客户端安装在能够执行 kubectl 命令的节点上,只需要下面一条命令: curl http ...

  10. Bootstrap3 表单-被支持的控件:文本域

    支持多行文本的表单控件.可根据需要改变 rows 属性. <textarea class="form-control" rows="3"></ ...