KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > struts > ActionSupport


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.struts;
18
19 import java.io.File JavaDoc;
20
21 import javax.servlet.ServletContext JavaDoc;
22
23 import org.apache.struts.action.Action;
24 import org.apache.struts.action.ActionServlet;
25
26 import org.springframework.context.support.MessageSourceAccessor;
27 import org.springframework.web.context.WebApplicationContext;
28 import org.springframework.web.util.WebUtils;
29
30 /**
31  * Convenience class for Spring-aware Struts 1.1+ Actions.
32  *
33  * <p>Provides a reference to the current Spring application context, e.g.
34  * for bean lookup or resource loading. Auto-detects a ContextLoaderPlugIn
35  * context, falling back to the root WebApplicationContext. For typical
36  * usage, i.e. accessing middle tier beans, use a root WebApplicationContext.
37  *
38  * <p>For Struts DispatchActions or Lookup/MappingDispatchActions, use the
39  * analogous {@link DispatchActionSupport DispatchActionSupport} or
40  * {@link LookupDispatchActionSupport LookupDispatchActionSupport} /
41  * {@link MappingDispatchActionSupport MappingDispatchActionSupport} class,
42  * respectively.
43  *
44  * <p>As an alternative approach, you can wire your Struts Actions themselves
45  * as Spring beans, passing references to them via IoC rather than looking
46  * up references in a programmatic fashion. Check out
47  * {@link DelegatingActionProxy DelegatingActionProxy} and
48  * {@link DelegatingRequestProcessor DelegatingRequestProcessor}.
49  *
50  * @author Juergen Hoeller
51  * @since 1.0.1
52  * @see ContextLoaderPlugIn#SERVLET_CONTEXT_PREFIX
53  * @see org.springframework.web.context.WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
54  * @see org.springframework.web.context.ContextLoaderListener
55  * @see org.springframework.web.context.ContextLoaderServlet
56  * @see DispatchActionSupport
57  * @see LookupDispatchActionSupport
58  * @see MappingDispatchActionSupport
59  * @see DelegatingActionProxy
60  * @see DelegatingRequestProcessor
61  */

62 public abstract class ActionSupport extends Action {
63
64     private WebApplicationContext webApplicationContext;
65
66     private MessageSourceAccessor messageSourceAccessor;
67
68
69     /**
70      * Initialize the WebApplicationContext for this Action.
71      * Invokes onInit after successful initialization of the context.
72      * @see #initWebApplicationContext
73      * @see #onInit
74      */

75     public void setServlet(ActionServlet actionServlet) {
76         super.setServlet(actionServlet);
77         if (actionServlet != null) {
78             this.webApplicationContext = initWebApplicationContext(actionServlet);
79             this.messageSourceAccessor = new MessageSourceAccessor(this.webApplicationContext);
80             onInit();
81         }
82         else {
83             onDestroy();
84         }
85     }
86
87     /**
88      * Fetch ContextLoaderPlugIn's WebApplicationContext from the ServletContext,
89      * falling back to the root WebApplicationContext (the usual case).
90      * @param actionServlet the associated ActionServlet
91      * @return the WebApplicationContext
92      * @throws IllegalStateException if no WebApplicationContext could be found
93      * @see DelegatingActionUtils#findRequiredWebApplicationContext
94      */

95     protected WebApplicationContext initWebApplicationContext(ActionServlet actionServlet)
96             throws IllegalStateException JavaDoc {
97
98         return DelegatingActionUtils.findRequiredWebApplicationContext(actionServlet, null);
99     }
100
101
102     /**
103      * Return the current Spring WebApplicationContext.
104      */

105     protected final WebApplicationContext getWebApplicationContext() {
106         return this.webApplicationContext;
107     }
108
109     /**
110      * Return a MessageSourceAccessor for the application context
111      * used by this object, for easy message access.
112      */

113     protected final MessageSourceAccessor getMessageSourceAccessor() {
114         return this.messageSourceAccessor;
115     }
116
117     /**
118      * Return the current ServletContext.
119      */

120     protected final ServletContext JavaDoc getServletContext() {
121         return this.webApplicationContext.getServletContext();
122     }
123
124     /**
125      * Return the temporary directory for the current web application,
126      * as provided by the servlet container.
127      * @return the File representing the temporary directory
128      */

129     protected final File JavaDoc getTempDir() {
130         return WebUtils.getTempDir(getServletContext());
131     }
132
133
134     /**
135      * Callback for custom initialization after the context has been set up.
136      * @see #setServlet
137      */

138     protected void onInit() {
139     }
140
141     /**
142      * Callback for custom destruction when the ActionServlet shuts down.
143      * @see #setServlet
144      */

145     protected void onDestroy() {
146     }
147
148 }
149
Popular Tags