对着参考书上的方法配置struts.xml来实现动态Action功能。
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>
<package name="default" namespace="/" extends="struts-default">
<result>/result.jsp</result>
<result name="add">/add.jsp</result>
<result name="update">/update.jsp</result>
</action>
</package>
</struts>
会出现如下错误。
HTTP Status 404 - There is no Action mapped for namespace [/] and action name [userAction!update] associated with context path
在网上搜索以后,发现2.5和2.3有所不同。
struts2.5 为了增加安全性,在 struts.xml 添加了这么个属性:<global-allowed-methods>regex:.*</global-allowed-methods>。
使用在配置文件struts.xml中应该加入这两行
<global-allowed-methods>regex:.*</global-allowed-methods>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
所以更改后的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>
<package name="default" namespace="/" extends="struts-default">
<global-allowed-methods>regex:.*</global-allowed-methods>
<action name="userAction" class="UserAction">
<result name="add">user_add.jsp</result>
<result name="update">user_update.jsp</result>
</action>
</package>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
</struts>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<global-allowed-methods>regex:.*</global-allowed-methods>
<action name="userAction" class="UserAction">
<result name="add">user_add.jsp</result>
<result name="update">user_update.jsp</result>
</action>
</package>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
</struts>
然后就可以成功使用Action!xx来实现动态功能调用了。