设计模式MVC

1,862 views

Published on

入门级讲解MVC设计模式,并以Model2模型和Struts2框架作为范例。

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,862
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
8
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

设计模式MVC

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

×