[Struts2 框架学习] Struts2.5 通配符动态方法调用问题
前言
最近通过传智播客上面的 2016 年版本的 Hibernate 的视频学习,而这一周开始则是开始试着搭建 SSH,以及跟着视频做个小项目,熟悉一下 SSH 三大框架的整合开发,即使以后可能用的比较少,在我看来,熟悉之后,可能更晓得为何现在越来越多用的是 SSM 这三大框架的整合。了解基本的开发流程更为重要。以后有时间,可能会反过去再去看下 EJB 开发流程。
问题
在使用 SSH 框架中,遇到 Struts2 配置文件中通配符的问题,折腾了许久,后来决定通过搜搜引擎来解决问题。
问题的出现在 Struts 的版本,我所使用的是 2.5 版本
struts.xml 配置如下:
<?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>
<!-- 配置一系列常量 -->
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- 配置动态方法调用 -->
<!-- <constant name="struts.enable.DynamicMethodInvocation" value="false" /> -->
<package name="front" namespace="/" extends="struts-default">
<!-- <action name="user_login" class="userAction" method="login">
<result name="loginSuccess">/WEB-INF/hello.jsp</result>
<result name="login">/WEB-INF/login.jsp</result>
</action> -->
<action name="user_*" class="userAction" method="{1}">
<result name="loginSuccess">/WEB-INF/hello.jsp</result>
<result name="login">/WEB-INF/login.jsp</result>
</action>
</package>
</struts>
错误信息如下:
Messages:
There is no Action mapped for namespace [/] and action name [user_login] associated with context path [/learnSSH_001].
思路
由于之前不用通配符的时候,项目可以正常运行(即上面注释掉的那段配置)。故分析是否是自己通配符用的不对。检查了几遍后,无果。于是乎开始搜索相关知识。
一开始并没有发现是版本的问题,有人说要在配置文件中启动动态方法调用的配置。即
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
配置后并没有什么效果,看了一下 Struts 默认的配置,在 2.5 版本已经默认设置为 false 了。故寻找其他解决方案
原因以及解决方法
新版本的 Struts,例如 2.5 版本,为了安全性考虑而添加了 allowed-methods 属性1。在配置 action 的时候,如果配置了通配符,则需要在 action 之中配置 allowed-methods 这个属性。
struts.xml 配置文件如下:
<?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>
<!-- 配置一系列常量 -->
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.encoding" value="UTF-8" />
<package name="front" namespace="" extends="struts-default">
<action name="user_*" class="userAction" method="{1}">
<result name="loginSuccess">/WEB-INF/hello.jsp</result>
<result name="login">/WEB-INF/login.jsp</result>
<!--
struts 2.5 需要配置 allowed-methods 这个属性来使用通配符
可以写 regex:.* 用于适配 Action 中的所有方法
或者手动配置 Action 中的多个方法,login,logout 等等 Action 中的方法名,用逗号分隔
直接写 * 也会报错,需要写成 regex:.*
-->
<allowed-methods>regex:.*</allowed-methods>
<!-- <allowed-methods>login</allowed-methods> -->
</action>
</package>
</struts>
也可以在一个命名空间中使用全局的 global-allowed-methods 属性来配置2,这样子就不用为该命名空间下的每个 action 再进行配置,配置如下:
<?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>
<!-- 配置一系列常量 -->
<constant name="struts.devMode" value="true" />
<constant name="struts.i18n.encoding" value="UTF-8" />
<package name="front" namespace="" extends="struts-default">
<!-- 全局配置 -->
<global-allowed-methods>regex:.*</global-allowed-methods>
<action name="user_*" class="userAction" method="{1}">
<result name="loginSuccess">/WEB-INF/hello.jsp</result>
<result name="login">/WEB-INF/login.jsp</result>
<!-- <allowed-methods>regex:.*</allowed-methods> -->
</action>
</package>
</struts>
后记
编辑:HochenChong
时间:2017-12-12
参考文献
- 在struts2.5版本中使用DMI遇到问题.http://ask.csdn.net/questions/260958 ↩
- struts2.5动态方法绑定问题.http://blog.csdn.net/maobois/article/details/51854607 ↩