struts2中如何自动纠正如contextpath/contextpath/*.action这种路径

在上篇 struts2中如何根据请求路径定位到详细的访问action 中,主要了解了在struts2中是如何通过一种路径解析成一个action的。那么针对以下的路径
contextpath/contextpath/*.action的路径,在命名空间为""的情况下,struts2是如何进行纠正的呢。

其实也很简单,这里面可以说是struts2本身的一处处理策略,因为在整个解析过程中,除掉上下文之后,就剩下以下的一个路径了:

contextpath/actionName

这里面有一个/符号,并且从理论上来讲,并没有一个有效的actionConfig与之匹配,但是由于项目中设置了参数

struts.enable.SlashesInActionNames

这个值没有被设置,那么默认就是false,即不允许在actionName中存在着/符号,那么在struts2中就直接在actionName中去除了这个/符号,如下代码所示,类DefaultActionMapper的方法parseNameAndNamespace

if (!allowSlashesInActionNames && name != null) {
            int pos = name.lastIndexOf('/');
            if (pos > -1 && pos < name.length() - 1) {
                name = name.substring(pos + 1);
            }
        }

这样处理之后,剩下的actionName就成了我们所需要的actionName了,这样就间接的找到了正确的actionConfig,也可以说是碰巧正确了。

转载请标明出处:i flym
本文地址:https://www.iflym.com/index.php/code/201302270002.html

相关文章:

作者: flym

I am flym,the master of the site:)

发表评论

邮箱地址不会被公开。 必填项已用*标注