概述:struts2.5以后加強(qiáng)了安全性,下面就是安全配置引發(fā)的問題
1.通配符:
在學(xué)習(xí)struts框架時(shí)經(jīng)常會(huì)使用到通配符調(diào)用方法,如下:
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_*"
中*這個(gè)符號(hào)代表的值會(huì)傳入method=“{1}”
中,并對(duì)應(yīng)action類的一個(gè)方法名,這樣就能很大程度地減少配置文檔中action的數(shù)目。
但是在使用這種通配符方法的時(shí)候,經(jīng)常會(huì)看到這樣的映射錯(cuò)誤提示
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].
如果看到提示的是映射問題,你可以按照映射路線排除一遍,
第一步:先排查訪問的鏈接有沒有問題(細(xì)節(jié)問題)
第二步:查看struts.xml的配置(仔細(xì)排查,出現(xiàn)問題幾率很大)
第三步:查看相關(guān)的action類及方法(比如return的值是不是跟配置文件中的result對(duì)應(yīng)得上等)
第四步:查看結(jié)果響應(yīng)頁(yè)面是否存在問題(出現(xiàn)問題的幾率比較小)
2.動(dòng)態(tài)方法
當(dāng)使用動(dòng)態(tài)調(diào)用方法時(shí)(action名 + 感嘆號(hào) + 方法名進(jìn)行方法調(diào)用),需要將其屬性改為true,
如:query為類中的方法名
1
|
<a href= "${pagecontext.request.contextpath }/test!query" rel= "external nofollow" >dynamicmethodinvocation</a><br> |
當(dāng)使用通配符調(diào)用語(yǔ)法時(shí),建議將其屬性改為false(struts2.5.2中默認(rèn)是false)
當(dāng)我們需要將其屬性改成false時(shí),
只在struts.xml配置文件中加入此句即可修改屬性
1
2
3
4
5
6
|
<constant name= "struts.enable.dynamicmethodinvocation" value= "false" /> <!-- 動(dòng)態(tài)方法調(diào)用 --> <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> |
總結(jié):<allowed-methods>方法名1,方法名2…</allowed-methods>代碼
補(bǔ)充:struts2.5框架使用通配符指定方法
struts框架使用的通配符調(diào)用方法配置:
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}的值,并對(duì)應(yīng)action類中的一個(gè)方法名。
注:struts2.3之前使用以上配置正常,struts2.3之后,使用通配符調(diào)用方法要加上<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 > |
總結(jié)
以上所述是小編給大家介紹的struts2.5+框架使用通配符與動(dòng)態(tài)方法 ,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)服務(wù)器之家網(wǎng)站的支持!
原文鏈接:https://www.cnblogs.com/dgwblog/p/9638045.html