Struts课程-5.ppt_第1页
Struts课程-5.ppt_第2页
Struts课程-5.ppt_第3页
Struts课程-5.ppt_第4页
Struts课程-5.ppt_第5页
免费预览已结束,剩余31页可下载查看

下载本文档

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

文档简介

Struts教学 第五课 Author huangjyemail hjyonline 本课内容 讲解高级Action部分讲解strutsException部分数据源配置部分数据库访问试验 Author huangjyemail hjyonline 高级Action部分 基于一个radio hidden或按钮执行不同逻辑 但不重复检查exectue方法中的参数名称 使用DispatchAction在非struts应用中使用Strutsform bean 在struts中不撤销代码 使用ForwardAction其他Actionsubclasses Author huangjyemail hjyonline DispatchAction 假定 同一个form因为某个radio是否被选定 或者哪个按钮被按下导致需要执行不同逻辑但是 在Html中一个form只有一个Action 几个Actions共享类似的或相同的方法问题 需要冗余的和重复的检查参数才能知道实际上是哪个方法被调用Formbeanradio按钮必须有getter setters目标 基于struts config xml 自动地指派逻辑 决定哪个方法被调用 Author huangjyemail hjyonline 例如 radio名称是 operation publicActionForwardexecute ActionMappingmapping ActionFormform HttpServletRequestrequest HttpServletResponseresponse throwsException UserFormBeanformBean UserFormBean form if radioButtonMatches formBean createAccount return createAccount mapping formBean request response elseif radioButtonMatches formBean changePassword return changePassword mapping formBean request response elseif radioButtonMatches formBean deleteAccount return deleteAccount mapping formBean request response else return makeErrorMessage privatebooleanradioButtonMatches UserFormBeanformBean Stringvalue Stringoperation formBean getOperation return operation null Author huangjyemail hjyonline 使用DispatchAction 使用struts config xml列出需要用来决定哪个方法被调用的参数 使用action的parameter属性继承DispatchAction而不是Action 直接实现需要的方法 彻底省略execute方法 自定义方法 和execute方法有相同的修饰 注意包是 struts actions 不是 struts action在form中 提供给定名字的参数 值将是方法的名称Formbean需要没有为button提供setter方法 Author huangjyemail hjyonline 举例 通过选择不同的radio执行不同的操作在所有Action子类中使用相同的基本Action Author huangjyemail hjyonline 流程说明 用户请求一个formForm有一个指定名字的radio 按钮或隐藏域 它的值和方法名对应form被提交到URLXXX do 地址在struts config xml被映射的一个Action类Action对象的指定方法被调用 方法是根据指定表单参数被选择 被执行方法的其中一个参数自动被创建并自动被扶植的formbean 根据request Action对象然后调用业务逻辑和数据访问逻辑 将结果放入普通的beans中 并设置到request session或者application Action使用mapping findForward返回条件 并且条件被在struts config xml中映射到对应的JSP页面 Author huangjyemail hjyonline 需要注意的地方 Form有个指定参数值和对应方法名相同Parameter在struts config xml中声明Formbean对这个特别参数不要提供setter 需要getter继承DispatchAction而不是Action 没有execute方法 其中一个方法名是parameter的值方法和execute具有相同的修饰 Author huangjyemail hjyonline 具体代码示例 Author huangjyemail hjyonline 使用DispatchAction举例 修改Struts config xml Author huangjyemail hjyonline 使用DispatchAction举例 importorg apache struts action publicclassUserFormBeanextendsActionForm privateStringemail privateStringpassword privateStringoperation createAccount publicStringgetEmail return email publicvoidsetEmail Stringemail this email email publicStringgetOperation return operation Author huangjyemail hjyonline 使用DispatchAction举例 定义Resultbean 省略 Author huangjyemail hjyonline 使用DispatchAction举例 定义DispatchActionpublicclassModifyAccountActionextendsDispatchAction publicActionForwardcreateAccount ActionMappingmapping ActionFormform HttpServletRequestrequest HttpServletResponseresponse throwsException if isComplexBusinessLogicSuccessful create return mapping findForward create success else return mapping findForward create failed publicActionForwardchangePassword publicActionForwarddeleteAccount privatebooleanisComplexBusinessLogicSuccessful Stringtype return Math random 0 5 Author huangjyemail hjyonline 使用DispatchAction举例 创建表单调用XXX doEmailaddress Password CreateAccountChangePasswordDeleteAccount Author huangjyemail hjyonline 使用DispatchAction举例 创建显示结果的jspAccountCreatedAccountsuccessfullycreatedfor Congratulations Author huangjyemail hjyonline ForwardAction 场景 你已经使用基于mvc的一套程序 其中用了javabean在servlet中频繁的使用request getParameter 已经有一套独立的并且使用了beans的jsp页面使用脚本元素或者jsp setProperty 问题 冗余和重复的request getParameter 现在还不准备全部struts重新改写 目标 使用Strutsformbeans功能 保留原有servlet JSP结构 Author huangjyemail hjyonline 使用forwardAction的步骤 修改bean 需要继承ActionForm 不需要实现Serializable 不需要检查null或者空字符串修改servlet 不需要创建bean 也不需要检查Request Session Application是否为null 不需要调用request getParameter修改form ACTION将指向XXX do Author huangjyemail hjyonline 使用forwardAction的步骤 在struts config xml中声明bean 和一般的formbean声明相同在struts config xml中声明action Author huangjyemail hjyonline Exception部分 程序化处理异常使用trycatch语句声明异常处理入口在struts config xml中 指定当错误发生的时候页面如何显示 当错误发生时 自定义页面被请求 Struts只在Action中抛出异常的时候才会发出请求 Author huangjyemail hjyonline Exception配置说明 exception四个元素的可能属性 key 资源文件的访问入口 用来创建一个ActionMessage 可以用html errors输出其具体内容 type exception的全名 path 错误页面的相对路径 handler 用来处理异常的类 通常被省略 使用默认的handler 自定义的exception的时候一般才需要handler Author huangjyemail hjyonline Exception使用举例1 页面和配置 struts config xmlnull jspNullPointerExceptionNullPointerExceptionBlah blah blah blah Author huangjyemail hjyonline Exception使用举例1 bean定义 packagecom sinojava importorg apache struts action publicclassColorBeanextendsActionForm privateStringforegroundColor backgroundColor publicStringgetForegroundColor return foregroundColor publicvoidsetForegroundColor StringfgColor if fgColor equals foregroundColor fgColor Author huangjyemail hjyonline Exception使用举例1 Action定义 publicclassShowColorsActionextendsAction publicActionForwardexecute ActionMappingmapping ActionFormform HttpServletRequestrequest HttpServletResponseresponse throwsException ColorBeancolorBean ColorBean form Stringfg colorBean getForegroundColor Stringbg colorBean getBackgroundColor if fg equals bg colorBean setForegroundColor BLACK colorBean setBackgroundColor WHITE return mapping findForward success Author huangjyemail hjyonline 处理一般异常 struts config xmlerror jspErrorErrorBlah blah blah blah Author huangjyemail hjyonline 处理一般异常 publicclassShowColorsActionextendsAction publicActionForwardexecute ActionMappingmapping ActionFormform HttpServletRequestrequest HttpServletResponseresponse throwsException ColorBeancolorBean ColorBean form Stringfg colorBean getForegroundColor Stringbg colorBean getBackgroundColor if fg equals bg colorBean setForegroundColor BLACK colorBean setBackgroundColor WHITE if bg equalsIgnoreCase papayawhip throw newException Bogusbackgroundcolor return mapping findForward success Author huangjyemail hjyonline 一般异常另一种处理方法 在web xml中定义404 WEB INF NotFound jsp Author huangjyemail hjyonline 使用自定义Exception处理器 ExceptionHandler的定义 publicclassCustomExceptionHandlerextendsExceptionHandler publicActionForwardexecute Exceptionexception ExceptionConfigconfig ActionMappingmapping ActionFormform HttpServletRequestrequest HttpServletResponseresponse throwsServletException Author huangjyemail hjyonline 使用自定义Exception处理器 处理内容可以记录日志 发送邮件等 Author huangjyemail hjyonline 使用自定义Exception处理器 自定义exceptionpublicclassCriticalProblemextendsException publicCriticalProblem Stringmessage super message Author huangjyemail hjyonline 在程序中 Action中 抛出异常 publicclassShowColorsActionextendsAction publicActionForwardexecute throwsException if bg equalsIgnoreCase papayawhip throw newException Bogusbackgroundcolor if Math random 0 1 Stringmessage Databaseserverexploded Buildingisburning throw newCriticalProblem message return mapping findForward success Author huangjyemail hjyonline Struts的jdbc连接池 数据源AJDBCDataSourceisanobjectdescribedby

温馨提示

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

评论

0/150

提交评论