eval tag tidying

This commit is contained in:
Keith Donald
2010-02-12 17:42:41 +00:00
parent 117b138233
commit 6390e897b8
6 changed files with 205 additions and 50 deletions

View File

@@ -38,6 +38,8 @@ import org.springframework.util.ClassUtils;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.servlet.handler.ConversionServiceHandlerInterceptor;
import org.springframework.web.servlet.handler.MappedInterceptor;
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping;
@@ -94,28 +96,42 @@ class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParser {
annMappingDef.getPropertyValues().add("order", 0);
String annMappingName = parserContext.getReaderContext().registerWithGeneratedName(annMappingDef);
RuntimeBeanReference conversionService = getConversionService(element, source, parserContext);
RuntimeBeanReference validator = getValidator(element, source, parserContext);
RootBeanDefinition bindingDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
bindingDef.setSource(source);
bindingDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
bindingDef.getPropertyValues().add("conversionService", getConversionService(element, source, parserContext));
bindingDef.getPropertyValues().add("validator", getValidator(element, source, parserContext));
bindingDef.getPropertyValues().add("conversionService", conversionService);
bindingDef.getPropertyValues().add("validator", validator);
RootBeanDefinition annAdapterDef = new RootBeanDefinition(AnnotationMethodHandlerAdapter.class);
annAdapterDef.setSource(source);
annAdapterDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
annAdapterDef.getPropertyValues().add("webBindingInitializer", bindingDef);
annAdapterDef.getPropertyValues().add("messageConverters", getMessageConverters(source));
String adapterName = parserContext.getReaderContext().registerWithGeneratedName(annAdapterDef);
String annAdapterName = parserContext.getReaderContext().registerWithGeneratedName(annAdapterDef);
RootBeanDefinition csInterceptorDef = new RootBeanDefinition(ConversionServiceHandlerInterceptor.class);
csInterceptorDef.setSource(source);
csInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(0, conversionService);
RootBeanDefinition mappedCsInterceptorDef = new RootBeanDefinition(MappedInterceptor.class);
mappedCsInterceptorDef.setSource(source);
mappedCsInterceptorDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
mappedCsInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(0, (Object) null);
mappedCsInterceptorDef.getConstructorArgumentValues().addIndexedArgumentValue(1, csInterceptorDef);
String mappedInterceptorName = parserContext.getReaderContext().registerWithGeneratedName(mappedCsInterceptorDef);
parserContext.registerComponent(new BeanComponentDefinition(annMappingDef, annMappingName));
parserContext.registerComponent(new BeanComponentDefinition(annAdapterDef, adapterName));
parserContext.registerComponent(new BeanComponentDefinition(annAdapterDef, annAdapterName));
parserContext.registerComponent(new BeanComponentDefinition(mappedCsInterceptorDef, mappedInterceptorName));
parserContext.popAndRegisterContainingComponent();
return null;
}
private Object getConversionService(Element element, Object source, ParserContext parserContext) {
private RuntimeBeanReference getConversionService(Element element, Object source, ParserContext parserContext) {
if (element.hasAttribute("conversion-service")) {
return new RuntimeBeanReference(element.getAttribute("conversion-service"));
}
@@ -129,7 +145,7 @@ class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParser {
}
}
private Object getValidator(Element element, Object source, ParserContext parserContext) {
private RuntimeBeanReference getValidator(Element element, Object source, ParserContext parserContext) {
if (element.hasAttribute("validator")) {
return new RuntimeBeanReference(element.getAttribute("validator"));
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright 2002-2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.servlet.handler;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.convert.ConversionService;
import org.springframework.util.Assert;
/**
* Interceptor that places the configured {@link ConversionService} in request scope so it's available during request processing.
* Mainly for use within JSP tags such as the spring:eval tag.
* The request attribute name is "org.springframework.core.convert.ConversionService", the value of ConversionService.class.getName();
* @author Keith Donald
* @since 3.0.1
*/
public class ConversionServiceHandlerInterceptor extends HandlerInterceptorAdapter {
private final ConversionService conversionService;
/**
* Creates a new {@link ConversionServiceHandlerInterceptor}.
* @param conversionService the conversion service to export to request scope when this interceptor is invoked.
*/
public ConversionServiceHandlerInterceptor(ConversionService conversionService) {
Assert.notNull(conversionService, "The ConversionService may not be null");
this.conversionService = conversionService;
}
@Override
public final boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws ServletException, IOException {
request.setAttribute(ConversionService.class.getName(), this.conversionService);
return true;
}
}

View File

@@ -20,6 +20,7 @@ import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.el.ELException;
import org.springframework.beans.BeansException;
import org.springframework.core.convert.ConversionService;
@@ -110,7 +111,8 @@ public class EvalTag extends HtmlEscapingAwareTag {
}
}
else {
pageContext.setAttribute(var, expression.getValue(context), scope);
Object result = expression.getValue(context);
pageContext.setAttribute(var, result, scope);
}
return EVAL_PAGE;
}
@@ -127,7 +129,7 @@ public class EvalTag extends HtmlEscapingAwareTag {
private ConversionService getConversionService() {
try {
return (ConversionService) this.pageContext.getRequest().getAttribute("org.springframework.core.convert.ConversionService");
return (ConversionService) this.pageContext.getRequest().getAttribute(ConversionService.class.getName());
} catch (BeansException e) {
return null;
}
@@ -147,19 +149,19 @@ public class EvalTag extends HtmlEscapingAwareTag {
public boolean canRead(EvaluationContext context, Object target,
String name) throws AccessException {
if (name.equals("pageContext")) {
Object implicitVar = resolveImplicitVariable(name);
if (implicitVar != null) {
return true;
}
// TODO support all other JSP implicit variables defined at http://java.sun.com/javaee/6/docs/api/javax/servlet/jsp/el/ImplicitObjectELResolver.html
return this.pageContext.findAttribute(name) != null;
}
public TypedValue read(EvaluationContext context, Object target,
String name) throws AccessException {
if (name.equals("pageContext")) {
return new TypedValue(this.pageContext);
Object implicitVar = resolveImplicitVariable(name);
if (implicitVar != null) {
return new TypedValue(implicitVar);
}
// TODO support all other JSP implicit variables defined at http://java.sun.com/javaee/6/docs/api/javax/servlet/jsp/el/ImplicitObjectELResolver.html
return new TypedValue(this.pageContext.findAttribute(name));
}
@@ -173,6 +175,14 @@ public class EvalTag extends HtmlEscapingAwareTag {
throw new UnsupportedOperationException();
}
private Object resolveImplicitVariable(String name) throws AccessException {
try {
return this.pageContext.getVariableResolver().resolveVariable(name);
} catch (ELException e) {
throw new AccessException("Unexpected exception occurred accessing '" + name + "' as an implicit variable", e);
}
}
}
}