1 package org.springframework.samples.jpetstore.web.struts;2 3 import javax.servlet.ServletContext ;4 5 import org.apache.struts.action.Action;6 import org.apache.struts.action.ActionServlet;7 8 import org.springframework.samples.jpetstore.domain.logic.PetStoreFacade;9 import org.springframework.web.context.WebApplicationContext;10 import org.springframework.web.context.support.WebApplicationContextUtils;11 12 /**13 * Superclass for Struts actions in JPetStore's web tier.14 *15 * <p>Looks up the Spring WebApplicationContext via the ServletContext16 * and obtains the PetStoreFacade implementation from it, making it17 * available to subclasses via a protected getter method.18 *19 * <p>As alternative to such a base class, consider using Spring's20 * ActionSupport class for Struts, which pre-implements21 * WebApplicationContext lookup in a generic fashion.22 *23 * @author Juergen Hoeller24 * @since 30.11.200325 * @see #getPetStore26 * @see org.springframework.web.context.support.WebApplicationContextUtils#getRequiredWebApplicationContext27 * @see org.springframework.web.struts.ActionSupport28 */29 public abstract class BaseAction extends Action {30 31 private PetStoreFacade petStore;32 33 public void setServlet(ActionServlet actionServlet) {34 super.setServlet(actionServlet);35 if (actionServlet != null) {36 ServletContext servletContext = actionServlet.getServletContext();37 WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);38 this.petStore = (PetStoreFacade) wac.getBean("petStore");39 }40 }41 42 protected PetStoreFacade getPetStore() {43 return petStore;44 }45 46 }47