Add Groovy markup templating support to the MVC config
Issue: SPR-11998
This commit is contained in:
committed by
Rossen Stoyanchev
parent
b18ed6b724
commit
80f4ea13c7
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2002-2014 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.config;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import org.springframework.beans.factory.support.AbstractBeanDefinition;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
|
||||
/**
|
||||
* Parse the <mvc:groovy-markup> MVC namespace element and register a
|
||||
* GroovyConfigurer bean
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 4.1
|
||||
*/
|
||||
public class GroovyMarkupBeanDefinitionParser extends AbstractSimpleBeanDefinitionParser {
|
||||
|
||||
public static final String BEAN_NAME = "mvcGroovyMarkupConfigurer";
|
||||
|
||||
@Override
|
||||
protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) {
|
||||
return BEAN_NAME;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBeanClassName(Element element) {
|
||||
return "org.springframework.web.servlet.view.groovy.GroovyMarkupConfigurer";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean isEligibleAttribute(String attributeName) {
|
||||
return attributeName.equals("resource-loader-path");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -39,6 +39,7 @@ public class MvcNamespaceHandler extends NamespaceHandlerSupport {
|
||||
registerBeanDefinitionParser("tiles", new TilesBeanDefinitionParser());
|
||||
registerBeanDefinitionParser("freemarker", new FreeMarkerBeanDefinitionParser());
|
||||
registerBeanDefinitionParser("velocity", new VelocityBeanDefinitionParser());
|
||||
registerBeanDefinitionParser("groovy-markup", new GroovyMarkupBeanDefinitionParser());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,9 +16,7 @@
|
||||
|
||||
package org.springframework.web.servlet.config;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.MutablePropertyValues;
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
@@ -37,6 +35,7 @@ import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
|
||||
import org.springframework.web.servlet.view.ViewResolverComposite;
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
|
||||
import org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver;
|
||||
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityViewResolver;
|
||||
import org.w3c.dom.Element;
|
||||
@@ -77,7 +76,7 @@ public class ViewResolversBeanDefinitionParser implements BeanDefinitionParser {
|
||||
|
||||
ManagedList<Object> resolvers = new ManagedList<Object>(4);
|
||||
resolvers.setSource(context.extractSource(element));
|
||||
String[] names = new String[] {"jsp", "tiles", "bean-name", "freemarker", "velocity", "bean", "ref"};
|
||||
String[] names = new String[] {"jsp", "tiles", "bean-name", "freemarker", "velocity", "groovy-markup", "bean", "ref"};
|
||||
|
||||
for (Element resolverElement : DomUtils.getChildElementsByTagName(element, names)) {
|
||||
String name = resolverElement.getLocalName();
|
||||
@@ -109,6 +108,11 @@ public class ViewResolversBeanDefinitionParser implements BeanDefinitionParser {
|
||||
else if ("bean-name".equals(name)) {
|
||||
resolverBeanDef = new RootBeanDefinition(BeanNameViewResolver.class);
|
||||
}
|
||||
else if ("groovy-markup".equals(name)) {
|
||||
resolverBeanDef = new RootBeanDefinition(GroovyMarkupViewResolver.class);
|
||||
resolverBeanDef.getPropertyValues().add("suffix", ".tpl");
|
||||
addUrlBasedViewResolverProperties(resolverElement, resolverBeanDef);
|
||||
}
|
||||
else {
|
||||
// Should never happen
|
||||
throw new IllegalStateException("Unexpected element name: " + name);
|
||||
|
||||
@@ -30,6 +30,8 @@ import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
|
||||
import org.springframework.web.servlet.view.groovy.GroovyMarkupConfigurer;
|
||||
import org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver;
|
||||
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
|
||||
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
|
||||
@@ -198,7 +200,7 @@ public class ViewResolverRegistry {
|
||||
|
||||
/**
|
||||
* Register Velocity view resolver with an empty default view name
|
||||
* prefix, a default suffix of ".vm".
|
||||
* prefix and a default suffix of ".vm".
|
||||
*
|
||||
* <p><strong>Note</strong> that you must also configure Velocity by adding a
|
||||
* {@link org.springframework.web.servlet.view.velocity.VelocityConfigurer} bean.
|
||||
@@ -224,6 +226,22 @@ public class ViewResolverRegistry {
|
||||
this.viewResolvers.add(resolver);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a Groovy Markup Template view resolver with an empty default view name
|
||||
* prefix and a default suffix of ".tpl".
|
||||
*/
|
||||
public UrlBasedViewResolverRegistration groovyMarkup() {
|
||||
if (this.applicationContext != null && !hasBeanOfType(GroovyMarkupConfigurer.class)) {
|
||||
throw new BeanInitializationException("In addition to a Groovy Markup Template view resolver " +
|
||||
"there must also be a single GroovyMarkupConfig bean in this web application context " +
|
||||
"(or its parent): GroovyMarkupConfigurer is the usual implementation. " +
|
||||
"This bean may be given any name.");
|
||||
}
|
||||
GroovyMarkupRegistration registration = new GroovyMarkupRegistration();
|
||||
this.viewResolvers.add(registration.getViewResolver());
|
||||
return registration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a {@link ViewResolver} bean instance. This may be useful to
|
||||
* configure a custom (or 3rd party) resolver implementation. It may also be
|
||||
@@ -282,4 +300,12 @@ public class ViewResolverRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
private static class GroovyMarkupRegistration extends UrlBasedViewResolverRegistration {
|
||||
|
||||
private GroovyMarkupRegistration() {
|
||||
super(new GroovyMarkupViewResolver());
|
||||
getViewResolver().setSuffix(".tpl");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user