指定struts2配置文件struts.xml的路径

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>


 

上面的过程是亲身并测试通过,希望这个总结对遇到同样错误的新手有帮助!

本篇文章来源于 Linux公社网站(www.linuxidc.com)  原文链接:http://www.linuxidc.com/Linux/2012-09/71109.htm

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值