第章 MVC入门_第1页
第章 MVC入门_第2页
第章 MVC入门_第3页
第章 MVC入门_第4页
第章 MVC入门_第5页
已阅读5页,还剩18页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

9 MVC 第 章 目标 掌握ModelI体系结构掌握ModelII体系结构掌握MVC应用程序 ModelI体系结构1 ModelI体系结构结合使用JSP页面和JavaBean来开发Web应用程序 应用服务器 响应 JSP页面 JavaBean 数据库服务器 ModelI体系结构2 ModelI体系结构用于开发简单的应用程序 ModelI体系结构包括多个用户可与之交互的页面 客户端能够直接访问加载到服务器上的页面 ModelIWeb应用程序由复杂的Web逻辑组成 并链接至Web应用程序的其他页面 ModelI体系结构3 本示例演示ModelI体系结构 客户输入产品ID时 显示相关产品信息 publicvoidsetProductname Stringprodname this productname prodname publicStringgetProductname returnproductname publicvoidsetProducttype Stringprodtype this producttype prodtype publicStringgetProducttype returnproducttype 这是一个JavaBean 它为产品名称和类型定义了各种方法 ModelI体系结构4 publicclassColProduct privateConnBeancBean ConnBean newInstance publicCollectiongetProduct Stringsql Collectioncol newArrayList try con cBean getConnDB stmt con createStatement rs stmt executeQuery sql while rs next ProductBeanproduct newProductBean product setProductname rs getString 2 product setProducttype rs getString 3 product setProductprice rs getInt 4 product setBrand rs getString 5 product setDescription rs getString 6 col add product 这个JavaBean将数据通过ProductBean放入集合ArrayList ModelI体系结构5 publicclassConnBean Connectioncon null privatestaticConnBeanme newConnBean publicstaticConnBeannewInstance returnme publicConnectiongetConnDB try Class forName sun jdbc odbc JdbcOdbcDriver newInstance con DriverManager getConnection jdbc odbc accp returncon 这个JavaBean实现数据库连接 ModelI体系结构6 这个JSP页面包含各JSP操作 可以用来访问JavaBean组件对象 ModelII体系结构1 ModelI体系结构结合使用JSP页面和JavaBean来开发Web应用程序 ModelII体系结构2 设计模式为软件设计问题提供标准的解决方案 ModelII是一种设计模式 MVC将交互式应用程序组织成三个独立的模块 应用程序模块 用于表示数据和业务逻辑 数据表示 控制器 用于发送请求和控制应用程序的流程 ModelII体系结构3 ModelII体系结构的优点 可以很灵活地设计和实现模型对象 使用同一模型处理多个请求 增加新客户端更容易 MVC的优点 ModelII体系结构4 MVC体系结构的各种对象包括 MVC对象 控制器 模型 视图 ModelII体系结构5 模型对象表示Web应用程序的数据元素 用户可与这些数据元素进行交互 控制器对象充当应用程序视图对象和模型对象之间的中间对象 视图对象提供模型组件以开发用户界面 ModelII体系结构的各种对象包括 基于MVC体系结构的应用程序9 1 MVC体系结构遵循分层方式 用户使用该方式可将代码分别写入不同的类和方法中 以一个简单论坛系统的部分功能为例 加以理解 标题 这个JSP提供图形界面用于客户发新贴 演示 示例 基于MVC体系结构的应用程序9 2 publicclassnewtopicservletextendsHttpServlet publicvoiddoGet HttpServletRequestrequest HttpServletResponseresponse throwsServletException IOException request setCharacterEncoding GBK Stringtitle request getParameter title DataBaseConndbBean DataBaseConn newInstance Connectioncon dbBean getConnDB Statementstmt con createStatement 把视图派发到viewForum jsp javax servlet RequestDispatcherdis request getRequestDispatcher viewforum jsp dis forward request response 这个Servlet转发视图 基于MVC体系结构的应用程序9 3 首页 上一页 示例查看论坛的内容 并且进行分页显示 视图对象 基于MVC体系结构的应用程序9 4 publicclassForumServletextendsHttpServlet PageBeanpage1 newPageBean PageBeanpage2 page1 getResult String request getParameter jumpPage 把PageBean保存到request对象中 request setAttribute page2 page2 把视图派发到view jsp javax servlet RequestDispatcherdis request getRequestDispatcher view jsp dis forward request response 控制器对象 基于MVC体系结构的应用程序9 5 publicintgetMaxPage returnthis maxPage publicvoidsetCurPage intcurPage this curPage curPage while rset next if i pageNum 1 pageBean rowsPerPage 1 Object obj newObject 3 obj 0 rset getString 2 publicvoidsetPageBean throwsException 得到总行数this setMaxRowCount this getAvailableCount if this maxRowCount this rowsPerPage 0 根据总行数计算总页数this maxPage this maxRowCount this rowsPerPage else this maxPage this maxRowCount this rowsPerPage 1 模型对象 基于MVC体系结构的应用程序9 6 publicclassDataBaseConn Connectioncon null privatestaticDataBaseConnme newDataBaseConn publicstaticDataBaseConnnewInstance returnme publicConnectiongetConnDB try Class forName sun jdbc odbc JdbcOdbcDriver newInstance con DriverManager getConnection jdbc odbc accp publicDataBaseConn 演示 示例 数据库连接 总结1 ModelI和ModelII体系结构用于开发Web应用程序在ModelI体系结构中 JSP页面单独负责开发Web应用程序ModelIWeb应用程序由复杂的Web逻辑和指向Web应用程序中其他页面的链接组成ModelI体系结构是一个很费时费力的过程ModelI体系结构提供的安全功能也很有限为了克服ModelI体系结构的缺陷 引入了ModelII体系结构 总结2 ModelI

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论