Top Description Fields Constructors Methods
org.springframework.web.servlet.mvc

public Class UrlFilenameViewController

extends AbstractUrlViewController
Class Inheritance
Imports
java.util.Map, java.util.concurrent.ConcurrentHashMap, jakarta.servlet.http.HttpServletRequest, org.springframework.lang.Nullable, org.springframework.util.StringUtils, org.springframework.web.servlet.HandlerMapping, org.springframework.web.util.ServletRequestPathUtils

Simple Controller implementation that transforms the virtual path of a URL into a view name and returns that view.

Can optionally prepend a prefix and/or append a suffix to build the viewname from the URL filename.

Find some examples below:

  1. "/index" -> "index"
  2. "/index.html" -> "index"
  3. "/index.html" + prefix "pre_" and suffix "_suf" -> "pre_index_suf"
  4. "/products/view.html" -> "products/view"

Thanks to David Barri for suggesting prefix/suffix support!

Authors
Alef Arendsen, Juergen Hoeller, Rob Harrop
See Also
setPrefix, setSuffix

Field Summary

Modifier and TypeField and Description
private String
private String
private final Map<String, String>
viewNameCache

Request URL path String to view name String.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected String

Returns:

the URL to use for view name extraction
extractOperableUrl
(HttpServletRequest
current HTTP request
request
)

Extract a URL path from the given request, suitable for view name extraction.

protected String

Returns:

the extracted URI filename; for example "index"
extractViewNameFromUrlPath
(String
the request URI; for example "/index.html"
uri
)

Extract the URL filename from the given request URI.

protected String
getPrefix()

Return the prefix to prepend to the request URL filename.

protected String
getSuffix()

Return the suffix to append to the request URL filename.

protected String
getViewNameForRequest(HttpServletRequest
current HTTP request
request
)

Implements abstract org.springframework.web.servlet.mvc.AbstractUrlViewController.getViewNameForRequest.

Returns view name based on the URL filename, with prefix/suffix applied when appropriate.

protected String

Returns:

the extracted URI filename; for example "index"
getViewNameForUrlPath
(String
the request URI; for example "/index.html"
uri
)

Returns view name based on the URL filename, with prefix/suffix applied when appropriate.

protected String

Returns:

the full view name to use
postProcessViewName
(String
the original view name, as indicated by the URL path
viewName
)

Build the full view name based on the given view name as indicated by the URL path.

public void
setPrefix(String prefix)

Set the prefix to prepend to the request URL filename to build a view name.

public void
setSuffix(String suffix)

Set the suffix to append to the request URL filename to build a view name.

Inherited from org.springframework.web.servlet.mvc.AbstractUrlViewController:
getUrlPathHelperhandleRequestInternalsetAlwaysUseFullPathsetRemoveSemicolonContentsetUrlDecodesetUrlPathHelper

Field Detail

prefixback to summary
private String prefix
suffixback to summary
private String suffix
viewNameCacheback to summary
private final Map<String, String> viewNameCache

Request URL path String to view name String.

Constructor Detail

UrlFilenameViewControllerback to summary
public UrlFilenameViewController()

Method Detail

extractOperableUrlback to summary
protected String extractOperableUrl(HttpServletRequest request)

Extract a URL path from the given request, suitable for view name extraction.

Parameters
request:HttpServletRequest

current HTTP request

Returns:String

the URL to use for view name extraction

extractViewNameFromUrlPathback to summary
protected String extractViewNameFromUrlPath(String uri)

Extract the URL filename from the given request URI.

Parameters
uri:String

the request URI; for example "/index.html"

Returns:String

the extracted URI filename; for example "index"

getPrefixback to summary
protected String getPrefix()

Return the prefix to prepend to the request URL filename.

getSuffixback to summary
protected String getSuffix()

Return the suffix to append to the request URL filename.

getViewNameForRequestback to summary
protected String getViewNameForRequest(HttpServletRequest request)

Implements abstract org.springframework.web.servlet.mvc.AbstractUrlViewController.getViewNameForRequest.

Returns view name based on the URL filename, with prefix/suffix applied when appropriate.

Parameters
request:HttpServletRequest

Doc from org.springframework.web.servlet.mvc.AbstractUrlViewController.getViewNameForRequest.

current HTTP request

Returns:String

Doc from org.springframework.web.servlet.mvc.AbstractUrlViewController.getViewNameForRequest.

a view name for this request (never null)

Annotations
@Override
See Also
extractViewNameFromUrlPath, setPrefix, setSuffix
getViewNameForUrlPathback to summary
protected String getViewNameForUrlPath(String uri)

Returns view name based on the URL filename, with prefix/suffix applied when appropriate.

Parameters
uri:String

the request URI; for example "/index.html"

Returns:String

the extracted URI filename; for example "index"

See Also
extractViewNameFromUrlPath, postProcessViewName
postProcessViewNameback to summary
protected String postProcessViewName(String viewName)

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.

Parameters
viewName:String

the original view name, as indicated by the URL path

Returns:String

the full view name to use

See Also
getPrefix(), getSuffix()
setPrefixback to summary
public void setPrefix(String prefix)

Set the prefix to prepend to the request URL filename to build a view name.

setSuffixback to summary
public void setSuffix(String suffix)

Set the suffix to append to the request URL filename to build a view name.