/**
* Copyright 2005-2010 hdiv.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hdiv.web.servlet.view;
import java.util.Locale;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.AbstractUrlBasedView;
import org.springframework.web.servlet.view.InternalResourceView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
/**
* Simple implementation of the {@link org.springframework.web.servlet.ViewResolver}
* interface, allowing for direct resolution of symbolic view names to URLs,
* without explicit mapping definition. This is useful if your symbolic names
* match the names of your view resources in a straightforward manner
* (i.e. the symbolic name is the unique part of the resource's filename),
* without the need for a dedicated mapping to be defined for each view.
*
* <p>Supports {@link AbstractUrlBasedView} subclasses like {@link InternalResourceView},
* {@link org.springframework.web.servlet.view.velocity.VelocityView} and
* {@link org.springframework.web.servlet.view.freemarker.FreeMarkerView}.
* The view class for all views generated by this resolver can be specified
* via the "viewClass" property.
*
* <p>View names can either be resource URLs themselves, or get augmented by a
* specified prefix and/or suffix. Exporting an attribute that holds the
* RequestContext to all views is explicitly supported.
*
* <p>Example: prefix="/WEB-INF/jsp/", suffix=".jsp", viewname="test" ->
* "/WEB-INF/jsp/test.jsp"
*
* <p>As a special feature, redirect URLs can be specified via the "redirect:"
* prefix. E.g.: "redirect:myAction.do" will trigger a redirect to the given
* URL, rather than resolution as standard view name. This is typically used
* for redirecting to a controller URL after finishing a form workflow.
*
* <p>Furthermore, forward URLs can be specified via the "forward:" prefix. E.g.:
* "forward:myAction.do" will trigger a forward to the given URL, rather than
* resolution as standard view name. This is typically used for controller URLs;
* it is not supposed to be used for JSP URLs - use logical view names there.
*
* <p><b>Note:</b> When chaining ViewResolvers, a UrlBasedViewResolver will check whether
* the {@link AbstractUrlBasedView#checkResource specified resource actually exists}.
* However, with {@link InternalResourceView}, it is not generally possible to
* determine the existence of the target resource upfront. In such a scenario,
* a UrlBasedViewResolver will always return View for any given view name;
* as a consequence, it should be configured as the last ViewResolver in the chain.
*
* @author Gorka Vicente
* @since HDIV 2.0.6
* @see #setViewClass
* @see #setPrefix
* @see #setSuffix
* @see #setRequestContextAttribute
* @see #REDIRECT_URL_PREFIX
* @see AbstractUrlBasedView
* @see InternalResourceView
* @see org.springframework.web.servlet.view.velocity.VelocityView
* @see org.springframework.web.servlet.view.freemarker.FreeMarkerView
*/
public class UrlBasedViewResolverHDIV extends UrlBasedViewResolver {
/**
* Overridden to implement check for "redirect:" prefix.
* <p>Not possible in <code>loadView</code>, since overridden
* <code>loadView</code> versions in subclasses might rely on the
* superclass always creating instances of the required view class.
* @see #loadView
* @see #requiredViewClass
*/
@Override
protected View createView(String viewName, Locale locale) throws Exception {
// If this resolver is not supposed to handle the given view,
// return null to pass on to the next resolver in the chain.
if (!canHandle(viewName, locale)) {
return null;
}
// Check for special "redirect:" prefix.
if (viewName.startsWith(REDIRECT_URL_PREFIX)) {
String redirectUrl = viewName.substring(REDIRECT_URL_PREFIX.length());
return new RedirectViewHDIV(redirectUrl, isRedirectContextRelative(), isRedirectHttp10Compatible());
}
// Check for special "forward:" prefix.
if (viewName.startsWith(FORWARD_URL_PREFIX)) {
String forwardUrl = viewName.substring(FORWARD_URL_PREFIX.length());
return new InternalResourceView(forwardUrl);
}
// Else fall back to superclass implementation: calling loadView.
return super.createView(viewName, locale);
}
}