學了一段時間struts2,跟着教程做,但發現struts2的版本不同,很多東西的使用是有差異的。例如之前遇到的創建sessionFactory的方式就跟之前版本有着明顯的差異。今天又遇到一個問題,那就是通配符的使用。
問題:若不使用通配符,可以找到相對應的action,而使用通配符就會報錯,找不到actionmapping之內的錯,找不到action。
問題原因: struts2.5 為了增加安全性,在 struts.xml 添加了這么個屬性:<global-allowed-methods>regex:.*</global-allowed-methods>
解決:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <package name="default" namespace="/" extends="struts-default"> <global-allowed-methods>regex:.*</global-allowed-methods> <action name="helloworld" class="com.imooc.action.HelloWorldAction"> <result>/result.jsp</result> <result name="add">/add.jsp</result> <result name="update">/update.jsp</result> </action> </package> <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> </struts>
1.首先,注意頭部信息,這個應該是用來指定文件中允許使用那些標簽。
<!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd">
2.加上下面這句。
<global-allowed-methods>regex:.*</global-allowed-methods>
或者(不加上面這句),在action中加上指定允許調用的方法的語句:
<allowed-methods>login,logout</allowed-methods>