001/* 002 * Copyright 2002-2015 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.servlet.view; 018 019import org.springframework.util.ClassUtils; 020 021/** 022 * Convenient subclass of {@link UrlBasedViewResolver} that supports 023 * {@link InternalResourceView} (i.e. Servlets and JSPs) and subclasses 024 * such as {@link JstlView}. 025 * 026 * <p>The view class for all views generated by this resolver can be specified 027 * via {@link #setViewClass}. See {@link UrlBasedViewResolver}'s javadoc for details. 028 * The default is {@link InternalResourceView}, or {@link JstlView} if the 029 * JSTL API is present. 030 * 031 * <p>BTW, it's good practice to put JSP files that just serve as views under 032 * WEB-INF, to hide them from direct access (e.g. via a manually entered URL). 033 * Only controllers will be able to access them then. 034 * 035 * <p><b>Note:</b> When chaining ViewResolvers, an InternalResourceViewResolver 036 * always needs to be last, as it will attempt to resolve any view name, 037 * no matter whether the underlying resource actually exists. 038 * 039 * @author Juergen Hoeller 040 * @since 17.02.2003 041 * @see #setViewClass 042 * @see #setPrefix 043 * @see #setSuffix 044 * @see #setRequestContextAttribute 045 * @see InternalResourceView 046 * @see JstlView 047 */ 048public class InternalResourceViewResolver extends UrlBasedViewResolver { 049 050 private static final boolean jstlPresent = ClassUtils.isPresent( 051 "javax.servlet.jsp.jstl.core.Config", InternalResourceViewResolver.class.getClassLoader()); 052 053 private Boolean alwaysInclude; 054 055 056 /** 057 * Sets the default {@link #setViewClass view class} to {@link #requiredViewClass}: 058 * by default {@link InternalResourceView}, or {@link JstlView} if the JSTL API 059 * is present. 060 */ 061 public InternalResourceViewResolver() { 062 Class<?> viewClass = requiredViewClass(); 063 if (InternalResourceView.class == viewClass && jstlPresent) { 064 viewClass = JstlView.class; 065 } 066 setViewClass(viewClass); 067 } 068 069 /** 070 * A convenience constructor that allows for specifying {@link #setPrefix prefix} 071 * and {@link #setSuffix suffix} as constructor arguments. 072 * @param prefix the prefix that gets prepended to view names when building a URL 073 * @param suffix the suffix that gets appended to view names when building a URL 074 * @since 4.3 075 */ 076 public InternalResourceViewResolver(String prefix, String suffix) { 077 this(); 078 setPrefix(prefix); 079 setSuffix(suffix); 080 } 081 082 083 /** 084 * This resolver requires {@link InternalResourceView}. 085 */ 086 @Override 087 protected Class<?> requiredViewClass() { 088 return InternalResourceView.class; 089 } 090 091 /** 092 * Specify whether to always include the view rather than forward to it. 093 * <p>Default is "false". Switch this flag on to enforce the use of a 094 * Servlet include, even if a forward would be possible. 095 * @see InternalResourceView#setAlwaysInclude 096 */ 097 public void setAlwaysInclude(boolean alwaysInclude) { 098 this.alwaysInclude = alwaysInclude; 099 } 100 101 102 @Override 103 protected AbstractUrlBasedView buildView(String viewName) throws Exception { 104 InternalResourceView view = (InternalResourceView) super.buildView(viewName); 105 if (this.alwaysInclude != null) { 106 view.setAlwaysInclude(this.alwaysInclude); 107 } 108 view.setPreventDispatchLoop(true); 109 return view; 110 } 111 112}