19. Manual Spring MVC, JSP & Spring Boot Auto-Config

ViewResolver, JSP Pipeline & Spring Boot Automation

Master server-rendered MVC using ViewResolver and JSP, WEB-INF security boundaries, and how Spring Boot automates manual MVC boilerplate.

ViewResolver, JSP Pipeline & Spring Boot Automation

In traditional server-rendered web applications, @Controller methods return logical view names that are resolved into physical HTML template resources (like JSP) using a ViewResolver.


1. Server-Rendered MVC Pipeline (ViewResolver)

@Controller
public class HomeController {

    @GetMapping("/home")
    public String renderHome(Model model) {
        model.addAttribute("title", "Spring MVC Masterclass");
        model.addAttribute("message", "Welcome to server-side rendering!");
        return "home"; // Logical view name
    }
}

Configuring InternalResourceViewResolver

In WebConfig.java:

@Bean
public ViewResolver viewResolver() {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    return resolver;
}
LOGICAL VIEW RESOLUTION:
Controller returns "home" ──► ViewResolver appends Prefix + Suffix ──► /WEB-INF/views/home.jsp

2. Model Data Flow & WEB-INF Security Boundary

<%-- /WEB-INF/views/home.jsp --%>
<!DOCTYPE html>
<html>
<head><title>${title}</title></head>
<body>
    <h1>${title}</h1>
    <p>${message}</p>
</body>
</html>

Why Place Views under WEB-INF/views/?

The Servlet specification mandates that files placed inside WEB-INF/ cannot be accessed directly by clients via direct URL requests (e.g., http://localhost:8080/WEB-INF/views/home.jsp returns HTTP 404).

DIRECT CLIENT ACCESS:   GET /WEB-INF/views/home.jsp ──► BLOCKED by Servlet Container
CONTROLLED MVC ACCESS:  GET /home ──► Controller adds Model Data ──► Internal Forward to JSP ──► Rendered HTML

3. How Spring Boot Automates Infrastructure

Spring Boot does NOT replace Spring MVC; it automates the manual bootstrap configuration using spring-boot-starter-web:

MANUAL SPRING MVC:
Developer manually instantiates Tomcat ──► Creates Spring Context ──► Registers DispatcherServlet ──► Configures ViewResolver

SPRING BOOT AUTO-CONFIGURATION:
Developer adds spring-boot-starter-web ──► Writes @SpringBootApplication ──► Spring Boot auto-configures embedded Tomcat & DispatcherServlet

4. Engineering Deep Dive: HashMap In-Memory Data Storage

In educational examples, an in-memory HashMap is often used to store domain objects:

@Repository
public class StudentRepository {
    private final Map<Integer, Student> storage = new HashMap<>();
}

Complexity Comparison

OperationHashMap Lookup ComplexityRelational DB Lookup Complexity (Indexed)
Lookup by Primary Key$O(1)$ average expected$O(\log_B N)$ B-Tree index lookup
Full Collection Traversal (findAll)$O(N)$ iteration$O(N)$ full table scan

[!WARNING] In-memory maps lack durability, transactions, and multithreaded concurrency protection. Replace HashMap with persistent storage engines (MySQL + Spring Data JPA) in production applications.


❓ Interactive Self-Assessment

Knowledge Check

Why are JSP view files placed inside the WEB-INF folder in Java web applications?

Trace Request Execution: GET /students/10

A client sends GET /students/10 to a Spring Boot REST API. Trace the complete end-to-end execution flow across all 8 architectural layers, identifying argument resolution and JSON conversion.

On this page