Struts2学习总结.ppt
《Struts2学习总结.ppt》由会员分享,可在线阅读,更多相关《Struts2学习总结.ppt(56页珍藏版)》请在三一办公上搜索。
1、项目开发的顺序,Struts2能做什么呢,Struts2的流程图,Struts2框架的完整工作流程,Struts2请求页面 的工作流程,In the diagram,an initial request goes to the Servlet container(such as Jetty or Resin)which is passed through a standard filter chain.The chain includes the(optional)ActionContextCleanUp filter,which is useful when integrating tech
2、nologies such as SiteMesh Plugin.Next,the required FilterDispatcher is called,which in turn consults the ActionMapper to determine if the request should invoke an action.If the ActionMapper determines that an Action should be invoked,the FilterDispatcher delegates control to the ActionProxy.The Acti
3、onProxy consults the framework Configuration Files manager(initialized from the struts.xml file).Next,the ActionProxy creates an ActionInvocation,which is responsible for the command pattern implementation.This includes invoking any Interceptors(the before clause)in advance of invoking the Action it
4、self.Once the Action returns,the ActionInvocation is responsible for looking up the proper result associated with the Action result code mapped in struts.xml.The result is then executed,which often(but not always,as is the case for Action Chaining)involves a template written in JSP or FreeMarker to
5、be rendered.While rendering,the templates can use the Struts Tags provided by the framework.Some of those components will work with the ActionMapper to render proper URLs for additional requests.Interceptors are executed again(in reverse order,calling the after clause).Finally,the response returns t
6、hrough the filters configured in the web.xml.If the ActionContextCleanUp filter is present,the FilterDispatcher will not clean up the ThreadLocal ActionContext.If the ActionContextCleanUp filter is not present,the FilterDispatcher will cleanup all ThreadLocals.,其工作流程,模拟Struts2工作原理,创建javaweb工程,模拟Stru
7、ts2工作原理,客户端有两个请求链接,test.jspsuserWorld,模拟Struts2工作原理,分别由两个action来处理,模拟Struts2工作原理,客户端请求提交给Servlet或过滤器来处理,public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletException HttpServletRequest req=(HttpServletRequest)request;HttpServletResponse re
8、s=(HttpServletResponse)response;String path=req.getServletPath();System.out.println(path=+path);if(/test.jsp.equals(path)chain.doFilter(req,res);else Action userAction=new UserAction();userAction.execute();req.getRequestDispatcher(/success.jsp).forward(req,res);,模拟Struts2工作原理,客户端请求提交给Servlet或过滤器来处理,
9、public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletException HttpServletRequest req=(HttpServletRequest)request;HttpServletResponse res=(HttpServletResponse)response;String path=req.getServletPath();System.out.println(path=+path);if(/test
10、.jsp.equals(path)chain.doFilter(req,res);else Action userAction=(Action)Class.forName(cn.itcast.action.UserAction).newInstance();userAction.execute();req.getRequestDispatcher(/success.jsp).forward(req,res);,利用Class.forName()方法取得类的完整路径,然后在利用newInstance()方法创建一个实例,模拟Struts2工作原理,客户端请求提交给Servlet或过滤器来处理,p
11、rivate Map map=new HashMap();public void init(FilterConfig filter)throws ServletException map.put(/primer/userAction.action,cn.itcast.action.UserAction);map.put(/helloWorld/helloAction.action,cn.itcast.action.HelloAction);,try Action userAction=(Action)Class.forName(map.get(path).newInstance();userA
12、ction.execute();req.getRequestDispatcher(/success.jsp).forward(req,res);catch(InstantiationException e)e.printStackTrace();catch(IllegalAccessException e)e.printStackTrace();catch(ClassNotFoundException e)e.printStackTrace();,用Map保存客户端请求链接和要访问action的对应值,模拟Struts2工作原理,public class Struts2Filter imple
13、ments Filter private Map map=new HashMap();public void init(FilterConfig filter)throws ServletException map.put(/primer/userAction.action,cn.itcast.action.UserAction);map.put(/helloWorld/helloAction.action,cn.itcast.action.HelloAction);,如果现在客户端请求再增加一个链接,这个时候是不是需要在Struts2Filter类中修改init()方法?,考虑是否可以增加一
14、个配置文件,将这些请求链接和action的对应值保存在配置文件中?,模拟Struts2工作原理,增加一个struts.xml的配置文件,public void init(FilterConfig filter)throws ServletException/解析struts.xml文件,放置xml文件到信息集合中-struts2框架实现 map.put(/primer/userAction.action,cn.itcast.action.UserAction);map.put(/helloWorld/helloAction.action,cn.itcast.action.HelloAction
15、);,Struts2框架中哪些是需要我们来编写的?,Struts2的基本配置,访问HelloWorld应用的路径的设置,在struts2中,访问struts2中action的URL路径由两部份组成:包的命名空间+action的名称例如:访问本例子HelloWorldAction的URL路径为:/primer/helloWorldAction.action(注意:完整路径为:http:/localhost:端口/内容路径/primer/helloWorldAction.action)。另外我们也可以加上.action后缀访问此Action。/success.jsp,Action名称的搜索顺序,1
16、获得请求路径的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,如
17、果存在这个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.jsp 1如果没
18、有为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 类.在编写 Ac
19、tion 类时,通常会对这个类进行扩展,Struts 2处理的请求后缀,StrutsPrepareAndExecuteFilter是Struts 2框架的核心控制器,它负责拦截由/*指定的所有用户请求,当用户请求到达时,该Filter会过滤用户的请求。默认情况下,如果用户请求的路径不带后缀或者后缀以.action结尾,这时请求将被转入Struts 2框架处理,否则Struts 2框架将略过该请求的处理。根据配置文件:struts2-core-2.1.8.1.jar包下的 org.apache.struts2/default.properties文件定义的常量决定 struts.action.e
20、xtension=action,默认处理的后缀是可以通过常量”struts.action.extension“进行修改的,如下面配置Struts 2只处理以.do为后缀的请求路径:如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。如:,细说常量定义,常量可以在struts.xml或struts.properties中配置,建议在struts.xml中配置,两种配置方式如下:在struts.xml文件中配置常量 在struts.properties中配置常量,(struts.properties文件放置在src下)struts.action.extension=do.go因为常量
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Struts2 学习 总结
链接地址:https://www.31ppt.com/p-6521435.html