Struts2.3和Struts2.5的區別,以及ActionContext.getContext()引發的爭議
struts 2.5裡面 程式碼是這樣寫的:
//這裡返回物件是MAP而不是HttpParameter,跟Eclipse和MyEclipse有關,這裡寫上老師原始碼,作為對比檢視:
com.opensymphony.xwork2.ActionContext ac = ActionContext.getContext();
org.apache.struts2.dispatcher.HttpParameters hp = ac.getParameters();
org.apache.struts2.dispatcher.Parameter p = hp.get("username");
String username = p.getValue();
而struts 2.3 裡面 程式碼是這樣寫的:
com.opensymphony.xwork2.ActionContext ac =ActionContext.getContext();
java.util.Map<String, Object> p = ac.getParameters();
String username = (String)p.get("username");
剛開始以為是MyEclipse和Eclipse版本的問題,後來才發現,和編輯器沒關,是struts版本的問題
struts2.5和struts2.3的不同之處
參考資料:https://blog.csdn.net/qiaohao0206/article/details/78175254
1、struts2.5中是沒有xwork-core-2.3.這個jar包的,因為它和struts2-core-2.3.這個包合併了
2、struts2.5的過濾器配置相比struts2.3少了.ng這個包,變成了:
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
3、另外struts.xml的頭部資訊也對應的換成2.5,下邊有示例
參考資料https://blog.csdn.net/nttzli/article/details/52729489
其中在2.3 中的web.xml
<filter>
<filter-name>struts23</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts23</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2.5 中的web.xml<filter>
<filter-name>action2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>action2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Eclipse下struts2.5環境搭建與struts2.3的區別
參考資料:https://www.imooc.com/article/167481、jar包的匯入
在struts2.3中,老師前後一共匯入了9個常用jar包。但是在struts2.5中是沒有xwork那個jar包的,所以不用匯入也找不到,更不用再去低版本里去單獨下載這個包,因為它和struts-core包合併了。其次,當我使用這8個包部署專案後,啟動Tomcat會報錯,原因是還少了log4j和slf4j這兩個jar包,匯入後就可以解決問題了。所以前後一共是10個jar包。如下圖所示。
2、web.xml檔案過濾器的配置
配置web.xml檔案,在編寫<filter-class>標籤時,如果按照2.3版本寫,用Ctrl+滑鼠移動是不會出現連結的,也就是說寫的不對。正確的寫法是org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter,它與2.3版本相比少了一個ng包。完整的示例上面已經寫過。
3、struts.xml檔案的配置
在eclipse下新建XML檔案是不會有struts的dtd版本頭部資訊的。而老師的頭部資訊很明顯是2.3版本的,2.5版本的頭部資訊如下,新建struts.xml時需要直接拷貝貼上進去。
<?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>
</struts>
4、動態呼叫Action的方式
通過在<action>標籤中指定method屬性來呼叫的這種方式兩者並無區別,但是如果是使用感嘆號和萬用字元方式,與2.3版本是有細微的不同的。
如下有一個Action類,裡面有兩個方法update()和delete()。
font>檔案的配置在eclipse下新建XML檔案是不會有struts的dtd版本頭部資訊的。而老師的頭部資訊很明顯是2.3版本的,2.5版本的頭部資訊如下,新建struts.xml時需要直接拷貝貼上進去。
1.public class HelloWorldAction extends ActionSupport {
2. @Override
3. public String execute() throws Exception {
4. return SUCCESS;
5. }
6. public String add(){ //通過指定method屬性
7. return SUCCESS;
8. }
9. public String delete(){ //通過感嘆號方式
10. return "delete";
11. }
12. public String update(){ //通過萬用字元方式
13. return "update";
14. }
15.}
感嘆號方式和萬用字元方式的struts.xml檔案配置示例如下,需要在global-allowed-methods標籤或allowed-methods標籤中指定相關的方法,多個方法要用“,”隔開。具體的標籤含義及使用還是和struts2.3版本老師講的一樣,可參考視訊教程,這裡不再贅述。
1.<struts>
2. <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
3. <package name="default" namespace="/" extends="struts-default">
4. <global-allowed-methods>delete</global-allowed-methods>
5. <action name="helloworld" class="imooc.action.HelloWorldAction">
6. <result>/result.jsp</result>
7. <result name="delete">/delete.jsp</result>
8. </action>
9. </package>
10.</struts>
1.<struts>
2. <package name="default" namespace="/" extends="struts-default">
3. <action name="helloworld_*" method="{1}" class="imooc.action.HelloWorldAction">
4. <result>/result.jsp</result>
5. <result name="update">/{1}.jsp</result>
6. <allowed-methods>update</allowed-methods>
7. </action>
8. </package>
9.</struts>