在学习struts框架时经常会使用到通配符调用方法,如下:

<package name="shop" namespace="/" extends="struts-default">
<!-- 配置Action -->
<actionname="user_*" class="userAction" method="{1}">
<result name="success">/WEB-INF/jsp/login.jsp</result>
</action>
</package>

其中的action name="user_*"中*这个符号代表的值会传入method=“{1}”中,并对应action类的一个方法名,这样就能很大程度地减少配置文档中action的数目。

但是在使用这种通配符方法的时候,经常会看到这样的映射错误提示

Struts has detected an unhandled exception:
Message:There is no Action mapped for namespace [/] and action name [user_login] associated with context path [/shop]

如果看到提示的是映射问题,你可以按照映射路线排除一遍,

第一步:先排查访问的链接有没有问题(细节问题)

第二步:查看struts.xml的配置(仔细排查,出现问题几率很大)

第三步:查看相关的action类及方法(比如return的值是不是跟配置文件中的result对应得上等)

第四步:查看结果响应页面是否存在问题(出现问题的几率比较小)

(具体的做法我不细讲了,网上很多优秀篇章都有提及,可自行百度)

如果上面的四个步骤没出问题,可是还是报错,怎么办?那就可能是内部属性配置的问题了

在Struts 2的核心jar包struts2-core中,有一个default.properties的默认配置文件(路径:struts-2.5.2-min\lib\org\apache\struts2\default.properties)里面配置了一些全局的信息

其中有条语句是配置动态方法调用的

struts.enable.DynamicMethodInvocation = true

当使用动态调用方法时(action名 + 感叹号 + 方法名进行方法调用),需要将其属性改为true,

当使用通配符调用语法时,建议将其属性改为false(struts2.5.2中默认是false)

当我们需要将其属性改成false时,

只在struts.xml配置文件中加入此句即可修改属性

<constant name="struts.enable.DynamicMethodInvocation" value="false" />

很多网友都说改了之后都行了,不过我换了struts2.5之后,整了好久都还是不行……

最后是在配置文档struts.xml的Action中配置了

<allowed-methods>Action内的方法名</allowed-methods>才成功了

比如上面代码修改如下:

<package name="shop" namespace="/" extends="struts-default">
<!-- 配置Action -->
<action name="user_*" class="userAction" method="{1}">
<result name="success">/WEB-INF/jsp/login.jsp</result>
<!-- 配置允许的方法,多个方法用逗号分隔 -->
<allowed-methods>login</allowed-methods>
</action>
</package>

备注:红色代码中的login为对应Action类里面的方法名,可以加上多个方法,方法名之间用逗号隔开。

总结:在struts2.3之前的版本,正常的配置就可以了,但在struts2.3版本之后,使用通配符调用方法时,内部会验证是否允许访问该方法,所以要加上

<allowed-methods>方法名1,方法名2…</allowed-methods>代码。

原文地址:http://www.cnblogs.com/gsy52300/p/5778754.html

以下是我的补充:

在编写struts.xml时习惯照抄以前的模板,比如:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="" extends="struts-default" namespace="/">
<action>
<result name="success">index.jsp</result>
</action>
</package>
<constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
</struts>

非常容易忽略版本问题,当我试着照原作者的方法解决问题时,发现根本不支持<allowed-methods>标签,原因是版本不对,因此需要将文件头部修改为当前使用版本即可。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
...
</struts>

【转】struts2.5框架使用通配符指定方法(常见错误)的更多相关文章

  1. struts2.5框架使用通配符指定方法常见错误

    struts2.5框架使用通配符指定方法(常见错误) 在学习struts框架时经常会使用到通配符调用方法,如下: <package name="shop" namespace ...

  2. struts2.5框架使用通配符指定方法(常见错误)

    在学习struts框架时经常会使用到通配符调用方法,如下: <package name="shop" namespace="/" extends=&quo ...

  3. struts2.5框架使用通配符指定方法,某一个匹配不到

    在学习struts框架时经常会使用到通配符调用方法,如下:但奇怪的是,在validateName请求老报404,其他的都是ok的,开始以为是配置错了,检查好久才知道: <action name= ...

  4. struts2.5+框架使用通配符与动态方法

    概述:struts2.5以后加强了安全性,下面就是安全配置引发的问题 通配符: 在学习struts框架时经常会使用到通配符调用方法,如下: <package name="usercru ...

  5. struts2.5框架使用通配符无效问题

    错误: Struts has detected an unhandled exception: Messages: There is no Action mapped for namespace [/ ...

  6. SSH三大框架的基本整合以及常见错误的解决方法

    一.新建项目 eclipse->file->new->other->Dynamic Web Project,project name为sshDemo 二.下载jar包 1.st ...

  7. struts2官方 中文教程 系列十三:利用通配符选择方法

    介绍 在本教程中,我们将介绍如何在struts.xml中配置action节点以达到仅使用一个action节点将几个不同的url关联到特定action类的特定方法.这样做的目的是减少struts.xml ...

  8. struts2视频学习笔记 19-20(手工编写代码实现所有方法和指定方法校验)

    课时19 对Action中所有方法进行输入校验 1.手工编写代码实现对action中所有方法输入校验 通过重写validate() 方法实现, validate()方法会校验action中所有与exe ...

  9. Struts2拦截指定方法的拦截器

    作者:禅楼望月 默认情况下,我们为一个Action配置一个拦截器,该拦截器会拦截该Action中的所有方法,但是有时候我们只想拦截指定的方法.为此,需要使用struts2拦截器的方法过滤特性. 要使用 ...

随机推荐

  1. 批处理学习笔记2 - 编写批处理的for循环

    批处理中的for循环集成的功能比较多,可以直接对文件操作. ====================================================================== ...

  2. 递增和递减进度条CCProgressTimer

    关于scheduleUpdate看这篇即可 http://www.benmutou.com/blog/archives/56 接下来是示例代码: CCSize size = CCDirector::s ...

  3. linux io ports io memory

    http://m.blog.csdn.net/article/details?id=7204458

  4. Java IP地址字符串与BigInteger的转换, 支持IPv6

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  5. 小程序之自定义组件 ---- 列表goodsList

    教程请查看小程序开发文档:https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/ 自定义组件:自定义组 ...

  6. linux环境变量设置 以及 source命令 Linux 之 /etc/profile、~/.bash_profile 等几个文件的执行过程 Linux 设置环境变量

    定制环境变量  环境变量是和Shell紧密相关的,用户登录系统后就启动了一个Shell.对于Linux来说一般是bash,但也可以重新设定或切换到其它的Shell.环境变量文件:/etc/profil ...

  7. Gson json解析工具

    json 解析工具 ,谷歌出品 对象转换字符串 HashMap<String,String> hashMap = new HashMap<String, String>();  ...

  8. FreeRTOS 低功耗之待机模式

    STM32F103 如何进入待机模式在 FreeRTOS 系统中,让 STM32 进入待机模式比较容易,调用固件库函数PWR_EnterSTANDBYMode 即可. STM32F103 如何退出待机 ...

  9. mysql升级php找不到pdo

    最近把mysql升级到了5.6,当时工作正常,等周末一来php报错,提示找不到pdo. 甚是奇怪啊,看了一下phpinfo,果然没有mysql的pdo驱动了. 于是用yum又重新安装php-pdo,还 ...

  10. wampserver 报错 Fatal error:Call to undefined function curl_init()

    解决办法如下: 左键wampserver软件,找到PHP,然后找到扩展,点击php_curl开启这个扩展. 然后找到PHP的配置文件php.ini,路径为D:\wamp\bin\php\php5.3. ...