Пример интеграции Struts + Spring
Вот руководство, чтобы показать, как получить доступ к bean-компонентам, объявленным в контейнере Spring Ioc, в веб-приложении, разработанном с помощью Apache Struts 1.x.
Загрузите этот пример Struts 1.x + Spring -Struts-Spring-Hibernate-Example.zip
Spring поставляется со специфичным для Struts решением для компонентов доступа, объявленных в контейнере Spring Ioc.
-
Зарегистрируйте готовый подключаемый модуль Struts Spring в файле конфигурации Struts.
-
Измените класс действий Struts, чтобы расширить класс SpringActionSupport, подкласс класса Struts Action.
-
ActionSupport предоставляет удобный методgetWebApplicationContext() для доступа к bean-компонентам, объявленным в контейнере Spring Ioc.
1. Struts + Spring зависимости
Для интеграции со Struts 1.x Spring требуются библиотеки «spring-web.jar» и «spring-struts.jar». Вы можете скачать его с веб-сайта Spring или Maven.
pom.xml
org.springframework spring 2.5.6 org.springframework spring-web 2.5.6 org.springframework spring-struts 2.0.8
2. Плагин регистрации Struts
В файле конфигурации Struts (struts-config.xml) зарегистрируйте готовый плагин Struts Spring - «ContextLoaderPlugIn».
struts-config.xml
«ContextLoaderPlugIn» будет обрабатывать всю работу по интеграции между Struts и Spring. Вы можете загрузить xml-файл компонента Spring в свойство «contextConfigLocation».
SpringBeans.xml
3. Spring ActionSupport
В классе Struts Action расширяет класс Spring «ActionSupport» и получает компонент Spring с помощью метода «getWebApplicationContext()».
CustomerBean.xml
Struts Action
package com.example.customer.action; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.springframework.web.struts.ActionSupport; import com.example.customer.bo.CustomerBo; import com.example.customer.model.Customer; public class ListCustomerAction extends ActionSupport{ public ActionForward execute(ActionMapping mapping,ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception { CustomerBo customerBo = (CustomerBo) getWebApplicationContext().getBean("customerBo"); ... return mapping.findForward("success"); } }
Готово.