好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

struts2.5+框架使用通配符与动态方法常见问题小结

概述: struts 2.5以后加强了安全性,下面就是安全配置引发的问题

1. 通配符 :

在学习struts框架时经常会使用到通配符调用方法,如下:

?

1

2

3

4

5

< package name= "usercrud" namespace= "/" extends = "struts-default" >

     <action name= "test-*" class = "com.gitee.dgwcode.action.usercrudaction" method= "{1}" >

       <result name= "query" >/view/success.jsp</result>

       <!-- <allowed-methods>query,delete,update,insert</allowed-methods> -->

     </action> </ package >

   其中的 action name="user_*" 中*这个符号代表的值会传入 method=[{1}] 中,并对应action类的一个方法名,这样就能很大程度地减少配置文档中action的数目。

但是在使用这种通配符方法的时候,经常会看到这样的映射错误提示

struts problem report
struts has detected an unhandled exception:

messages:   
there is no action mapped for namespace [/] and action name [test-update] associated with context path [/struts2_01].

如果看到提示的是映射问题,你可以按照映射路线排除一遍,

第一步:先排查访问的链接有没有问题(细节问题)

第二步:查看struts.xml的配置(仔细排查,出现问题几率很大)

第三步:查看相关的action类及方法(比如return的值是不是跟配置文件中的result对应得上等)

第四步:查看结果响应页面是否存在问题(出现问题的几率比较小)

2.动态方法

当使用动态调用方法时(action名 + 感叹号 + 方法名进行方法调用),需要将其属性改为true,

如:query为类中的方法名

?

1

<a href= "${pagecontext.request.contextpath }/test!query" rel= "external nofollow" >dynamicmethodinvocation</a><br>

当使用通配符调用语法时,建议将其属性改为false(struts2.5.2中默认是false)

当我们需要将其属性改成false时,

只在struts.xml配置文件中加入此句即可修改属性

?

1

2

3

4

5

6

<constant name= "struts.enable.dynamicmethodinvocation" value= "false" />

<!-- 动态方法调用 -->

     <action name= "test"      class = "com.gitee.dgwcode.action.usercrudaction" >

       <result name= "query" >/view/success.jsp</result>

       <allowed-methods>query,delete,update,insert</allowed-methods>

     </action>

总结:<allowed-methods>方法名1,方法名2…</allowed-methods>代码

补充:struts2.5框架使用通配符指定方法

struts框架使用的通配符调用方法配置:

?

1

2

3

4

5

< package name= "hew" extends = "struts-default" >

   <!-- 配置action -->

   <action name= "action_*" class = "action" method= "{1}" >

     <result name= "success" >index.jsp</result>

   </action></ package >

其中 <action name="action_*" class="action"> 中的 name="action_*" 中的*代表的是method="{1}"中的{1}的值,并对应action类中的一个方法名。

注:struts2.3之前使用以上配置正常,struts2.3之后,使用通配符调用方法要加上<allowed-mthods>方法名1,方法名2..</allowed-mthods>

?

1

2

3

4

5

6

7

< package name= "hew" extends = "struts-default" >

   <!-- 配置action -->

   <action name= "action_*" class = "action" method= "{1}" >

     <result name= "success" >index.jsp</result>

     <allowed-mthods>方法名 1 ,方法名 2 ..</allowed-mthods>

   </action>

</ package >

总结

以上所述是小编给大家介绍的struts2.5+框架使用通配符与动态方法 ,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

原文链接:https://www.cnblogs.com/dgwblog/p/9638045.html

查看更多关于struts2.5+框架使用通配符与动态方法常见问题小结的详细内容...

  阅读:54次