Struts2中Action必須實現execute方法嗎

struts2中action中是否須要實現execute方法呢?web

其實分兩種狀況:jsp


1)若是你的Action類是繼承自ActionSupport或是BaseAction的話,確切的說是重寫了execute方法,ActionSupport裏的默認實現就是返回"success"視圖。所以,你能夠不實現execute方法,只要你的struts.xml裏有"success"對應的result便可。xml

  1. <action name="doRevenuesMaintenance">  
  2.             <interceptor-ref name="novatar-webStack-baseparam">  
  3.                 <param name="security.actionType">PRIVATE</param>  
  4.             </interceptor-ref>  
  5.             <result name="success">incomeMaintenance.jsp</result>  
  6. < /action>  

 

這段代碼中就是經過action的配置文件來控制跳轉頁面。在action的類中沒有execute()方法重寫。繼承

 

2)若是你的Action類沒有繼承ActionSupport或是BaseAction的話,而你又沒有在struts.xml中對應<action>標籤中用method屬性指定你本身的方法的話,默認就要找execute方法,這時是必需要實現execute方法的,不然Struts2會找不到對應的方法而報錯。

不過,大部分狀況下都是繼承ActionSupport的(好比輸入驗證、文件上傳等功能就要求必須繼承)。還有,無論你寫沒寫execute方法,仍是能夠用<action>標籤的method屬性指定其餘方法的。文件上傳

相關文章
相關標籤/搜索