SlideShare une entreprise Scribd logo
1  sur  31
设计模式之MVC基于Web 主讲:Hesey
提纲 What and Why 优缺点 实例分析 举一反三
What is MVC? MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model)、视图(View)和控制器(Controller)。 目的:动态的程序设计 简化后续修改和扩展 复用代码 专一化(模块、人员)
视图 界面设计人员进行图形界面设计 展示数据(不仅仅是显示) 无业务逻辑
控制器 负责转发请求,对请求进行处理 不同层面间的组织作用 控制应用程序的流程 处理事件并作出响应
模型 包含程序应有的功能(实现算法等) Bean/Service/DAO etc. 封装数据与逻辑 直接访问数据(DB) 不依赖“视图”和“控制器” 刷新机制
原始时代(JSP or Servlet)
原始时代(JSP or Servlet)  Password.htm <HTML> <BODY> <form action="PasswordGen.jsp" method="POST"> <H3>Welcome to the Password Generator</H3> First Name: <input type="text" name="firstName"><br> Last Name: <input type="text" name="lastName"><br> <p> <input type="submit" value="Generate Password"> </form> </BODY> </HTML>
原始时代(JSP or Servlet)
原始时代(JSP or Servlet) PasswordGen.jsp <html> <body> <%   String firstName = request.getParameter("firstName");   String lastName = request. getParameter("lastName");   String password; if( (firstName.length()>=2) && (lastName.length()>=2)) 	password = lastName.substring(0,2)+firstName.substring(0,2);   else 	password = "NoGo"; %> <h1> Password Generated! </h1> Your super secret password is <%= password %>. <br><a href="Password.htm">To generate another password.</a> </body> </html>
原始时代(JSP or Servlet) PasswordGen.jsp <html> <body> <%   String firstName = request.getParameter("firstName");   String lastName = request. getParameter("lastName");   String password; if( (firstName.length()>=2) && (lastName.length()>=2)) 	password = lastName.substring(0,2)+firstName.substring(0,2);   else 	password = "NoGo"; %> <h1> Password Generated! </h1> Your super secret password is <%= password %>. <br><a href="Password.htm">To generate another password.</a> </body> </html> 看着都费劲,别说修改了╮(╯_╰)╭
缺点 职责多元 HTML代码和Java代码混杂(好眼花⊙﹏⊙) 修改逻辑or 修改呈现? 代码复用?(审核身份、日志记录) 团队开发困难
MVC
优点 解耦 职责单一 分工明确(模块、人员) 代码复用 测试方便 对比 职责多元 HTML代码和Java代码混杂(好眼花) 修改逻辑 or 修改呈现? 增加服务? 代码复用?(审核身份、日志记录) 团队开发困难
缺点 比较抽象 没有明确的定义,难以完全理解 需要精心计划与设计,花费可观 更多文件,更多代码,增加了复杂度 这么复杂,为什么还要用? 弥补:框架
Servlet and JSP
Servlet and JSP  Password.htm <HTML> <BODY> <form action="/webapp/passwordservlet" method="POST"> <H3>Welcome to the Password Generator</H3> First Name: <input type="text" name="firstName"><br> Last Name: <input type="text" name="lastName"><br> <p> <input type="submit" value="Generate Password"> </form> </BODY> </HTML>
Servlet and JSP  passwordServlet.java public void doPost(HttpServletRequest request, HttpServletResponse response) throws Exception { GenBeangb = new GenBean(); gb.setFirstName(request.getParameter("firstName")); gb.setLastName(request.getParameter("lastName")); gb.generate(); request.setAttribute("gen", gb); RequestDispatcherrd = getServletContext().getRequestDispatcher("/PasswordGen.jsp"); rd.forward(request, response);   }
Servlet and JSP  GenBean.java  public class GenBean {   private String firstName;   private String lastName;   private String password = "NoGo";   public String getFirstName(){ return firstName; }   public void setFirstName(String fn){ firstName = fn;}   public String getLastName(){ return lastName; }   public void setLastName(String ln){ lastName = ln; }   public String getPassword(){return password;}   public String generate() {     if( (firstName.length() >= 2) && (lastName.length() >= 2))       password = lastName.substring(0,2) + firstName.substring(0,2);     return password;   } }
Servlet and JSP PasswordGen.jsp <html> <body> <jsp:useBean id = "gen" class = “genpackage.GenBean" scope="request"/> <h1> Password Generated! </h1> Your super secret password is <jsp:getProperty name="gen" property="password"/> <br><a href="Password.htm">To generate another password.</a> </body> </html>  清晰了很多吧o(≧v≦)o~~
体现分发的Servlet控制器  verify.java  public void doPost(HttpServletRequest request, HttpServletResponse response) throws Exception {     String username = request.getParameter(“username"));      String password = request.getParameter(“password"));  boolean valid = new Security().verify(username, password); RequestDispatcherrd= null;     if(valid) { rd= getServletContext().getRequestDispatcher("/valid.jsp");     }      else { rd= getServletContext().getRequestDispatcher("/invalid.jsp");     } rd.forward(request, response);   }
现在呈现和逻辑被分开了,真好呀 Servlet 1 JSP 1 Servlet 2 JSP 2 Servlet 3 JSP 3 有没有改进的余地?
如果把Servlet组合起来? Controller JSP 1 Model 1 JSP 2 Model 2 JSP 3 Model 3 关系是不是简单了许多?
Struts 2
Model Action 1 Action 2 Action 3 Bean DAO Service Struts 2 XML配置 Controller Struts 2 Dispatcher 浏览器 Web容器 View JSP 1 JSP 2 DataBase
web.xml     <filter>         <filter-name>struts2</filter-name>         <filter-class>             org.apache.struts2.dispatcher.FilterDispatcher         </filter-class>     </filter>     <filter-mapping>         <filter-name>struts2</filter-name>         <url-pattern>/*</url-pattern>     </filter-mapping>
Struts.xml <struts>  	<package name=“default” extends=“struts-default”> 		<action name=“hello” class=“HelloWorld”> 		<result name=“SUCCESS”> /HelloWorld.jsp 		</result> 		</action>		 	</package> </struts>
HelloWorld.java(POJO) public class HelloWorld {	private String message;	public String execute() Exception {   		message = “Hello,world”;        	message += “The  time is:”;        	message += System.currentTimeMillis(); return “SUCCESS”;	}	public void setMessage(String message){this.message = message;	}	public String getMessage() {		return message;	}}
HelloWorld.jsp <%@ taglib prefix="s" uri="/struts-tags" %><html><head><title>Hello World!</title></head><body><h2><s:property value="message" /></h2></body></html>
举一反三 思考模式思想,套用模式不如不用模式 解耦 单一职责原则(SRP) 测试
谢谢!

Contenu connexe

Similaire à 设计模式MVC

Struts学习笔记
Struts学习笔记Struts学习笔记
Struts学习笔记yiditushe
 
ASP.Net MVC2 简介
ASP.Net MVC2 简介ASP.Net MVC2 简介
ASP.Net MVC2 简介Allen Lsy
 
Asp.net mvc 培训
Asp.net mvc 培训Asp.net mvc 培训
Asp.net mvc 培训lotusprince
 
Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Wade Huang
 
Vlog02 [eng sub]什麼是controller和如何在asp.net核心中創建controller?-what is controller ...
Vlog02  [eng sub]什麼是controller和如何在asp.net核心中創建controller?-what is controller ...Vlog02  [eng sub]什麼是controller和如何在asp.net核心中創建controller?-what is controller ...
Vlog02 [eng sub]什麼是controller和如何在asp.net核心中創建controller?-what is controller ...SernHao TV
 
Struts快速学习指南
Struts快速学习指南Struts快速学习指南
Struts快速学习指南yiditushe
 
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit TestingASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing江華 奚
 
ASP.NET MVC Code Templates實戰開發 -twMVC#4
 ASP.NET MVC Code Templates實戰開發 -twMVC#4 ASP.NET MVC Code Templates實戰開發 -twMVC#4
ASP.NET MVC Code Templates實戰開發 -twMVC#4twMVC
 
twMVC#04 | ASP.NET MVC - Code Templates實戰開發
twMVC#04 | ASP.NET MVC - Code Templates實戰開發twMVC#04 | ASP.NET MVC - Code Templates實戰開發
twMVC#04 | ASP.NET MVC - Code Templates實戰開發twMVC
 
JdonFramework中文
JdonFramework中文JdonFramework中文
JdonFramework中文banq jdon
 
基于J2 Ee 的通用Web 信息系统框架设计与实现
基于J2 Ee 的通用Web 信息系统框架设计与实现基于J2 Ee 的通用Web 信息系统框架设计与实现
基于J2 Ee 的通用Web 信息系统框架设计与实现yiditushe
 
網站設計100步
網站設計100步網站設計100步
網站設計100步evercislide
 
前端MVC之backbone
前端MVC之backbone前端MVC之backbone
前端MVC之backboneJerry Xie
 
ASP.NET Core 2.1設計新思維與新發展
ASP.NET  Core 2.1設計新思維與新發展ASP.NET  Core 2.1設計新思維與新發展
ASP.NET Core 2.1設計新思維與新發展江華 奚
 
Exam 98-375 HTML5 Application Development Fundamentals
Exam 98-375 HTML5 Application Development FundamentalsExam 98-375 HTML5 Application Development Fundamentals
Exam 98-375 HTML5 Application Development FundamentalsChieh Lin
 
TBAD F2E 2010 review
TBAD F2E 2010 reviewTBAD F2E 2010 review
TBAD F2E 2010 reviewleneli
 
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練42015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4Duran Hsieh
 
ASP NET MVC
ASP NET MVC ASP NET MVC
ASP NET MVC leeju lee
 
Backbone.js and MVW 101
Backbone.js and MVW 101Backbone.js and MVW 101
Backbone.js and MVW 101Jollen Chen
 

Similaire à 设计模式MVC (20)

Struts学习笔记
Struts学习笔记Struts学习笔记
Struts学习笔记
 
ASP.Net MVC2 简介
ASP.Net MVC2 简介ASP.Net MVC2 简介
ASP.Net MVC2 简介
 
Asp.net mvc 培训
Asp.net mvc 培训Asp.net mvc 培训
Asp.net mvc 培训
 
Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有Asp.net mvc網站的從無到有
Asp.net mvc網站的從無到有
 
Vlog02 [eng sub]什麼是controller和如何在asp.net核心中創建controller?-what is controller ...
Vlog02  [eng sub]什麼是controller和如何在asp.net核心中創建controller?-what is controller ...Vlog02  [eng sub]什麼是controller和如何在asp.net核心中創建controller?-what is controller ...
Vlog02 [eng sub]什麼是controller和如何在asp.net核心中創建controller?-what is controller ...
 
Struts快速学习指南
Struts快速学习指南Struts快速学习指南
Struts快速学习指南
 
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit TestingASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
ASP.NET Core MVC 2.2從開發到測試 - Development & Unit Testing
 
ASP.NET MVC Code Templates實戰開發 -twMVC#4
 ASP.NET MVC Code Templates實戰開發 -twMVC#4 ASP.NET MVC Code Templates實戰開發 -twMVC#4
ASP.NET MVC Code Templates實戰開發 -twMVC#4
 
twMVC#04 | ASP.NET MVC - Code Templates實戰開發
twMVC#04 | ASP.NET MVC - Code Templates實戰開發twMVC#04 | ASP.NET MVC - Code Templates實戰開發
twMVC#04 | ASP.NET MVC - Code Templates實戰開發
 
JdonFramework中文
JdonFramework中文JdonFramework中文
JdonFramework中文
 
基于J2 Ee 的通用Web 信息系统框架设计与实现
基于J2 Ee 的通用Web 信息系统框架设计与实现基于J2 Ee 的通用Web 信息系统框架设计与实现
基于J2 Ee 的通用Web 信息系统框架设计与实现
 
網站設計100步
網站設計100步網站設計100步
網站設計100步
 
前端MVC之backbone
前端MVC之backbone前端MVC之backbone
前端MVC之backbone
 
ASP.NET Core 2.1設計新思維與新發展
ASP.NET  Core 2.1設計新思維與新發展ASP.NET  Core 2.1設計新思維與新發展
ASP.NET Core 2.1設計新思維與新發展
 
Exam 98-375 HTML5 Application Development Fundamentals
Exam 98-375 HTML5 Application Development FundamentalsExam 98-375 HTML5 Application Development Fundamentals
Exam 98-375 HTML5 Application Development Fundamentals
 
TBAD F2E 2010 review
TBAD F2E 2010 reviewTBAD F2E 2010 review
TBAD F2E 2010 review
 
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練42015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
2015 年逢甲大學資訊系:ASP.NET MVC 4 教育訓練4
 
ASP NET MVC
ASP NET MVC ASP NET MVC
ASP NET MVC
 
Backbone.js and MVW 101
Backbone.js and MVW 101Backbone.js and MVW 101
Backbone.js and MVW 101
 
Jsp
JspJsp
Jsp
 

设计模式MVC

  • 2. 提纲 What and Why 优缺点 实例分析 举一反三
  • 3. What is MVC? MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model)、视图(View)和控制器(Controller)。 目的:动态的程序设计 简化后续修改和扩展 复用代码 专一化(模块、人员)
  • 5. 控制器 负责转发请求,对请求进行处理 不同层面间的组织作用 控制应用程序的流程 处理事件并作出响应
  • 6. 模型 包含程序应有的功能(实现算法等) Bean/Service/DAO etc. 封装数据与逻辑 直接访问数据(DB) 不依赖“视图”和“控制器” 刷新机制
  • 8. 原始时代(JSP or Servlet) Password.htm <HTML> <BODY> <form action="PasswordGen.jsp" method="POST"> <H3>Welcome to the Password Generator</H3> First Name: <input type="text" name="firstName"><br> Last Name: <input type="text" name="lastName"><br> <p> <input type="submit" value="Generate Password"> </form> </BODY> </HTML>
  • 10. 原始时代(JSP or Servlet) PasswordGen.jsp <html> <body> <% String firstName = request.getParameter("firstName"); String lastName = request. getParameter("lastName"); String password; if( (firstName.length()>=2) && (lastName.length()>=2)) password = lastName.substring(0,2)+firstName.substring(0,2); else password = "NoGo"; %> <h1> Password Generated! </h1> Your super secret password is <%= password %>. <br><a href="Password.htm">To generate another password.</a> </body> </html>
  • 11. 原始时代(JSP or Servlet) PasswordGen.jsp <html> <body> <% String firstName = request.getParameter("firstName"); String lastName = request. getParameter("lastName"); String password; if( (firstName.length()>=2) && (lastName.length()>=2)) password = lastName.substring(0,2)+firstName.substring(0,2); else password = "NoGo"; %> <h1> Password Generated! </h1> Your super secret password is <%= password %>. <br><a href="Password.htm">To generate another password.</a> </body> </html> 看着都费劲,别说修改了╮(╯_╰)╭
  • 12. 缺点 职责多元 HTML代码和Java代码混杂(好眼花⊙﹏⊙) 修改逻辑or 修改呈现? 代码复用?(审核身份、日志记录) 团队开发困难
  • 13. MVC
  • 14. 优点 解耦 职责单一 分工明确(模块、人员) 代码复用 测试方便 对比 职责多元 HTML代码和Java代码混杂(好眼花) 修改逻辑 or 修改呈现? 增加服务? 代码复用?(审核身份、日志记录) 团队开发困难
  • 15. 缺点 比较抽象 没有明确的定义,难以完全理解 需要精心计划与设计,花费可观 更多文件,更多代码,增加了复杂度 这么复杂,为什么还要用? 弥补:框架
  • 17. Servlet and JSP Password.htm <HTML> <BODY> <form action="/webapp/passwordservlet" method="POST"> <H3>Welcome to the Password Generator</H3> First Name: <input type="text" name="firstName"><br> Last Name: <input type="text" name="lastName"><br> <p> <input type="submit" value="Generate Password"> </form> </BODY> </HTML>
  • 18. Servlet and JSP passwordServlet.java public void doPost(HttpServletRequest request, HttpServletResponse response) throws Exception { GenBeangb = new GenBean(); gb.setFirstName(request.getParameter("firstName")); gb.setLastName(request.getParameter("lastName")); gb.generate(); request.setAttribute("gen", gb); RequestDispatcherrd = getServletContext().getRequestDispatcher("/PasswordGen.jsp"); rd.forward(request, response); }
  • 19. Servlet and JSP GenBean.java public class GenBean { private String firstName; private String lastName; private String password = "NoGo"; public String getFirstName(){ return firstName; } public void setFirstName(String fn){ firstName = fn;} public String getLastName(){ return lastName; } public void setLastName(String ln){ lastName = ln; } public String getPassword(){return password;} public String generate() { if( (firstName.length() >= 2) && (lastName.length() >= 2)) password = lastName.substring(0,2) + firstName.substring(0,2); return password; } }
  • 20. Servlet and JSP PasswordGen.jsp <html> <body> <jsp:useBean id = "gen" class = “genpackage.GenBean" scope="request"/> <h1> Password Generated! </h1> Your super secret password is <jsp:getProperty name="gen" property="password"/> <br><a href="Password.htm">To generate another password.</a> </body> </html> 清晰了很多吧o(≧v≦)o~~
  • 21. 体现分发的Servlet控制器 verify.java public void doPost(HttpServletRequest request, HttpServletResponse response) throws Exception { String username = request.getParameter(“username")); String password = request.getParameter(“password")); boolean valid = new Security().verify(username, password); RequestDispatcherrd= null; if(valid) { rd= getServletContext().getRequestDispatcher("/valid.jsp"); } else { rd= getServletContext().getRequestDispatcher("/invalid.jsp"); } rd.forward(request, response); }
  • 22. 现在呈现和逻辑被分开了,真好呀 Servlet 1 JSP 1 Servlet 2 JSP 2 Servlet 3 JSP 3 有没有改进的余地?
  • 23. 如果把Servlet组合起来? Controller JSP 1 Model 1 JSP 2 Model 2 JSP 3 Model 3 关系是不是简单了许多?
  • 25. Model Action 1 Action 2 Action 3 Bean DAO Service Struts 2 XML配置 Controller Struts 2 Dispatcher 浏览器 Web容器 View JSP 1 JSP 2 DataBase
  • 26. web.xml <filter>         <filter-name>struts2</filter-name>         <filter-class>             org.apache.struts2.dispatcher.FilterDispatcher         </filter-class>     </filter>     <filter-mapping>         <filter-name>struts2</filter-name>         <url-pattern>/*</url-pattern>     </filter-mapping>
  • 27. Struts.xml <struts> <package name=“default” extends=“struts-default”> <action name=“hello” class=“HelloWorld”> <result name=“SUCCESS”> /HelloWorld.jsp </result> </action> </package> </struts>
  • 28. HelloWorld.java(POJO) public class HelloWorld { private String message; public String execute() Exception { message = “Hello,world”; message += “The time is:”; message += System.currentTimeMillis(); return “SUCCESS”; } public void setMessage(String message){this.message = message; } public String getMessage() { return message; }}
  • 29. HelloWorld.jsp <%@ taglib prefix="s" uri="/struts-tags" %><html><head><title>Hello World!</title></head><body><h2><s:property value="message" /></h2></body></html>