在完成web的大作业时使用了通配符*结果出现以下情况
出错信息:There is no Action mapped for namespace [/] and action name [Student_Query_Action] associated with context path [/%E5%AD%A6%E7%94%9F%E9%80%89%E8%AF%BE%E7%B3%BB%E7%BB%9F].
原因:struts2从2.5版本开始,默认开启了严格的方法调用。如果要使用通配符*,必须在package中设置 strict-method-invocation=“false”。而我没有设置
<action name="login" class="Action.LoginAction">
<result name="student">/student.jsp</result>
<result name="teacher">/teacher.jsp</result>
<result name="admin">/admin.jsp</result>
<result name="error">/index.jsp</result>
</action>
<action name="Student_*_Action" class="Action.StudentAction" method="{1}">
<result name="success">/student.jsp</result>
<result name="error">/index.jsp</result>
</action>
</package>
详细情况如下:
struts2从2.5版本开始,为了提升安全性,默认开启了严格的方法调用。
在struts2-core-2.5.*.jar中我们可以找到配置文件struts-default.xml,
首先请看:
而我们在自己的struts.xml中创建的package都是继承自struts-default,既默认设置有 strict-method-invocation=“true” 。那么它有什么用呢?我们继续来看它的配置文件:
有一个属性,全局允许方法,所以在严格方法调用的情况下,它默认允许通过的只能是和上图相同方法名的方法。可以说,该属性的作用是指定可被调用的方法 。
总结:
因此想要使action调用其它方法有两种解决方法
(1)将strict-method-invocation 设置成 false,取消严格的方法调用。
(2)新加属性所使用的方法。
如: