开发者

Two differents views depending on the URL file extension using Spring MVC 3

开发者 https://www.devze.com 2023-03-16 15:19 出处:网络
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 extensi

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 contentTyp开发者_如何转开发e="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:

This view resolver uses the requested media type to select a suitable View for a request.

If the requested path has a file extension and if the setFavorPathExtension(boolean) property is true, the mediaTypes property is inspected for a matching media type.

There is also a section of the ref manual covering this resolver.


Following skaffman advice. I did solve this using ContentNegotiatingViewResolver.

servletContext.xml:

<bean id="resourceResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver"/>

<bean id="viewResolver"
  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
</bean>

<mvc:annotation-driven/>

The resolvers are picked up automatically by ContentNegotiatingViewResolver.

Controller:

@Controller
@RequestMapping("/filemanagements")
public class FileManagementController {

    @RequestMapping(method=RequestMethod.GET)
    public ModelAndView list(@RequestParam(required=false) String contentType) {
        return new ModelAndView(baseLogicView + "/list");
    }

    private static final String baseLogicView = "/filemanagement";
}

These jsps:

/WEB-INF/jsp/filemanagements/list.jsp
/WEB-INF/jsp/filemanagements/listXml.jsp

Resource bundle views.properties:

/filemanagement/list.(class)=org.springframework.web.servlet.view.JstlView
/filemanagement/list.url=/WEB-INF/jsp/filemanagement/listXml.jsp
/filemanagement/list.contentType=text/xml

It was necessary to specify the contentType there because if you don't it defaults to this "text/html;charset=ISO-8859-1" even if you set <%@ page contentType="text/xml" %> in the JSP that renders the XML.

After that I could do the following requests:

http://localhost:8080/filemanagement-web/filemanagements (list.jsp)
http://localhost:8080/filemanagement-web/filemanagements.html (list.jsp)
http://localhost:8080/filemanagement-web/filemanagements.xml (listXml.jsp)
0

精彩评论

暂无评论...
验证码 换一张
取 消