概述
问题:
1、Struts2在web.xml中配置为“/*”和“*.action,*.jsp”的差别。
2、There is no Action mapped for namespace / and action name ...的问题。
分析(环境是Struts2.1.8.1):
Struts2过滤器的配置有2种方式:
那么这两种方式的配置,究竟有什么差别呢?
首先,假如配置方式是*.action的话,一般应当同时配置*.jsp,因为如果不通过action而直接访问jsp页面的话,Struts2标签在解析的时候会获取当前线程ThreadLocal中的Dispatcher。而Dispatcher是在Struts过滤器中预设的。代码如下:
除了为当前线程预设Dispatcher以外,Struts2对“/*”的请求,在完成普通的“*.action”过滤的基础上,另外提供2点功能:
第1点用于访问classpath中特定的静态资源;
第2点支持无后缀名的Action请求;
Struts2的标签有时候需要某些CSS、JS文件的支持,比如<s:head/>标签,可能就转换成:
中通过packages参数指定的包、以及 "org.apache.struts2.static template org.apache.struts2.interceptor.debugging static"这4个包。
由于packages可配置,从而,如果有自己的classpath上的资源需要访问,或者需要更改Struts本身的静态资源时,只要把Classpath下相应的package设置在过滤器的初始参数中即可(这一条看上去好像没什么用处)。
上面是使用/*时对静态资源的访问,那么使用*.action时如果需要的话,如何访问静态资源呢?
很简单,只要把需要用到的静态资源解压缩到WebContent/struts目录下即可。
第2点“支持无后缀名的Action请求”经常带来一些混乱,最典型的就是“/*”错误地拦截了其他的映射为无后缀名的Servlet请求。比如DWR、FCKEditor等都存在这种问题。
比如,当访问“/demo/dwr”时,正常情况应该显示当前系统中对外暴露的JS方法的列表,但在Struts2的默认配置下,却得到“There is no Action mapped for namespace / and action name dwr.”
又比如在默认配置下,访问http://localhost:8080/demo/hello.action
和访问http://localhost:8080/demo/hello这两者是等同的。
当然,也只有无后缀名的URL请求才会被Struts2当做是Action,这也是为什么/dwr无法访问,然而/dwr/interface.js可以访问的原因。
具体的,看一下下面的代码就明白了:
- //Struts2默认将“*.action”或者无后缀的URL当做Action
- protected List<String> extensions = new ArrayList<String>() {{ add("action"); add("");}};
- protected String dropExtension(String name, ActionMapping mapping) {
- if (extensions == null) {
- return name;
- }
- for (String ext : extensions) {
- if ("".equals(ext)) {
- // This should also handle cases such as /foo/bar-1.0/description. It is tricky to distinquish /foo/bar-1.0 but perhaps adding a numeric check in the future could work
- // request uri如果不包含扩展名的话,则匹配此情况
- int index = name.lastIndexOf('.');
- if (index == -1 || name.indexOf('/', index) >= 0) {
- return name;
- }
- } else {
- String extension = "." + ext;
- if (name.endsWith(extension)) {
- name = name.substring(0, name.length() - extension.length());
- mapping.setExtension(ext);
- return name;
- }
- }
- }
- return null;
- }
//Struts2默认将“*.action”或者无后缀的URL当做Action
protected List<String> extensions = new ArrayList<String>() {{ add("action"); add("");}};
protected String dropExtension(String name, ActionMapping mapping) {
if (extensions == null) {
return name;
}
for (String ext : extensions) {
if ("".equals(ext)) {
// This should also handle cases such as /foo/bar-1.0/description. It is tricky to distinquish /foo/bar-1.0 but perhaps adding a numeric check in the future could work
// request uri如果不包含扩展名的话,则匹配此情况
int index = name.lastIndexOf('.');
if (index == -1 || name.indexOf('/', index) >= 0) {
return name;
}
} else {
String extension = "." + ext;
if (name.endsWith(extension)) {
name = name.substring(0, name.length() - extension.length());
mapping.setExtension(ext);
return name;
}
}
}
return null;
}
那么,怎么解决此问题呢?
有2种办法。
第1种很简单,在Struts.properties中定义:
struts.action.extension = action即可解决此问题。
Struts2缺省配置对应于:
struts.action.extension = action,(注意后面有个逗号)
第2种是在Struts.properties中设置:
struts.action.excludePattern = /dwr.*,/webEditor.*(注意,这儿是正则表达式,不是URL匹配模式,所以要写/dwr.*而不是/dwr/*)
这种写法应配置StrutsPrepareAndExecuteFilter,配置FilterDispatcher是无效的。
struts.i18n.encoding=utf-8
struts.action.extension=,
struts.action.excludePattern = /js.*,/static.*(js和static文件夹是项目中js和css所在的目录)
最后
以上就是光亮小懒虫为你收集整理的Struts2在web.xml中配置为“/*”和“*.action,*.jsp”的差别的全部内容,希望文章能够帮你解决Struts2在web.xml中配置为“/*”和“*.action,*.jsp”的差别所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复