Struts2学习总结.ppt

上传人:max****ui 文档编号:10976823 上传时间:2020-04-16 格式:PPT 页数:56 大小:2.91MB
返回 下载 相关 举报
Struts2学习总结.ppt_第1页
第1页 / 共56页
Struts2学习总结.ppt_第2页
第2页 / 共56页
Struts2学习总结.ppt_第3页
第3页 / 共56页
点击查看更多>>
资源描述
项目开发的顺序 Struts2能做什么呢 Struts2的流程图 Struts2框架的完整工作流程 Struts2请求页面的工作流程 Inthediagram aninitialrequestgoestotheServletcontainer suchasJettyorResin whichispassedthroughastandardfilterchain Thechainincludesthe optional ActionContextCleanUpfilter whichisusefulwhenintegratingtechnologiessuchasSiteMeshPlugin Next therequiredFilterDispatcheriscalled whichinturnconsultstheActionMappertodetermineiftherequestshouldinvokeanaction IftheActionMapperdeterminesthatanActionshouldbeinvoked theFilterDispatcherdelegatescontroltotheActionProxy TheActionProxyconsultstheframeworkConfigurationFilesmanager initializedfromthestruts xmlfile Next theActionProxycreatesanActionInvocation whichisresponsibleforthecommandpatternimplementation ThisincludesinvokinganyInterceptors thebeforeclause inadvanceofinvokingtheActionitself OncetheActionreturns theActionInvocationisresponsibleforlookinguptheproperresultassociatedwiththeActionresultcodemappedinstruts xml Theresultisthenexecuted whichoften butnotalways asisthecaseforActionChaining involvesatemplatewritteninJSPorFreeMarkertoberendered Whilerendering thetemplatescanusetheStrutsTagsprovidedbytheframework SomeofthosecomponentswillworkwiththeActionMappertorenderproperURLsforadditionalrequests Interceptorsareexecutedagain inreverseorder callingtheafterclause Finally theresponsereturnsthroughthefiltersconfiguredintheweb xml IftheActionContextCleanUpfilterispresent theFilterDispatcherwillnotcleanuptheThreadLocalActionContext IftheActionContextCleanUpfilterisnotpresent theFilterDispatcherwillcleanupallThreadLocals 其工作流程 模拟Struts2工作原理 创建javaweb工程 模拟Struts2工作原理 客户端有两个请求链接 test jspsuserWorld 模拟Struts2工作原理 分别由两个action来处理 模拟Struts2工作原理 客户端请求提交给Servlet或过滤器来处理 publicvoiddoFilter ServletRequestrequest ServletResponseresponse FilterChainchain throwsIOException ServletException HttpServletRequestreq HttpServletRequest request HttpServletResponseres HttpServletResponse response Stringpath req getServletPath System out println path path if test jsp equals path chain doFilter req res else ActionuserAction newUserAction userAction execute req getRequestDispatcher success jsp forward req res 模拟Struts2工作原理 客户端请求提交给Servlet或过滤器来处理 publicvoiddoFilter ServletRequestrequest ServletResponseresponse FilterChainchain throwsIOException ServletException HttpServletRequestreq HttpServletRequest request HttpServletResponseres HttpServletResponse response Stringpath req getServletPath System out println path path if test jsp equals path chain doFilter req res else ActionuserAction Action Class forName cn itcast action UserAction newInstance userAction execute req getRequestDispatcher success jsp forward req res 利用Class forName 方法取得类的完整路径 然后在利用newInstance 方法创建一个实例 模拟Struts2工作原理 客户端请求提交给Servlet或过滤器来处理 privateMapmap newHashMap publicvoidinit FilterConfigfilter throwsServletException map put primer userAction action cn itcast action UserAction map put helloWorld helloAction action cn itcast action HelloAction try ActionuserAction Action Class forName map get path newInstance userAction execute req getRequestDispatcher success jsp forward req res catch InstantiationExceptione e printStackTrace catch IllegalAccessExceptione e printStackTrace catch ClassNotFoundExceptione e printStackTrace 用Map保存客户端请求链接和要访问action的对应值 模拟Struts2工作原理 publicclassStruts2FilterimplementsFilter privateMapmap newHashMap publicvoidinit FilterConfigfilter throwsServletException map put primer userAction action cn itcast action UserAction map put helloWorld helloAction action cn itcast action HelloAction 如果现在客户端请求再增加一个链接 这个时候是不是需要在Struts2Filter类中修改init 方法 考虑是否可以增加一个配置文件 将这些请求链接和action的对应值保存在配置文件中 模拟Struts2工作原理 增加一个struts xml的配置文件 publicvoidinit FilterConfigfilter throwsServletException 解析struts xml文件 放置xml文件到信息集合中 struts2框架实现map put primer userAction action cn itcast action UserAction map put helloWorld helloAction action cn itcast action HelloAction Struts2框架中哪些是需要我们来编写的 Struts2的基本配置 访问HelloWorld应用的路径的设置 在struts2中 访问struts2中action的URL路径由两部份组成 包的命名空间 action的名称例如 访问本例子HelloWorldAction的URL路径为 primer helloWorldAction action 注意 完整路径为 http localhost 端口 内容路径 primer helloWorldAction action 另外我们也可以加上 action后缀访问此Action success jsp Action名称的搜索顺序 1 获得请求路径的URI 例如url是 http server struts2 path1 path2 path3 test action2 首先寻找namespace为 path1 path2 path3的package 如果存在这个package 则在这个package中寻找名字为test的action 如果不存在这个package则转步骤3 3 寻找namespace为 path1 path2的package 如果存在这个package 则在这个package中寻找名字为test的action 如果不存在这个package 则转步骤4 4 寻找namespace为 path1的package 如果存在这个package 则在这个package中寻找名字为test的action 如果仍然不存在这个package 就去默认的namaspace的package下面去找名字为test的action 默认的命名空间为空字符串 如果还是找不到 页面提示找不到action Action配置中的各项默认值 问题 如果没有为action指定class 默认是com opensymphony xwork2 ActionSupport执行ActionSupport中的execute方法由struts default xml文件决定 success jsp success jsp1 如果没有为action指定class 默认是ActionSupport 2 如果没有为action指定method 默认执行action中的execute 方法 ActionSupport的execute方法里面就一句话return success 3 如果没有指定result的name属性 默认值为success Action配置中的各项默认值 问题 如果请求的路径查找不到action的情况下 程序运行会抛出异常 可以通过配置当找不到action的情况下 会执行默认的action success jsp success jsp ActionSupport 类是默认的Action类 在编写Action类时 通常会对这个类进行扩展 Struts2处理的请求后缀 StrutsPrepareAndExecuteFilter是Struts2框架的核心控制器 它负责拦截由 指定的所有用户请求 当用户请求到达时 该Filter会过滤用户的请求 默认情况下 如果用户请求的路径不带后缀或者后缀以 action结尾 这时请求将被转入Struts2框架处理 否则Struts2框架将略过该请求的处理 根据配置文件 struts2 core 2 1 8 1 jar包下的org apache struts2 default properties文件定义的常量决定struts action extension action 默认处理的后缀是可以通过常量 struts action extension 进行修改的 如下面配置Struts2只处理以 do为后缀的请求路径 如果用户需要指定多个请求后缀 则多个后缀之间以英文逗号 隔开 如 细说常量定义 常量可以在struts xml或struts properties中配置 建议在struts xml中配置 两种配置方式如下 在struts xml文件中配置常量在struts properties中配置常量 struts properties文件放置在src下 struts action extension do go因为常量可以在多个配置文件中进行定义 所以我们需要了解下struts2加载常量的搜索顺序 1struts default xml2struts plugin xml3struts xml4struts properties 自己创建 5web xml如果在多个文件中配置了同一个常量 则后一个文件中配置的常量值会覆盖前面文件中配置的常量值 常用的常量介绍 指定默认编码集 作用于HttpServletRequest的setCharacterEncoding方法和freemarker velocity的输出该属性指定需要Struts2处理的请求后缀 该属性的默认值是action 即所有匹配 action的请求都由Struts2处理 如果用户需要指定多个请求后缀 则多个后缀之间以英文逗号 隔开设置浏览器是否缓存静态内容 默认值为true 生产环境下使用 开发阶段最好关闭配置当国际化文件修改时 重新加载该国际化资源文件 默认值是false 不重新加载 true为重新加载当struts的配置文件修改后 系统是否自动重新加载该文件 默认值是false 不重新加载 true为重新加载开发模式下使用 这样可以打印出更详细的错误信息 默认值为false 生产环境下使用 开发阶段最好打开默认的视图主题与spring集成时 指定由spring负责action对象的创建该属性设置Struts2是否支持动态方法调用 该属性的默认值是true 如果需要关闭动态方法调用 则可设置该属性为false上传文件的大小限制 指定多个struts配置文件 在大部分应用里 随着应用规模的增加 系统中Action的数量也会大量增加 导致struts xml配置文件变得非常臃肿 为了避免struts xml文件过于庞大 臃肿 提高struts xml文件的可读性 我们可以将一个struts xml配置文件分解成多个配置文件 然后在struts xml文件中包含其他配置文件 下面的struts xml通过元素指定多个配置文件 通过这种方式 我们就可以将Struts2的Action按模块添加在多个配置文件中 Struts2的结果类型 结果类型转发举例 action中增加 HttpServletRequestrequest ServletActionContext getRequest request setAttribute username username success jsp页面中增加 resulttype requestScope username 得出结果 在success jsp页面中得到username的值 resulttype username result 每个action方法都将返回一个String类型的值 Struts将根据这个值来决定响应什么结果 每个Action声明都必须包含有数量足够多的result元素 每个result元素分别对应着action方法的一个返回值 result元素可以有下面两个属性name 结果的名字 必须与Action方法的返回值相匹配 默认值为successtype 响应结果的类型 默认值为dispatcher 北京传智播客教育 结果类型 在struts2 core 2 3 3 jar包下的struts default xml配置文件 结果类型 dispatcher dispatcher结果类型是最常用的结果类型 也是struts框架默认的结果类型该结果类型有一个location参数 它是一个默认参数查看API文档org apache struts2 dispatcher ServletDispatcherResultdispatcher结果类型将把控制权转发给应用程序里的某个资源 dispatcher结果类型不能把控制权转发给一个外部资源 若需要把控制权重定向到一个外部资源 应该使用redirect结果类型 等同 结果类型 dispatcher的两种写法 resulttype success jsp resulttype success jsp 结果类型 dispatcher的底层代码说明 在struts default xml文件中找到ServletDispatcherResult actionpublicclassServletDispatcherResultextendsStrutsResultSupport publicvoiddoExecute StringfinalLocation ActionInvocationinvocation throwsException RequestDispatcherdispatcher request getRequestDispatcher finalLocation dispatcher forward request response StrutsResultSupport actionpublicabstractclassStrutsResultSupportimplementsResult StrutsStatics publicvoidsetLocation Stringlocation this location location 结果类型 redirect redirect结果类型将把响应重定向到另一个资源 而不是转发给该资源 redirect结果类型接受下面这些参数 location 用来给出重定向的目的地parse 用来表明是否把location参数的值视为一个OGNL表达式来解释 默认值为trueredirect结果类型可以把响应重定向到一个外部资源struts default xml中的 等同 结果类型 redirect的底层代码说明 在struts default xml文件中找到ServletRedirectResult actionpublicclassServletRedirectResultextendsStrutsResultSupportimplementsReflectionExceptionHandler protectedvoiddoExecute StringfinalLocation ActionInvocationinvocation throwsException sendRedirect response finalLocation protectedvoidsendRedirect HttpServletResponseresponse StringfinalLocation throwsIOException response sendRedirect finalLocation StrutsResultSupport actionpublicabstractclassStrutsResultSupportimplementsResult StrutsStatics publicvoidsetLocation Stringlocation this location location 北京传智播客教育 结果类型 redirectAction redirectAction结果类型把响应重定向到另一个ActionredirectAction结果类型接受下面这些参数 actionName 指定 目的地 动作的名字 它是默认属性namespace 用来指定 目的地 动作的命名空间 如果没有配置该参数 Struts会把当前Action所在的命名空间作为 目的地 的命名空间 查看API文档 结果类型 redirectAction的底层代码说明 在struts default xml文件中找到ServletActionRedirectResult actionpublicclassServletActionRedirectResultextendsServletRedirectResult publicvoidsetActionName StringactionName this actionName actionName publicvoidsetNamespace Stringnamespace this namespace namespace ServletRedirectResult actionpublicclassServletRedirectResultextendsStrutsResultSupportimplementsReflectionExceptionHandler protectedvoiddoExecute StringfinalLocation ActionInvocationinvocation throwsException sendRedirect response finalLocation protectedvoidsendRedirect HttpServletResponseresponse StringfinalLocation throwsIOException response sendRedirect finalLocation 通配符和动态方法调用 通配符举例 BookAction publicclassBookActionextendsActionSupport publicStringexecute throwsException System out println BookAction execute returnnull 显示图书添加页面 publicStringadd System out println 显示图书添加页面 return add 通配符举例 struts pattern xml pattern BookAction jsp 通配符举例 BookAction如何自定义方法 在action中增加新的方法的要求 方法最好要用public修饰方法的名称自定义方法没有参数方法的返回值为String 要定义的方法就是与execute方法的方法名称不同 其他的都一样 只有这样的定义方法struts2才识别 通配符映射 一个Web应用可能有成百上千个action声明 可以利用struts提供的通配符映射机制把多个彼此相似的映射关系简化为一个映射关系通配符映射规则若找到多个匹配 没有通配符的那个将胜出若指定的动作不存在 Struts将会尝试把这个URI与任何一个包含着通配符 的动作名及进行匹配若Struts找到的带有通配符的匹配不止一个 最后一个匹配将胜出被通配符匹配到的URI字符串的子串可以用 1 2 来引用 1 匹配第一个子串 2 匹配第二个子串 0 匹配整个URI 可以匹配零个或多个字符 但不包括 字符 如果想把 字符包括在内 需要使用 如果需要对某个字符进行转义 需要使用 通配符映射示例 1 包声明 上面的包声明可以由正确的命名空间和 add组成的URI来调用 pattern BookAction jsp pattern BookAction jsp pattern BookAction jsp 通配符映射示例 2 包声明 上面的包可改写为 通配符映射示例 3 包声明 上面的包可改写为 动态方法调用 动态方法调用 通过url动态调用Action中的方法如果Action中存在多个方法时 我们可以使用 方法名调用指定方法默认情况下 Struts的动态方法调用处于激活状态 若想禁用该功能 则可以在struts xml文件中添加如下constant元素 Action的配置 Jsp页面的代码 使用通配符定义action Action的配置 Jsp页面的代码 开发中比较常用这种方式调用action中的方法 全局结果 当多个action中都使用到了相同result 这时我们应该把result定义为全局结果 message jsp注 局部的会覆盖全局全局结果和局部结果的区别 全局结果 对该包下所有的action配置起作用 局部结果 只对当前的action起作用Struts2中应用范围内action的实例 每个请求都会创建一个action实例 Interceptor的工作流程 Struts2的流程图 Struts2的处理流程 StrutsPrepareAndExecuteFilter Interceptor Action Result Jsp html 用户请求 Struts2内置的一些拦截器或用户自定义拦截器 用户编写的action类 类似struts1中的Action 类似struts1中的forward 响应 Struts2的拦截器举例 如果写一段程序来完成一项功能的话 有可能是这样做的 classA aa 第1步 第2步 第3步 第4步 Struts2的拦截器举例 classA aa 第1步 第2步 第3步 第4步 classA1 第1步 classA2 第2步 classA3 第3步 classA4 第4步 分离关注 Struts2的拦截器中的分离关注 把过滤器要完成的事情委托给多个类完成 这种观点就是分离关注 过滤器负责调用这些类 处理Cookies的拦截器 处理web中的cookies 令牌拦截器 处理表单重复提交 参数拦截器文件上传拦截器i18n拦截器 处理国际化 自定义拦截器 拦截器和过滤器的区别 相同点都是拦截作用不同点过滤器是J2EE中的规范 任何javaWeb程序都可以使用拦截器是Struts2框架独有的 离开了Struts2框架 拦截器将不能使用 拦截器是依赖于Struts2框架的完成的功能不同过滤器负责拦截请求的路径 解析xml文件其他拦截功能交给拦截器处理调用顺序 过滤器 拦截器 拦截器概述 struts default xml 拦截器概述 演示过滤器和拦截器的执行顺序 使用如下三个拦截器演示struts的执行流程 断点演示 默认的是defaultStack 在栈中的顺序如下
展开阅读全文
相关资源
相关搜索

当前位置:首页 > 图纸专区 > 课件教案


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!