I have read quite a few posts on ViewResolvers in Spring and I still have a question. I have a DispatcherServlet named dispatcher in my deployment descriptor and it maps “/” all URLs to view resolvers in dispatcher-servlet.xml. I have only one right now.
<bean id="jspViewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/"
p:suffix=".jsp"/>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>jsp/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
And a controller:
@Controller
public class ItemsController {
@RequestMapping("/service")
public String service()
{
System.out.println("From ItemsController...");
return "service";
}
@RequestMapping("/")
public String home()
{
System.out.println("From ItemsController...");
return "index";
}
}
So jspViewResolver returns, in case /service is requested, “WEB-INF/jsp/service.jsp”. That’s fine. But if I want to map requests to html or xhtml files. What need I do? Add another dispatcher? Please, help me out here. Thank you so much in advance.
See section 15.5.2. Chaining ViewResolvers of the Spring documentation. This allows you to use multiple
InternalResourceViewResolverinstances.You can also add other view resolvers to your
applicationContext, e.g.,Then in your annotated controller, when you return the string
"someView"the bean with the same id will be used as your view instead of your jsp resolver. When names are conflicting you have to use theorderproperty to set which resolver gets precedence.