struts.xml配置部分:
<action name="*Person" class="org.apache.struts.wildcardmethod.action.PersonAction" method="{1}">
<result name="success">/view.jsp</result>
<result name="input">/input.jsp</result>
<allowed-methods>create,edit</allowed-methods>
</action>
页面请求部分:
<s:if test="personList.size > 0">
<ol>
<s:iterator value="personList">
<li>
<s:property value="firstName"/>
<s:property value="lastName"/>
<s:url action="editPerson" var="editUrl">
<s:param name="id" value="id"/>
</s:url>
<s:url action="deletePerson" var="deleteUrl">
<s:param name="id" value="id"/>
</s:url>
<a href="<s:property value='#editUrl'/>">Edit</a>
<a href="<s:property value='#deleteUrl'/>">Delete</a>
</li>
</s:iterator>
</ol>
</s:if>
<s:url action="createPerson" var="newUrl">
<s:param name="id" value="0"/>
</s:url>
<p><a href="<s:property value='#newUrl'/>">Create new person.</a></p>
调用delete方法是可以,其他的都不行,报错(部分信息):
Struts Problem Report
Struts has detected an unhandled exception:
Messages:
There is no Action mapped for namespace [/] and action name [saveOrUpdatePerson] associated with context path [/wildcard-method-selection].
找了好久都没发现什么问题,google一下,有人说是版本问题,试了一下,果然行。
在struts2.3之前的版本,正常的配置就可以了,但在struts2.3版本之后,使用通配符调用方法时,内部会验证是否允许访问该方法,所以要加上<allowed-methods>方法名1,方法名2…</allowed-methods>代码。
原文链接:http://www.cnblogs.com/gsy52300/archive/2016/08/17/5778754.html
但是为什么没有配置allowed-methods也能调用delete方法呢?execute方法能调用,这个可以理解,因为是框架里面有的,allowed-methods应该没有限制对execute方法的调用。而delete方法是自定义的(反正在ActionSupport里面没有定义),怎么就能调用呢?有待答案。
官方文档有正解! http://struts.apache.org/docs/action-configuration.html#ActionConfiguration-StrictDMI