KickJava   Java API By Example, From Geeks To Geeks.

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


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.ActionServlet;
24 import org.apache.struts.actions.MappingDispatchAction;
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.2 MappingDispatchActions.
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 classic Struts Actions, DispatchActions or LookupDispatchActions,
39  * use the analogous {@link ActionSupport ActionSupport} or
40  * {@link DispatchActionSupport DispatchActionSupport} /
41  * {@link LookupDispatchActionSupport LookupDispatchActionSupport} class.
42  *
43  * <p>As an alternative approach, you can wire your Struts Actions themselves
44  * as Spring beans, passing references to them via IoC rather than looking
45  * up references in a programmatic fashion. Check out
46  * {@link DelegatingActionProxy DelegatingActionProxy} and
47  * {@link DelegatingRequestProcessor DelegatingRequestProcessor}.
48  *
49  * @author Juergen Hoeller
50  * @since 1.1.3
51  * @see ContextLoaderPlugIn#SERVLET_CONTEXT_PREFIX
52  * @see WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
53  * @see org.springframework.web.context.ContextLoaderListener
54  * @see org.springframework.web.context.ContextLoaderServlet
55  * @see ActionSupport
56  * @see DispatchActionSupport
57  * @see LookupDispatchActionSupport
58  * @see DelegatingActionProxy
59  * @see DelegatingRequestProcessor
60  */

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

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

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

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

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

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

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

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

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