在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. 华科机考:N阶楼梯上楼

    时间限制:1秒空间限制:32768K 题目描述 N阶楼梯上楼问题:一次可以走两阶或一阶,问有多少种上楼方式.(要求采用非递归) 输入描述: 输入包括一个整数N,(1<=N<90). 输出描 ...

  2. Weblogic Exception in AppMerge flows' progression

    原因:经过分析是web.xml配置的问题,有些servlet上面配置了'display-name',这个weblogic是不支持的. 解决:在web.xml中把'display-name'删除掉,工程 ...

  3. Printer for Me

    今天,良心系部终于给我配了打印机^^. 办公室门外还挂了牌子.

  4. plsql和tsql常用函数比较

    数学函数 .绝对值 S:) value O:) value from dual .取整(大) S:select ceiling(-1.001) value O:select ceil(-1.001) ...

  5. 剑指架构师系列-ftp服务器

    1.安装FTP 我们在开发项目时,肯定需要专门的一台ftp服务器来存在上传的静态资源,今天我们就在CentOS下搭建一个ftp服务器. 1.安装vsftpd组件,安装完后,有/etc/vsftpd/v ...

  6. webpack4.x配置详解,多页面,多入口,多出口,新特性新坑!!

    花了差不多一天多的时间,重新撸了一遍webpack4.x的常用配置. 基本上常用的配置都熟悉了一遍,总体上来讲,为了对parcel进行反击,webpack从4.x开始,正在朝着尽可能的简化配置文件的方 ...

  7. 安卓高级6 玩转AppBarLayout,更酷炫的顶部栏 Toolbar

    原文大神地址:http://www.jianshu.com/p/d159f0176576 上一篇文章[<CoordinateLayout的使用如此简单 >]上一篇文章<Coordin ...

  8. Android Studio精彩案例(一)《ActionBar和 ViewPager版仿网易新闻客户端》

    转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 为了能更好的分享高质量的文章,所以开设了此专栏.文章代码都以Android Studio亲测运行,读者朋友可在后面直接下载源码.该专栏 ...

  9. java 随机数高效生成

    分享牛,分享牛原创.近期去面试经常被问到java如何生产随机数,以及生成很大的字符串保证不能重复,还要考虑性能,之前本人面试别人的时候,可能不会问这个问题.既然这个java随机数问题经常被问到,那咱们 ...

  10. iOS学习笔记--数据存储

    iOS应用数据存储的常用方式 XML属性列表(plist)归档 Preference(偏好设置) NSKeyedArchiver归档(NSCoding) SQLite3 Core Data 1. XM ...