最近开始学习struts2,准备用struts2做项目,按照以往的习惯喜欢把配置文件之类的放到/WEB-INF/config目录下,结果写的Demo运行时就出问题了。
几经测试发现只有放到src下面才能正常运行,但是本人是属于那种固执(:-D也就是那种不撞南墙不死心)的人,查找了3、4个小时,翻阅无数同仁的文章,虽然没有直接找到解决办法,看一些回帖时也得到了启示,最后总算是解决了这个问题,下面就贴出解决的步骤,希望能为某些人解决一些同样的问题。
--------------------
平台、技术:Tomcat 6.6,JDK1.6,Struts2.18
--------------------
先帖出几个主要的配置文件:
----struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="def" extends="struts-default" namespace="/"> <action name="hello"> <result>/hello.jsp</result> </action> </package> </struts>
----web.xml
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
//这是我测试用的两个文件代码,由于只是简单的测试,jsp就不贴出了。
---下面是使用非默认路径(不在src下)时出现的错误
- 地址栏输入:../hello.action,出现异常:
警告: Could not find action or result
There is no Action mapped for namespace / and action name hello. - [unknown location]
表明请求映射出现了问题,难道是struts2的配置文件出现了错误?仔细检查一下,不是!
再回过头看看Tomcat的启动日志,有下面一句:
2011-7-17 20:15:36 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Unable to locate configuration files of the name struts.xml, skipping
关键是在上面下划线的那一句,不能定位配置文件,也就是struts.xml找不到!这是因为struts2默认是到src下面去找struts配置文件的,现在我是放在/WEB-INF/下面,当然会找不到了!
- 那么,我们就在配置struts2的web.xml中filter的元素内指明文件路径嘛,在配置struts2的过滤器中添加如下元素:
<init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts.xml</param-value> </init-param>
重启Tomcat,这下大发了,Tomcat直接蹦出异常:
com.opensymphony.xwork2.inject.DependencyException:
com.opensymphony.xwork2.inject.ContainerImpl$MissingDependencyException:
No mapping found for dependency [type=com.opensymphony.xwork2.ObjectFactory, name='default'] in public void com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.
setObjectFactory(com.opensymphony.xwork2.ObjectFactory).
依赖的配置文件没有找到。
- google或baidu一下,发现有很多人出现过这种问题,认真的读下去,发现虽然指明了struts的配置文件的路径,但是他依赖的如struts-default.xml没有放入初始化参数。
%>_<%,为了测试成功,我忍,满足它,给config配置增加几个值,变为:struts-default.xml,struts-plugin.xml,/WEB-INF/struts.xml。
重启,期待着没有错误的日子........
Bingo!Tomcat终于没有发出救火车的声音了....
下面重新访问 ..../hello.action。
问题解决了????恭喜你!
依然是错误[There is no Action mapped for namespace / and action name hello. - [unknown location]]??????不要着急,很正常!
- 仔细看看Tomcat的启动日志,依然可以发现一个失败信息:
2011-7-17 20:15:36 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Unable to locate configuration files of the name struts.xml, skipping
为什么指定了自己的struts.xml文件路径依然访问不到呢?
原因依然在struts加载配置文件的方式,struts并不是获取的配置文件相对应用(项目)的路径,而是相对src,对于web是相对/WEB-INF/classes文件夹的路径,现在知道了最终的解决方案了?
对了,就是把web.xml中的[/WEB-INF/struts.xml]改成 [../struts.xml],即使用相对/WEB-INF/classes文件夹的路径!
最终web.xml的配置为:
<filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter. StrutsPrepareAndExecuteFilter </filter-class> <init-param> <param-name>config</param-name> <param-value> struts-default.xml,struts-plugin.xml,../struts.xml </param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>*.action</url-pattern> </filter-mapping>
编后:经过以上的步骤我最终解决了问题,希望能对出现了同样问题的人有所帮助!由于第一次在ITeye上发技术类的文章不足之处还请大家多多包涵!