Spring MVC JSP View Resolver is a way to configure the jsp view names using the org.springframework.web.servlet.view.InternalResourceViewResolver.
InternalResourceViewResolver has 2 properties that are very useful
1. prefix
2. suffix.
These 2 properties help in determining the complete path of the view.
Lets see steps to configure the jsp view resolver.
1. Dispatcher servlet
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.kscodes.sampleproject" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans> |
2. Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.kscodes.sampleproject.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class JspViewResolverController { @RequestMapping(value = "/home") public String showHome() { return "home"; } } |
Output
The dispatcher servlet has prefix as WEB-INF and suffix as .jsp, so when the controller returns the string as home, it will search for the file WEB-INF/home.jsp and if it finds the file, then it will display that jsp.