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; 020import javax.servlet.ServletException; 021import javax.servlet.http.HttpServletRequest; 022import javax.servlet.http.HttpServletResponse; 023 024import org.springframework.core.convert.ConversionService; 025import org.springframework.util.Assert; 026 027/** 028 * Interceptor that places the configured {@link ConversionService} in request scope 029 * so it's available during request processing. The request attribute name is 030 * "org.springframework.core.convert.ConversionService", the value of 031 * {@code ConversionService.class.getName()}. 032 * 033 * <p>Mainly for use within JSP tags such as the spring:eval tag. 034 * 035 * @author Keith Donald 036 * @since 3.0.1 037 */ 038public class ConversionServiceExposingInterceptor extends HandlerInterceptorAdapter { 039 040 private final ConversionService conversionService; 041 042 043 /** 044 * Creates a new {@link ConversionServiceExposingInterceptor}. 045 * @param conversionService the conversion service to export to request scope when this interceptor is invoked 046 */ 047 public ConversionServiceExposingInterceptor(ConversionService conversionService) { 048 Assert.notNull(conversionService, "The ConversionService may not be null"); 049 this.conversionService = conversionService; 050 } 051 052 053 @Override 054 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) 055 throws ServletException, IOException { 056 057 request.setAttribute(ConversionService.class.getName(), this.conversionService); 058 return true; 059 } 060 061}