All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.web.servlet.mvc.UrlFilenameViewController Maven / Gradle / Ivy

There is a newer version: 6.1.6
Show newest version
/*
 * Copyright 2002-2020 the original author or authors.
 *
 * 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
 *
 *      https://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.springframework.web.servlet.mvc;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerMapping;

/**
 * Simple {@code Controller} implementation that transforms the virtual
 * path of a URL into a view name and returns that view.
 *
 * 

Can optionally prepend a {@link #setPrefix prefix} and/or append a * {@link #setSuffix suffix} to build the viewname from the URL filename. * *

Find some examples below: *

    *
  1. {@code "/index" -> "index"}
  2. *
  3. {@code "/index.html" -> "index"}
  4. *
  5. {@code "/index.html"} + prefix {@code "pre_"} and suffix {@code "_suf" -> "pre_index_suf"}
  6. *
  7. {@code "/products/view.html" -> "products/view"}
  8. *
* *

Thanks to David Barri for suggesting prefix/suffix support! * * @author Alef Arendsen * @author Juergen Hoeller * @author Rob Harrop * @see #setPrefix * @see #setSuffix */ public class UrlFilenameViewController extends AbstractUrlViewController { private String prefix = ""; private String suffix = ""; /** Request URL path String to view name String. */ private final Map viewNameCache = new ConcurrentHashMap<>(256); /** * Set the prefix to prepend to the request URL filename * to build a view name. */ public void setPrefix(@Nullable String prefix) { this.prefix = (prefix != null ? prefix : ""); } /** * Return the prefix to prepend to the request URL filename. */ protected String getPrefix() { return this.prefix; } /** * Set the suffix to append to the request URL filename * to build a view name. */ public void setSuffix(@Nullable String suffix) { this.suffix = (suffix != null ? suffix : ""); } /** * Return the suffix to append to the request URL filename. */ protected String getSuffix() { return this.suffix; } /** * Returns view name based on the URL filename, * with prefix/suffix applied when appropriate. * @see #extractViewNameFromUrlPath * @see #setPrefix * @see #setSuffix */ @Override protected String getViewNameForRequest(HttpServletRequest request) { String uri = extractOperableUrl(request); return getViewNameForUrlPath(uri); } /** * Extract a URL path from the given request, * suitable for view name extraction. * @param request current HTTP request * @return the URL to use for view name extraction */ protected String extractOperableUrl(HttpServletRequest request) { String urlPath = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); if (!StringUtils.hasText(urlPath)) { urlPath = getUrlPathHelper().getLookupPathForRequest(request, HandlerMapping.LOOKUP_PATH); } return urlPath; } /** * Returns view name based on the URL filename, * with prefix/suffix applied when appropriate. * @param uri the request URI; for example {@code "/index.html"} * @return the extracted URI filename; for example {@code "index"} * @see #extractViewNameFromUrlPath * @see #postProcessViewName */ protected String getViewNameForUrlPath(String uri) { return this.viewNameCache.computeIfAbsent(uri, u -> postProcessViewName(extractViewNameFromUrlPath(u))); } /** * Extract the URL filename from the given request URI. * @param uri the request URI; for example {@code "/index.html"} * @return the extracted URI filename; for example {@code "index"} */ protected String extractViewNameFromUrlPath(String uri) { int start = (uri.charAt(0) == '/' ? 1 : 0); int lastIndex = uri.lastIndexOf('.'); int end = (lastIndex < 0 ? uri.length() : lastIndex); return uri.substring(start, end); } /** * Build the full view name based on the given view name * as indicated by the URL path. *

The default implementation simply applies prefix and suffix. * This can be overridden, for example, to manipulate upper case * / lower case, etc. * @param viewName the original view name, as indicated by the URL path * @return the full view name to use * @see #getPrefix() * @see #getSuffix() */ protected String postProcessViewName(String viewName) { return getPrefix() + viewName + getSuffix(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy