com.crygier.spring.util.web
Class MimeTypeViewResolver

java.lang.Object
  extended by org.springframework.context.support.ApplicationObjectSupport
      extended by org.springframework.web.context.support.WebApplicationObjectSupport
          extended by org.springframework.web.servlet.view.AbstractCachingViewResolver
              extended by com.crygier.spring.util.web.MimeTypeViewResolver
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationContextAware, org.springframework.core.Ordered, org.springframework.web.context.ServletContextAware, org.springframework.web.servlet.ViewResolver

public class MimeTypeViewResolver
extends org.springframework.web.servlet.view.AbstractCachingViewResolver
implements org.springframework.core.Ordered, org.springframework.beans.factory.InitializingBean

Utilizes the MIME Type from the Accept Header of the request to choose the proper view resolver. It will allow you to choose your own mappings by setting the 'mimeTypeToViewResolver' property. The intent here is to allow for extremely simple REST-like interfaces. A sample Spring Setup would be as follows:

   

        
                
                        
                                
                        
                
        
 
If you don't set any mappings, there are several defaults: text/html - org.springframework.web.servlet.view.InternalResourceViewResolver - Default Prefix is /. Override by setting 'defaultInternalResourcePrefix' - Default Suffix is .jsp. Override by setting 'defaultInternalResourceSuffix' application/json - org.springframework.web.servlet.view.json.MappingJacksonJsonView text/xml - org.springframework.oxm.xstream.XStreamMarshaller This means that Controllers should know nothing about Views, and should use the @RequestMapping annotation in this class to tell it how to resolve views that need a name, just as the JSP view resolver. So, to map to a JSP view with the default text/html view handler, setting the annotation "@ResponseMapping("welcome")" on a method will resolve to "/welcome.jsp".


Nested Class Summary
static interface MimeTypeViewResolver.ResponseMapping
           
 
Field Summary
static org.apache.commons.logging.Log logger
           
 
Fields inherited from interface org.springframework.core.Ordered
HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
 
Constructor Summary
MimeTypeViewResolver()
           
 
Method Summary
 void afterPropertiesSet()
           
protected  org.springframework.web.servlet.ViewResolver findViewResolverForMediaType(org.springframework.http.MediaType requestedMediaType)
           
protected  java.lang.Object getCacheKey(java.lang.String viewName, java.util.Locale locale)
           
protected  org.springframework.web.context.request.ServletRequestAttributes getCurrentRequestAttributes()
           
protected  org.springframework.web.servlet.HandlerExecutionChain getHandler(javax.servlet.http.HttpServletRequest request)
           
protected  java.util.List<org.springframework.web.servlet.HandlerMapping> getHandlerMappings()
           
 int getOrder()
           
protected  java.lang.String getViewNameFromRequest()
          Gets the name of the view to resolve from the request.
protected  org.springframework.web.servlet.View loadView(java.lang.String viewName, java.util.Locale locale)
           
 void setDefaultInternalResourcePrefix(java.lang.String defaultInternalResourcePrefix)
          Sets the default InternalResourceViewResolver Prefix for mapping with the default handler for text/html.
 void setDefaultInternalResourceSuffix(java.lang.String defaultInternalResourceSuffix)
          Sets the default InternalResourceViewResolver Suffix for mapping with the default handler for text/html.
 void setMimeTypeToViewResolver(java.util.Map<java.lang.String,org.springframework.web.servlet.ViewResolver> mimeTypeToViewResolver)
           
 void setOrder(int order)
           
 
Methods inherited from class org.springframework.web.servlet.view.AbstractCachingViewResolver
clearCache, createView, isCache, isCacheUnresolved, removeFromCache, resolveViewName, setCache, setCacheUnresolved
 
Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
 
Methods inherited from class org.springframework.context.support.ApplicationObjectSupport
getApplicationContext, getMessageSourceAccessor, initApplicationContext, requiredContextClass, setApplicationContext
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

public static final org.apache.commons.logging.Log logger
Constructor Detail

MimeTypeViewResolver

public MimeTypeViewResolver()
Method Detail

loadView

protected org.springframework.web.servlet.View loadView(java.lang.String viewName,
                                                        java.util.Locale locale)
                                                 throws java.lang.Exception
Specified by:
loadView in class org.springframework.web.servlet.view.AbstractCachingViewResolver
Throws:
java.lang.Exception

findViewResolverForMediaType

protected org.springframework.web.servlet.ViewResolver findViewResolverForMediaType(org.springframework.http.MediaType requestedMediaType)

getViewNameFromRequest

protected java.lang.String getViewNameFromRequest()
                                           throws java.lang.Exception
Gets the name of the view to resolve from the request. First looks to see if there is a ResponseMapping annotation on the method that is called, and uses the value attribute of that. If that is not found, it will use the name of the method that is called.

Returns:
Throws:
java.lang.Exception

getHandlerMappings

protected java.util.List<org.springframework.web.servlet.HandlerMapping> getHandlerMappings()

getHandler

protected org.springframework.web.servlet.HandlerExecutionChain getHandler(javax.servlet.http.HttpServletRequest request)
                                                                    throws java.lang.Exception
Throws:
java.lang.Exception

getCacheKey

protected java.lang.Object getCacheKey(java.lang.String viewName,
                                       java.util.Locale locale)
Overrides:
getCacheKey in class org.springframework.web.servlet.view.AbstractCachingViewResolver

afterPropertiesSet

public void afterPropertiesSet()
                        throws java.lang.Exception
Specified by:
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
Throws:
java.lang.Exception

getCurrentRequestAttributes

protected org.springframework.web.context.request.ServletRequestAttributes getCurrentRequestAttributes()

setMimeTypeToViewResolver

public void setMimeTypeToViewResolver(java.util.Map<java.lang.String,org.springframework.web.servlet.ViewResolver> mimeTypeToViewResolver)

setDefaultInternalResourcePrefix

public void setDefaultInternalResourcePrefix(java.lang.String defaultInternalResourcePrefix)
Sets the default InternalResourceViewResolver Prefix for mapping with the default handler for text/html.

Parameters:
defaultInternalResourcePrefix -

setDefaultInternalResourceSuffix

public void setDefaultInternalResourceSuffix(java.lang.String defaultInternalResourceSuffix)
Sets the default InternalResourceViewResolver Suffix for mapping with the default handler for text/html.

Parameters:
defaultInternalResourceSuffix -

getOrder

public int getOrder()
Specified by:
getOrder in interface org.springframework.core.Ordered

setOrder

public void setOrder(int order)