指定struts2配置文件struts.xml的路径
2014-02-25 15:18
609 查看
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文件改成:
好,重启服务器,tomcat直接报错:
没有找到依赖配置文件com.opensymphony.xwork2.ObjectFactory。
虽然web.xml中指明了struts的配置文件的路径,但是他依赖的如struts-default.xml没有初始化,那就把它加入进去:
注意后面的:"../xml/struts.xml",为什么这样写呢,因为加载struts.xml时并不是加载的项目中存放配置文件的绝对路径(web_inf/xml),而是src目录的也就是classes目录下,所以就写相对路径。
以下是struts.xml文件:
login.xml
上面的过程是亲身并测试通过,希望这个总结对遇到同样错误的新手有帮助!
本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2012-09/71109.htm
启动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
相关文章推荐
- Myeclipse下java.lang.OutOfMemoryError: Java heap space的解决
- Spring中类的简单使用
- 【 java版坦克大战--事件处理】 键盘控制小球上下左右移动
- Java 内存区域
- Java堆内存的10个要点
- 7.8.3: 深入理解容器中的Bean---Bean继承与Java继承的区别
- java socket udp实例
- java匿名内部类范例
- powerdesigner Could not Initialize JavaVM
- 7.6: Spring3.0提供的Java配置管理
- 7.5.10: Spring容器中的Bean---Spring的Bean和JavaBean
- JAVA自定义注释(Target,Retention,Documented,Inherit)
- 7.5.9: Spring容器中的Bean---组合属性名称
- 7.5.8: Spring容器中的Bean---注入集合值
- 7.5.7: Spring容器中的Bean---注入嵌套Bean
- Spring3.2异步处理http请求
- 7.5.6: Spring容器中的Bean---使用自动装配注入合作者Bean
- 7.5.5: Spring容器中的Bean---配置合作者Bean
- eclipse配置maven
- 7.5.4: Spring容器中的Bean---设置普通属性值