SSH框架,往Action注入bean

第一種方式:DelegatingRequestProcessor方式
DelegatingRequestProcessor繼承自RequestProcessor。

爲了讓Struts使用DelegatingRequestProcessor,需要在struts-config.xml文件中增加如下代碼:

<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />

這一行代碼是告訴Struts用DelegatingRequestProcessor來代替原來的RequestProcessor。完成設置這個設置後,Struts會將攔截到的用戶請求轉發到Spring context下的bean,根據bean的name屬性來匹配。而Struts中的action配置則無需配置type屬性(即使配置了type屬性也不起任何作用,除非在spring的配置文件裏找不到對應的name屬性值

配置了上面的一行代碼後,就可以在Spring的配置文件(可以不是applicationContext.xml,比如假設這裏是action- servlet.xml)中配置用來處理請求的Action bean了。配置的時候需要注意的是Action bean不再需要id屬性,而要用name替代id屬性,這時name屬性的值應與struts-config.xml中配置的Action的path屬性的值相同。

這樣,處理請求的Action就能處於Spring的管理之下了。

 
第二種方式:DelegatingActionProxy方式
struts-config.xml中的action的type設置爲:org.springframework.web.struts.DelegatingActionProxy
對應的具體Action繼承自類:DelegatingActionProxy(這種方式無需在struts-config.xml文件中添加<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />配置)
然後就可以在Spring的配置文件(可以不是applicationContext.xml,比如假設這裏是action- servlet.xml)中配置用來處理請求的Action bean了。配置的時候需要注意的是Action bean不再需要id屬性,而要用name替代id屬性,這時name屬性的值應與struts-config.xml中配置的Action的path屬性的值相同。
 
第三種方式:ActionSupport方式
自己寫一個ActionBase類,繼承自ActionSupport類,通過ActionSupport中的getWebApplicationContext().getBean(beanName)方式獲得spring管理的bean,然後讓所有的對應的Action繼承自改ActionBase類,該方法避免了配置文件的增加。
 
public Class ActionBase extends ActionSupport{
       public Object getBean(String beanName){
             return getWebApplicationContext().getBean(beanName);
       }
}
 
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章