KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > samples > jpetstore > web > struts > BaseAction


1 package org.springframework.samples.jpetstore.web.struts;
2
3 import javax.servlet.ServletContext JavaDoc;
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 ServletContext
16  * and obtains the PetStoreFacade implementation from it, making it
17  * available to subclasses via a protected getter method.
18  *
19  * <p>As alternative to such a base class, consider using Spring's
20  * ActionSupport class for Struts, which pre-implements
21  * WebApplicationContext lookup in a generic fashion.
22  *
23  * @author Juergen Hoeller
24  * @since 30.11.2003
25  * @see #getPetStore
26  * @see org.springframework.web.context.support.WebApplicationContextUtils#getRequiredWebApplicationContext
27  * @see org.springframework.web.struts.ActionSupport
28  */

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 JavaDoc 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
Popular Tags