How does the InternalResourceViewResolver function?

InternalResourceViewResolver is a Spring MVC ViewResolver implementation that resolves logical view names into JSP files by combining a configured prefix and suffix around the view name. It's the standard resolver for JSP-based applications.

Key Points: • It concatenates a prefix (a directory path) and a suffix (typically .jsp) around the logical view name returned by a controller. • Views under /WEB-INF/ are protected from direct browser access, which is why the prefix commonly points there for security. • Because it forwards internally to the resource rather than redirecting, the URL in the browser doesn't change when the view renders. • It's specific to JSP-style forward-based views; other view technologies like Thymeleaf use their own dedicated resolver instead. • It's typically registered once as a bean, applying its prefix/suffix convention to every logical view name in the application.

Example: With prefix /WEB-INF/views/ and suffix .jsp configured, a controller returning the view name "home" causes InternalResourceViewResolver to forward the request to /WEB-INF/views/home.jsp for rendering.

Code Example:

@Bean
public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    return resolver;
}

Interview Tip: A concise interview answer is:

"InternalResourceViewResolver builds the actual JSP file path by wrapping a controller's logical view name with a configured prefix and suffix, then forwards the request to it. Pointing the prefix at /WEB-INF/ also keeps the raw JSP files from being requested directly by a browser."