您的位置:首页 > 编程语言 > Java开发

改变Struts2配置文件默认路径

2012-07-30 17:24 295 查看
ssh还是毕业的时候自觉了下下,对这个框架本不是很了解,加上出来工作用的一直是play框架,今天闲得慌就写了个ssh的demo,途中遇到了各种一大堆的错误,大部不外乎是缺少jar包造成的,到整合struts2的时候我不想把struts.xml文件放在默认目录src下,就改变了它的放置目录,放到WEB-INF\xml。

启动tomcat时也没有报错,然后访问配置好的action,结果当然是404,然后仔细看下tomcat的打印信息我发现了这样一行信息:

Unable to locate configuration files of the name struts.xml 直译过来是:没有找到名字为struts的配置文件。

才知道是struts.xml没有被加载,因为我把struts.xml文件放在WEB-INF\xml下了,tomcat的默认初始化路径是项目的src目录,当然找不到struts的配置文件。

我反编译org.apache.struts2.dispatcher.FilterDispatcher类发现它有一个init方法,又去网上找了些相关资料看下,找到了个解决方案,把web.xml文件改成:

<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/xml/struts.xml</param-value>
</init-param>
</filter>
好,重启服务器,tomcat直接报错:

Caused by: 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).
at com.opensymphony.xwork2.inject.ContainerImpl.addInjectorsForMembers(ContainerImpl.java:144)
at com.opensymphony.xwork2.inject.ContainerImpl.addInjectorsForMethods(ContainerImpl.java:113)
at com.opensymphony.xwork2.inject.ContainerImpl.addInjectors(ContainerImpl.java:90)
at com.opensymphony.xwork2.inject.ContainerImpl.addInjectors(ContainerImpl.java:86)
at com.opensymphony.xwork2.inject.ContainerImpl$1.create(ContainerImpl.java:71)
at com.opensymphony.xwork2.inject.ContainerImpl$1.create(ContainerImpl.java:67)
at com.opensymphony.xwork2.inject.util.ReferenceCache$CallableCreate.call(ReferenceCache.java:150)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at com.opensymphony.xwork2.inject.util.ReferenceCache.internalCreate(ReferenceCache.java:76)
at com.opensymphony.xwork2.inject.util.ReferenceCache.get(ReferenceCache.java:116)
at com.opensymphony.xwork2.inject.ContainerImpl.inject(ContainerImpl.java:490)
at com.opensymphony.xwork2.inject.ContainerImpl$6.call(ContainerImpl.java:530)
at com.opensymphony.xwork2.inject.ContainerImpl$6.call(ContainerImpl.java:528)
at com.opensymphony.xwork2.inject.ContainerImpl.callInContext(ContainerImpl.java:580)
at com.opensymphony.xwork2.inject.ContainerImpl.inject(ContainerImpl.java:528)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:233)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:66)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:390)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:437)
... 28 more
没有找到依赖配置文件com.opensymphony.xwork2.ObjectFactory。

虽然web.xml中指明了struts的配置文件的路径,但是他依赖的如struts-default.xml没有初始化,那就把它加入进去:

<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,../xml/struts.xml</param-value>
</init-param>
</filter>
注意后面的:"../xml/struts.xml",为什么这样写呢,因为加载struts.xml时并不是加载的项目中存放配置文件的绝对路径(web_inf/xml),而是src目录的也就是classes目录下,所以就写相对路径。

以下是struts.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<!-- 配置struts2的常量,指定Struts的工厂类,托管给Spring -->
<constant name="struts.objectFactory" value="spring" />

<!-- 引入配置文件 -->
<include file="../xml/struts/login.xml"/>

</struts>


login.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>

<constant name="struts.action.extension" value="do,h"/>
<package name="loginPackage" namespace="/" extends="struts-default">

<action name="login_*" method="{1}"  class="www.com.controller.LogigAction">
<result name="SUCCESS">/index.jsp</result>
<result name="ERR">/index.jsp</result>
</action>

</package>

</struts>


上面的过程是亲身并测试通过,希望这个总结对遇到同样错误的新手有帮助!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: