001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.servlet.handler; 018 019import java.io.IOException; 020 021import javax.servlet.ServletException; 022import javax.servlet.http.HttpServletRequest; 023import javax.servlet.http.HttpServletResponse; 024 025import org.springframework.core.convert.ConversionService; 026import org.springframework.util.Assert; 027 028/** 029 * Interceptor that places the configured {@link ConversionService} in request scope 030 * so it's available during request processing. The request attribute name is 031 * "org.springframework.core.convert.ConversionService", the value of 032 * {@code ConversionService.class.getName()}. 033 * 034 * <p>Mainly for use within JSP tags such as the spring:eval tag. 035 * 036 * @author Keith Donald 037 * @since 3.0.1 038 */ 039public class ConversionServiceExposingInterceptor extends HandlerInterceptorAdapter { 040 041 private final ConversionService conversionService; 042 043 044 /** 045 * Creates a new {@link ConversionServiceExposingInterceptor}. 046 * @param conversionService the conversion service to export to request scope when this interceptor is invoked 047 */ 048 public ConversionServiceExposingInterceptor(ConversionService conversionService) { 049 Assert.notNull(conversionService, "The ConversionService may not be null"); 050 this.conversionService = conversionService; 051 } 052 053 054 @Override 055 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) 056 throws ServletException, IOException { 057 058 request.setAttribute(ConversionService.class.getName(), this.conversionService); 059 return true; 060 } 061 062}