KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > mvc > AbstractUrlViewController


1 /*
2  * Copyright 2002-2007 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.servlet.mvc;
18
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import javax.servlet.http.HttpServletResponse JavaDoc;
21
22 import org.springframework.web.servlet.ModelAndView;
23 import org.springframework.web.util.UrlPathHelper;
24
25 /**
26  * Abstract base class for <code>Controllers</code> that return a view name
27  * based on the request URL.
28  *
29  * <p>Provides infrastructure for determining view names from URLs and configurable
30  * URL lookup. For information on the latter, see <code>alwaysUseFullPath</code>
31  * and <code>urlDecode</code> properties.
32  *
33  * @author Juergen Hoeller
34  * @since 1.2.6
35  * @see #setAlwaysUseFullPath
36  * @see #setUrlDecode
37  */

38 public abstract class AbstractUrlViewController extends AbstractController {
39
40     private UrlPathHelper urlPathHelper = new UrlPathHelper();
41
42
43     /**
44      * Set if URL lookup should always use full path within current servlet
45      * context. Else, the path within the current servlet mapping is used
46      * if applicable (i.e. in the case of a ".../*" servlet mapping in web.xml).
47      * Default is "false".
48      * @see org.springframework.web.util.UrlPathHelper#setAlwaysUseFullPath
49      */

50     public void setAlwaysUseFullPath(boolean alwaysUseFullPath) {
51         this.urlPathHelper.setAlwaysUseFullPath(alwaysUseFullPath);
52     }
53
54     /**
55      * Set if context path and request URI should be URL-decoded.
56      * Both are returned <i>undecoded</i> by the Servlet API,
57      * in contrast to the servlet path.
58      * <p>Uses either the request encoding or the default encoding according
59      * to the Servlet spec (ISO-8859-1).
60      * <p>Note: Setting this to "true" requires JDK 1.4 if the encoding differs
61      * from the VM's platform default encoding, as JDK 1.3's URLDecoder class
62      * does not offer a way to specify the encoding.
63      * @see org.springframework.web.util.UrlPathHelper#setUrlDecode
64      */

65     public void setUrlDecode(boolean urlDecode) {
66         this.urlPathHelper.setUrlDecode(urlDecode);
67     }
68
69     /**
70      * Set the UrlPathHelper to use for the resolution of lookup paths.
71      * <p>Use this to override the default UrlPathHelper with a custom subclass,
72      * or to share common UrlPathHelper settings across multiple MethodNameResolvers
73      * and HandlerMappings.
74      * @see org.springframework.web.servlet.handler.AbstractUrlHandlerMapping#setUrlPathHelper
75      */

76     public void setUrlPathHelper(UrlPathHelper urlPathHelper) {
77         this.urlPathHelper = urlPathHelper;
78     }
79
80     /**
81      * Return the UrlPathHelper to use for the resolution of lookup paths.
82      */

83     protected UrlPathHelper getUrlPathHelper() {
84         return this.urlPathHelper;
85     }
86
87
88     /**
89      * Retrieves the URL path to use for lookup and delegates to
90      * {@link #getViewNameForRequest}.
91      */

92     protected ModelAndView handleRequestInternal(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
93         String JavaDoc lookupPath = getUrlPathHelper().getLookupPathForRequest(request);
94         String JavaDoc viewName = getViewNameForRequest(request);
95         if (logger.isDebugEnabled()) {
96             logger.debug("Returning view name '" + viewName + "' for lookup path [" + lookupPath + "]");
97         }
98         return new ModelAndView(viewName);
99     }
100
101     /**
102      * Return the name of the view to render for this request, based on the
103      * given lookup path. Called by {@link #handleRequestInternal}.
104      * @param request current HTTP request
105      * @return a view name for this request (never <code>null</code>)
106      * @see #handleRequestInternal
107      * @see #setAlwaysUseFullPath
108      * @see #setUrlDecode
109      */

110     protected abstract String JavaDoc getViewNameForRequest(HttpServletRequest JavaDoc request);
111
112 }
113
Popular Tags