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

public Class DefaultRequestToViewNameTranslator

extends Object
implements RequestToViewNameTranslator
Class Inheritance
All Implemented Interfaces
org.springframework.web.servlet.RequestToViewNameTranslator
Imports
jakarta.servlet.ServletRequest, jakarta.servlet.http.HttpServletRequest, org.springframework.lang.Nullable, org.springframework.util.StringUtils, org.springframework.web.servlet.RequestToViewNameTranslator, org.springframework.web.util.ServletRequestPathUtils

RequestToViewNameTranslator that simply transforms the URI of the incoming request into a view name.

Can be explicitly defined as the viewNameTranslator bean in a org.springframework.web.servlet.DispatcherServlet context. Otherwise, a plain default instance will be used.

The default transformation simply strips leading and trailing slashes as well as the file extension of the URI, and returns the result as the view name with the configured prefix and a suffix added as appropriate.

The stripping of the leading slash and file extension can be disabled using the stripLeadingSlash and stripExtension properties, respectively.

Find below some examples of request to view name translation.

Authors
Rob Harrop, Juergen Hoeller
Since
2.0
See Also
org.springframework.web.servlet.RequestToViewNameTranslator, org.springframework.web.servlet.ViewResolver

Field Summary

Modifier and TypeField and Description
private String
private String
private static final String
private boolean
private boolean
private boolean
private String

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public String
getViewName(HttpServletRequest
the incoming HttpServletRequest providing the context from which a view name is to be resolved
request
)

Implements org.springframework.web.servlet.RequestToViewNameTranslator.getViewName.

Translates the request URI of the incoming HttpServletRequest into the view name based on the configured parameters.

public void
setPrefix(String
the prefix to prepend to generated view names
prefix
)

Set the prefix to prepend to generated view names.

public void
setSeparator(String separator)

Set the value that will replace '/' as the separator in the view name.

public void
setStripExtension(boolean stripExtension)

Set whether file extensions should be stripped from the URI when generating the view name.

public void
setStripLeadingSlash(boolean stripLeadingSlash)

Set whether leading slashes should be stripped from the URI when generating the view name.

public void
setStripTrailingSlash(boolean stripTrailingSlash)

Set whether trailing slashes should be stripped from the URI when generating the view name.

public void
setSuffix(String
the suffix to append to generated view names
suffix
)

Set the suffix to append to generated view names.

protected String

Returns:

the transformed path, with slashes and extensions stripped if desired
transformPath
(String
the lookup path for the current request, as determined by the UrlPathHelper
lookupPath
)

Transform the request URI (in the context of the webapp) stripping slashes and extensions, and replacing the separator as required.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

prefixback to summary
private String prefix
separatorback to summary
private String separator
SLASHback to summary
private static final String SLASH
stripExtensionback to summary
private boolean stripExtension
stripLeadingSlashback to summary
private boolean stripLeadingSlash
stripTrailingSlashback to summary
private boolean stripTrailingSlash
suffixback to summary
private String suffix

Constructor Detail

DefaultRequestToViewNameTranslatorback to summary
public DefaultRequestToViewNameTranslator()

Method Detail

getViewNameback to summary
public String getViewName(HttpServletRequest request)

Implements org.springframework.web.servlet.RequestToViewNameTranslator.getViewName.

Translates the request URI of the incoming HttpServletRequest into the view name based on the configured parameters.

Parameters
request:HttpServletRequest

Doc from org.springframework.web.servlet.RequestToViewNameTranslator.getViewName.

the incoming HttpServletRequest providing the context from which a view name is to be resolved

Returns:String

Doc from org.springframework.web.servlet.RequestToViewNameTranslator.getViewName.

the view name, or null if no default found

Annotations
@Override
Exceptions
IllegalArgumentException:
if neither a parsed RequestPath, nor a String lookupPath have been resolved and cached as a request attribute.
See Also
ServletRequestPathUtils#getCachedPath(ServletRequest), transformPath
setPrefixback to summary
public void setPrefix(String prefix)

Set the prefix to prepend to generated view names.

Parameters
prefix:String

the prefix to prepend to generated view names

setSeparatorback to summary
public void setSeparator(String separator)

Set the value that will replace '/' as the separator in the view name. The default behavior simply leaves '/' as the separator.

setStripExtensionback to summary
public void setStripExtension(boolean stripExtension)

Set whether file extensions should be stripped from the URI when generating the view name. Default is "true".

setStripLeadingSlashback to summary
public void setStripLeadingSlash(boolean stripLeadingSlash)

Set whether leading slashes should be stripped from the URI when generating the view name. Default is "true".

setStripTrailingSlashback to summary
public void setStripTrailingSlash(boolean stripTrailingSlash)

Set whether trailing slashes should be stripped from the URI when generating the view name. Default is "true".

setSuffixback to summary
public void setSuffix(String suffix)

Set the suffix to append to generated view names.

Parameters
suffix:String

the suffix to append to generated view names

transformPathback to summary
protected String transformPath(String lookupPath)

Transform the request URI (in the context of the webapp) stripping slashes and extensions, and replacing the separator as required.

Parameters
lookupPath:String

the lookup path for the current request, as determined by the UrlPathHelper

Returns:String

the transformed path, with slashes and extensions stripped if desired

Annotations
@Nullable