Understanding View Resolution and Navigation in Spring MVC
Spring MVC supports a variety of view technologies for rendering responses. The framework's primary responsibility is to process a request, populate a model, and then delegate to a view for rendering. The view is responsible for presenting the model data to the user in a specific format, such as HTML, XML, or JSON.
JSP Views
JavaServer Pages (JSP) is a common view technology. To configure JSP support, define an InternalResourceViewResolver bean in your Spring configuration. This resolver maps logical view names returned by controller methods to physical JSP file paths.
When multiple view resolvers are configured, their order determines priority. Resolvers with a lower
ordervalue are consulted first. Once a resolver successfully resolves a view name, subsequent resolvers are not invoked.
<!-- Configuration for JSP View Resolver -->
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- Path prefix for JSP files -->
<property name="prefix" value="/WEB-INF/views/"/>
<!-- File extension suffix -->
<property name="suffix" value=".jsp"/>
<!-- Priority order among resolvers -->
<property name="order" value="10"/>
</bean>
Thymeleaf Views
Thymeleaf is a modern server-side Java template engine that works with HTML5. To integrate Thymeleaf, you need to add the required dependency and configure a ThymeleafViewResolver.
Required Dependency
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.15.RELEASE</version>
</dependency>
Spring Configuration
<bean id="thymeleafResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="order" value="5"/>
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine">
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/"/>
<property name="suffix" value=".html"/>
<property name="templateMode" value="HTML"/>
<property name="characterEncoding" value="UTF-8" />
</bean>
</property>
</bean>
</property>
</bean>
FreeMarker Views
FreeMarker is a template engine for generating text output (HTML, XML, etc.). Configure a FreeMarkerViewResolver to use FreeMarker templates.
<!-- Configuration for FreeMarker View Resolver -->
<bean id="freemarkerResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="prefix" value="/WEB-INF/ftl/"/>
<property name="suffix" value=".ftlh"/>
<property name="order" value="15"/>
</bean>
XSLT Views
XSLT (Extensbile Stylesheet Language Transformations) can transform XML data into other formats like HTML. Use an XsltViewResolver for XSLT-based views.
<!-- Configuration for XSLT View Resolver -->
<bean id="xsltResolver" class="org.springframework.web.servlet.view.xslt.XsltViewResolver">
<property name="prefix" value="/WEB-INF/xslt/"/>
<property name="suffix" value=".xsl"/>
<property name="order" value="20"/>
</bean>
Forwarding and Redirecting Requests
Spring MVC provides two primary mechanisms for navigating to different resources: forwarding and redirecting.
Request Forwarding
Forwarding is a server-side operation where the request is passed to another resource within the same application context. The client's browser URL does not change.
@Controller
public class NavigationController {
@GetMapping("/processForm")
public String handleFormSubmission() {
// Forward the request to the /displayResult endpoint
return "forward:/displayResult";
}
}
Request Redirecting
Redirecting is a client-side operation. The server sends a response instructing the browser to make a new request to a different URL. The browser's address bar updates to the new URL.
@Controller
public class NavigationController {
@PostMapping("/submitData")
public String processData() {
// Redirect the client to the /confirmation page
return "redirect:/confirmation";
}
}
Key distinction: A forward involves a single request/response cycle within the server. A redirect results in two seperate requests from the client.
View Controllers
View controlllers offer a simplified way to map URLs directly to view names without writing a dedicated controller method. This is useful to static pages.
Instead of writing a controller method:
@Controller
public class SimpleController {
@GetMapping("/welcome")
public String showWelcomePage() {
return "home"; // Resolves to home.jsp or home.html
}
}
You can configure it directly in the Spring MVC configuration file:
<mvc:view-controller path="/welcome" view-name="home"/>
Important Note: When <mvc:view-controller> is used, request mappings in annotated controllers might be bypassed. To ensure both view controllers and annotated controllers work together, you must enable the MVC annotation-driven configuration:
<mvc:annotation-driven />