I want to have the same request mapping but resolve to different view depending on the file extension. I have two JSPs one that renders HTML and another that renders XML. Depending on the file extension I should resolve to the corresponding jsp.
This is my controller:
@Controller
public class FileManagementController {
@RequestMapping(value="/filemanagements", method=RequestMethod.GET)
public ModelAndView list() {
//if file extension .xml return /filemanagement/listXml
//if no file extension present return /filemanagement/list
}
}
And I Have the following y Root of my WebApp:
/jsp/filemanagement/list.jsp
<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
..
/jsp/filemanagement/listXml.jsp
<?xml version="1.0" encoding="UTF-8"?>
<%@page contentType="text/xml" pageEncoding="UTF-8"%>
….
This is how I have configured my ViewResolver in the servletContext.xml:
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
Have a look at
ContentNegotiatingViewResolver. From the javadoc:There is also a section of the ref manual covering this resolver.