读书人

怎么指定struts2配置文件的路径

发布时间: 2012-12-16 12:02:32 作者: rapoo

如何指定struts2配置文件的路径?
如何指定struts2配置文件的路径?
如题,我现在在用MyEclipse搭框架,想把所有的的配置文件都放在web-inf/cfg/目录下的,这样好统一管理.
看见书上写可以使用init param配个config参数的,然后指定配置文件的目录.但试了n久都没成功,老报空指针异常.....
把那个config参数去掉,再把struts.xml文件放到src目录就又好了......郁闷.

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>WEB-INF/cfg/struts.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</filter>
[解决办法]
在搭建的时候 会让你选择配置文件的目录啊 你选下就可以了!!
[解决办法]
在搭建的时候???你的意思是MyEclipse有 struts2 的插件了??
[解决办法]
URL没学好吧,
<param-value>WEB-INF/cfg/struts.xml</param-value>
WEB-INF前面要加/改成
<param-value>/WEB-INF/cfg/struts.xml</param-value>
[解决办法]

引用:
URL没学好吧,
<param-value>WEB-INF/cfg/struts.xml </param-value>
WEB-INF前面要加/改成
<param-value>/WEB-INF/cfg/struts.xml </param-value>

顶起,楼主servlet学的不怎么好吧,建议加强下
[解决办法]
同志,其实,两种方式我都试过了,都不行的...
[解决办法]
<param-value>../cfg/struts.xml </param-value>
加载时是用的类中的路径,所以根目录是WEB-INF/classes,所以要向上一层才能找到。不过还是建议楼主把配置文件放到src下,要不在struts.xml中include其它配置文件的时候每个都要写../,影响可读性
[解决办法]
web.xml

Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<!-- struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
<init-param>
<param-name>config</param-name>
<param-value>../struts2conf/struts.xml</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>


</filter-mapping>

<welcome-file-list>
<welcome-file>index.wml</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


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>
<!-- struts2 默认配置文件,必须加进来 有这里看出,当前文件的路径是跟路径下classes路径,所以其他的文件要从classes这个路径开始找 -->
<include file="struts-plugin.xml" />
<include file="struts-default.xml" />
<!-- 自己添加的配置文件 注意路径 -->
<include file="../struts2conf/mystruts.xml" />
<include file="../struts2conf/wml-struts.xml" />
</struts>

参考:http://cooler1217.iteye.com/blog/1222497
大部分人都是说这样解决的
但是我的问题没有解决。。。
[解决办法]
如果使用自定义的配置文件,还需要加入struts-default.xml,否则会报错:

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).


有文章说,还需要加入struts-plugin.xml,但是我没加也没报错!

读书人网 >Java相关

热点推荐