在我们使用struts2的时候,肯定不想每一个方法就写一<action>
,所以通常我们会使用通配符,简化我们的代码。
<action name="userAction_*" class="userAction" method="{1}" >
<result name="login">/login.jsp</result>
<result name="home">/index.jsp</result>
</action>
但自己发现运行的时候,前端会报错,说找不到action
Struts Problem Report
Struts has detected an unhandled exception:Messages: There is no Action mapped for namespace [/] and action nam [userAction_login] associated with context path [/bos-web].
但在网上找,你也会发现,很多都是这么写的,真的找了许久啊,我还记得上次也是这种错误,后来指定了method的方法名,也能实现。最后猜想会不会是因为版本的问题,因为我使用的是struts2 2.5,搜索后发现在struts2.3版本之后需要在action标签加上这行代码<allowed-methods>方法1,方法2…</allowed-methods>
才能使用通配符。
<action name="userAction_*" class="userAction" method="{1}" >
<result name="login">/login.jsp</result>
<result name="home">/index.jsp</result>
<allowed-methods>login</allowed-methods>
</action>
还是因为自己经验不足啊,一直以为代码的问题,苦苦找了很久。自己写下来,记忆更加深刻一些。