修改Struts2(2.16+)配置文件的位置

最早学习Struts1时,它的配置文件是放在WEB-INF下的。过渡到Struts2,把配置文件放到src总感觉别扭。于是为了让自己习惯,我就把Struts2(2.16以上的版本)的配置文件也放到了WEB-INF下。

于是在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>
/WEB-INF/struts.xml
</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


启动服务器OK,没出错。可是无论我怎么发送请求,都报没有找到相应的Action。这是这么回事呢?

于是找到Struts2的源码下个断点启动服务器:

Dispatcher dispatcher = init.initDispatcher(config);看到了它,看看里面怎么回事:


/**
* Creates and initializes the dispatcher
*/
public Dispatcher initDispatcher( HostConfig filterConfig ) {
Dispatcher dispatcher = createDispatcher(filterConfig);
dispatcher.init();
return dispatcher;
}

继续看dispatcher.init();

/**
* Load configurations, including both XML and zero-configuration strategies,
* and update optional settings, including whether to reload configurations and resource files.
*/
public void init() {

if (configurationManager == null) {
configurationManager = new ConfigurationManager(BeanSelectionProvider.DEFAULT_BEAN_NAME);
}

try {
init_DefaultProperties(); // [1]
init_TraditionalXmlConfigurations(); // [2]
init_LegacyStrutsProperties(); // [3]
init_CustomConfigurationProviders(); // [5]
init_FilterInitParameters() ; // [6]
init_AliasStandardObjects() ; // [7]

Container container = init_PreloadConfiguration();
container.inject(this);
init_CheckConfigurationReloading(container);
init_CheckWebLogicWorkaround(container);

if (!dispatcherListeners.isEmpty()) {
for (DispatcherListener l : dispatcherListeners) {
l.dispatcherInitialized(this);
}
}
} catch (Exception ex) {
if (LOG.isErrorEnabled())
LOG.error("Dispatcher initialization failed", ex);
throw new StrutsException(ex);
}
}


看看init_TraditionalXmlConfigurations(); // [2]

private void init_TraditionalXmlConfigurations() {
String configPaths = initParams.get("config");
if (configPaths == null) {
configPaths = DEFAULT_CONFIGURATION_PATHS;
}
String[] files = configPaths.split("\\s*[,]\\s*");
for (String file : files) {
if (file.endsWith(".xml")) {
if ("xwork.xml".equals(file)) {
configurationManager.addConfigurationProvider(new XmlConfigurationProvider(file, false));
} else {
configurationManager.addConfigurationProvider(new StrutsXmlConfigurationProvider(file, false, servletContext));
}
} else {
throw new IllegalArgumentException("Invalid configuration file name");
}
}
}

原来在这:我发现开始在没有改变路径并配置的情况下 ,它加载的是

private static final String DEFAULT_CONFIGURATION_PATHS = "struts-default.xml,struts-plugin.xml,struts.xml";

看看Tomcat里路径(被我改后的位置)它在:

\Tomcat6.0\webapps\struct2\WEB-INF

之前它在

C:\Tomcat6.0\webapps\struct2\WEB-INF\classes

默认回去这里面找,
所以再用原来的位置是加载不上这个文件的,于是我改正了:

<init-param>
<param-name>config</param-name>
<param-value>struts-default.xml,struts-plugin.xml,../struts.xml</param-value>
</init-param>

Ok 启动下服务器;重新请求下Action,成功了。可以还有点小麻烦,我在提交请求时老报

警告:2010-8-19 23:06:42 com.opensymphony.xwork2.util.logging.jdk.JdkLogger warn
警告: No configuration found for the specified action: 'user/user' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
2010-8-19 23:06:43 com.opensymphony.xwork2.util.logging.jdk.JdkLogger warn
警告: No configuration found for the specified action: 'user/user' in namespace: ''. Form action defaulting to 'action' attribute's literal value.

竟然没有出错,可是看着警告很不爽,看看.......原来是标签用错了我在package加了命名空间/user可是在<s:form>中action="user/user!login",他似乎还有个属性namespace,于是加上namespace="/user" ,action="user!login",重新试试,OK很顺利.......
刚开始学习Struts2.可能说的不对,请大家多多指教。

我在整合S2SH时,同样把所有的配置文件放到了WEB-INF里,同样我也遇到了更过麻烦,运行配置文件没读取.......
最典型的是我在mian方法中读取Spring中的bean怎么初始化不了我定义的bean
具体是这样的:

//加载spring配置文件,初始化IoC容器
ApplicationContext context = null;
context=new ClassPathXmlApplicationContext("/WEB-INF/applicationContext.xml");
UsersDAO item=(UsersDAO)context.getBean("UsersDAO");
System.out.println(item.getClass());


怎么都是徒劳,后来想起来在mian方法中是没有所谓的容器的。于是改成:

//加载spring配置文件,初始化IoC容器
ApplicationContext context = null;
context= new FileSystemXmlApplicationContext("/WebRoot/WEB-INF/applicationContext.xml");
UsersDAO item=(UsersDAO)context.getBean("UsersDAO");
System.out.println(item.getClass());

正在学习中。希望大家多多指教下。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值