概述:struts2.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