- 浏览: 62733 次
- 性别:
- 来自: 北京
最新评论
-
wucaifang819787:
你好!麻烦问下不知道哪个图片行不行的:http://dl.it ...
struts2源码浅析(四) -
ChenXzh:
高手,佩服得五体投地
关于struts2报There is no Action mapped for namespace / and action name xxx_xxx
接上篇http://mazhiyuan.iteye.com/blog/1202086,本文着重dispatcher和filter的探讨。
Dispatcher已经在之前讲过,FilterDispatcher是Struts2的核心控制器,首先看一下init()方法。
public void init(FilterConfig filterConfig) throws ServletException { try { this.filterConfig = filterConfig; initLogging(); //创建dispatcher,前面都已经讲过啰 dispatcher = createDispatcher(filterConfig); dispatcher.init(); //注入将FilterDispatcher中的变量通过container注入,如下面的staticResourceLoader dispatcher.getContainer().inject(this); //StaticContentLoader在BeanSelectionProvider中已经被注入了依赖关系:DefaultStaticContentLoader //可以在struts-default.xml中的<bean>可以找到 staticResourceLoader.setHostConfig(new FilterHostConfig(filterConfig)); } finally { ActionContext.setContext(null); } } //下面来看DefaultStaticContentLoader的setHostConfig public void setHostConfig(HostConfig filterConfig) { //读取初始参数pakages,调用parse(),解析成类似/org/apache/struts2/static,/template的数组 String param = filterConfig.getInitParameter("packages"); //"org.apache.struts2.static template org.apache.struts2.interceptor.debugging static" String packages = getAdditionalPackages(); if (param != null) { packages = param + " " + packages; } this.pathPrefixes = parse(packages); initLogging(filterConfig); }
doFilter方法,struts2中最富盛名的方法了,每当有一个Request,都会调用这些Filters的doFilter方法
先来张图
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; ServletContext servletContext = getServletContext(); String timerKey = "FilterDispatcher_doFilter: "; try { // FIXME: this should be refactored better to not duplicate work with the action invocation //先看看ValueStackFactory所注入的实现类OgnlValueStackFactory //new OgnlValueStack ValueStack stack = dispatcher.getContainer().getInstance(ValueStackFactory.class).createValueStack(); ActionContext ctx = new ActionContext(stack.getContext()); ActionContext.setContext(ctx); UtilTimerStack.push(timerKey); //如果是multipart/form-data就用MultiPartRequestWrapper进行包装 //MultiPartRequestWrapper是StrutsRequestWrapper的子类,两者都是HttpServletRequest实现 //此时在MultiPartRequestWrapper中就会把Files给解析出来,用于文件上传 //所有request都会StrutsRequestWrapper进行包装,StrutsRequestWrapper是可以访问ValueStack //下面是参见Dispatcher的wrapRequest // String content_type = request.getContentType(); //if(content_type!= null&&content_type.indexOf("multipart/form-data")!=-1){ //MultiPartRequest multi =getContainer().getInstance(MultiPartRequest.class); //request =new MultiPartRequestWrapper(multi,request,getSaveDir(servletContext)); //} else { // request = new StrutsRequestWrapper(request); // } request = prepareDispatcherAndWrapRequest(request, response); ActionMapping mapping; try { //根据url取得对应的Action的配置信息 //看一下注入的DefaultActionMapper的getMapping()方法.Action的配置信息存储在 ActionMapping对象中 mapping = actionMapper.getMapping(request, dispatcher.getConfigurationManager()); } catch (Exception ex) { log.error("error getting ActionMapping", ex); dispatcher.sendError(request, response, servletContext, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, ex); return; } //如果找不到对应的action配置,则直接返回。比如你输入***.jsp等等 //这儿有个例外,就是如果path是以“/struts”开头,则到初始参数packages配置的包路径去查找对应的静态资源并输出到页面流中,当然.class文件除外。如果再没有则跳转到404 if (mapping == null) { // there is no action in this request, should we look for a static resource? String resourcePath = RequestUtils.getServletPath(request); if ("".equals(resourcePath) && null != request.getPathInfo()) { resourcePath = request.getPathInfo(); } if (staticResourceLoader.canHandle(resourcePath)) { // 在DefaultStaticContentLoader中:return serveStatic && (resourcePath.startsWith("/struts") || resourcePath.startsWith("/static")); staticResourceLoader.findStaticResource(resourcePath, request, response); } else { // this is a normal request, let it pass through chain.doFilter(request, response); } // The framework did its job here return; } //正式开始Action的方法 dispatcher.serviceAction(request, response, servletContext, mapping); } finally { try { ActionContextCleanUp.cleanUp(req); } finally { UtilTimerStack.pop(timerKey); } } }
//下面是ActionMapper接口的实现类 DefaultActionMapper的getMapping()方法的源代码: public ActionMapping getMapping(HttpServletRequest request, ConfigurationManager configManager) { ActionMapping mapping = new ActionMapping(); String uri = getUri(request);//得到请求路径的URI,如:testAtcion.action或testAction.do int indexOfSemicolon = uri.indexOf(";");//修正url的带;jsessionid 时找不到而且的bug uri = (indexOfSemicolon > -1) ? uri.substring(0, indexOfSemicolon) : uri; uri = dropExtension(uri, mapping);//删除扩展名,默认扩展名为action if (uri == null) { return null; } parseNameAndNamespace(uri, mapping, configManager);//匹配Action的name和namespace handleSpecialParameters(request, mapping);//去掉重复参数 //如果Action的name没有解析出来,直接返回 if (mapping.getName() == null) { returnnull; } //下面处理形如testAction!method格式的请求路径 if (allowDynamicMethodCalls) { // handle "name!method" convention. String name = mapping.getName(); int exclamation = name.lastIndexOf("!");//!是Action名称和方法名的分隔符 if (exclamation != -1) { mapping.setName(name.substring(0, exclamation));//提取左边为name mapping.setMethod(name.substring(exclamation + 1));//提取右边的method } } return mapping; }
从代码中看出,getMapping()方法返回ActionMapping类型的对象,该对象包含三个参数:Action的name、namespace和要调用的方法method。
如果getMapping()方法返回ActionMapping对象为null,则FilterDispatcher认为用户请求不是Action,自然另当别论.
FilterDispatcher会做一件非常有意思的事:如果请求以/struts开头,会自动查找在web.xml文件中配置的 packages初始化参数
<filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> <init-param> <param-name>packages</param-name> <param-value>com.mazhiyuan.action</param-value> </init-param> </filter>
FilterDispatcher会将com.lizanhong.action包下的文件当作静态资源处理,即直接在页面上显示文件内容,不过会忽略扩展名为class的文件。
比如在com.mazhiyuan.action包下有一个mazhiyuan.txt的文本文件,其内容为“mazhiyuan”,访问 http://localhost:8081/Struts2Demo/struts/mazhiyuan.txt时会输出txt中的内容
FilterDispatcher.findStaticResource()方法
protectedvoid findStaticResource(String name, HttpServletRequest request, HttpServletResponse response) throws IOException { if (!name.endsWith(".class")) {//忽略class文件 //遍历packages参数 for (String pathPrefix : pathPrefixes) { InputStream is = findInputStream(name, pathPrefix);//读取请求文件流 if (is != null) { ... // set the content-type header String contentType = getContentType(name);//读取内容类型 if (contentType != null) { response.setContentType(contentType);//重新设置内容类型 } ... try { //将读取到的文件流以每次复制4096个字节的方式循环输出 copy(is, response.getOutputStream()); } finally { is.close(); } return; } } } }
如果用户请求的资源不是以/struts开头——可能是.jsp文件,也可能是.html文件,则通过过滤器链继续往下传送,直到到达请求的资源为止。
如果getMapping()方法返回有效的ActionMapping对象,则被认为正在请求某个Action,将调用Dispatcher.serviceAction(request, response, servletContext, mapping)方法,该方法是处理Action的关键所在。
本文到此结束,下一篇讲讲serviceAction,并以此结束struts2的源码之旅。
下一篇:http://mazhiyuan.iteye.com/blog/1202128
发表评论
-
JAVA多线程-厕所问题
2012-11-22 11:55 1931在http://my.oschina.net/xpbug/bl ... -
第八章 最大自序列和
2012-11-01 20:29 837第八章的问题是常见的---最大自序列和 的问题 书中提 ... -
第二章 旋转字符串的思考
2012-10-26 16:09 848编程珠玑第二章旋转字符串,abcdefg向左旋转3位,变为de ... -
Mongdb的upsert出现E11000 duplicate key errors的错误分析
2012-10-25 17:36 9157昨日上线的系统,今天查日志时发现有不少E11000 dupli ... -
开源的Mongodb java client -- mango发布
2012-07-20 21:53 1855Mango ---- 一个非常简单的操作mongodb的 ... -
SOAP消息
2012-03-05 20:37 1260本文转自:http://blog.csdn.net/chang ... -
wsdl文档结构
2012-03-05 20:32 1505本文转自:http://blog.csdn.net/chang ... -
浅出Apache Cxf
2012-03-05 20:14 0由于业务需要,开放了系统的 Web Se ... -
struts2源码浅析(四)
2011-10-19 17:15 1515接上一篇讲了filter后,现在request到了action ... -
struts2源码浅析(二)
2011-10-19 16:34 2227接上一篇http://mazhiyuan.iteye.com/ ... -
struts2源码浅析(一)
2011-10-19 16:18 17291. Struts2架构图 请求首先通过Filter ... -
struts2.1权威指南-笔记
2010-12-19 22:36 10951.struts 1.x 和 struts 2.x的 ... -
Hibernate学习总结4---对象状态
2010-12-10 16:14 950session 的几个主要方法: 1,save方法和persi ... -
Hibernate学习总结3 --配置文件
2010-12-10 16:10 939如果不希望使用默认的hibernate.cfg.xml 文件作 ... -
Hibernate 学习总结一
2010-12-10 14:54 864引入: 模型不匹配(阻 ... -
HF servlet&jsp 前6章要点总结
2010-11-21 11:58 884今天有时间把前6章主要讲servlet的内容坐下总结。好了,开 ... -
jquery源码分析之属性篇
2010-11-20 20:09 1915jquery提供了一些快捷函 ... -
HF servelt&jsp 定制标记开发 要点总结
2010-11-13 11:41 13111.标记文件使用一个页 ... -
bean相关标准动作总结+复习
2010-11-07 23:22 7581.<jsp:useBean>动作会定义一个变量, ... -
HF servlet&jsp ---include 指令和动作元素
2010-11-07 23:02 8111.include的2种方式 include多用于网站中可重用 ...
相关推荐
struts2 源码分析struts2 源码分析struts2 源码分析struts2 源码分析struts2 源码分析struts2 源码分析
struts2 源码绝对完整,struts2 源码绝对完整。
Struts2源码阅读
struts2源码 struts2案例 struts2使用包
struts2源码struts2源码struts2源码
struts2源码分析总结 是我在网上找到的关于源码讲解比较深刻的资料总结。对于研究struts2源码很有价值。
struts源码struts源码struts源码struts源码
struts2源码详细解析51CTO下载-struts2源代码分析(个人觉得非常经典)
Struts2源码,让你一眼看穿struts2的原理和运行机制
STRUTS2源码解析STRUTS2源码解析STRUTS2源码解析STRUTS2源码解析STRUTS2源码解析STRUTS2源码解析STRUTS2源码解析STRUTS2源码解析STRUTS2源码解析STRUTS2源码解析STRUTS2源码解析STRUTS2源码解析STRUTS2源码解析...
java最新struts2源码,刚才github官网下载:https://github.com/apache/struts
struts2源码解析.pdf
已struts 2 源码 导入eclipse工程
struts2.1.8 struts2源码 Eclipse关联这个可以
struts2源码还有插件源码xwork-core源码
Strut2源码分析,写的非常好, 喜欢Struts2的人可以看一看
浪曦struts2源码第三课
struts2 项目源码,适合初学者,有需要的下载吧
Apache Struts 2 源码(struts-2.5.28.3-src.zip),Apache Struts 2.5.28.3是一个优雅的、可扩展的框架,用于创建企业级 Java Web 应用程序。它可以在完整发行版中使用,也可以作为单独的库、源代码、示例和文档...
struts 初步认识Struts2并部署验证 struts2 将要进行自定义类型转换的点数据从1个增加到3个 struts3 进行全局的类型转换 struts4 使用Struts2中内部类中的方法进行自定义类型转换,用到的类StrutsTypeConverter ...