SPR-6413 and SPR-6414 first cut: needs review, particularly compatibility note in AbstractHandlerMapping
This commit is contained in:
parent
6c89946d42
commit
de1d548725
|
|
@ -16,20 +16,16 @@
|
|||
|
||||
package org.springframework.web.servlet.config;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.RuntimeBeanReference;
|
||||
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
|
||||
import org.springframework.beans.factory.parsing.CompositeComponentDefinition;
|
||||
import org.springframework.beans.factory.support.ManagedList;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.beans.factory.xml.BeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.format.support.FormattingConversionServiceFactoryBean;
|
||||
import org.springframework.util.ClassUtils;
|
||||
import org.springframework.util.xml.DomUtils;
|
||||
import org.springframework.validation.Validator;
|
||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
|
||||
|
|
@ -56,44 +52,34 @@ import org.w3c.dom.Element;
|
|||
* @author Juergen Hoeller
|
||||
* @since 3.0
|
||||
*/
|
||||
class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParser {
|
||||
public class AnnotationDrivenBeanDefinitionParser implements BeanDefinitionParser {
|
||||
|
||||
private static final boolean jsr303Present = ClassUtils.isPresent(
|
||||
"javax.validation.Validator", AnnotationDrivenBeanDefinitionParser.class.getClassLoader());
|
||||
"javax.validation.Validator", InterceptorsBeanDefinitionParser.class.getClassLoader());
|
||||
|
||||
|
||||
public BeanDefinition parse(Element element, ParserContext parserContext) {
|
||||
Object source = parserContext.extractSource(element);
|
||||
|
||||
RootBeanDefinition mappingDef = new RootBeanDefinition(DefaultAnnotationHandlerMapping.class);
|
||||
mappingDef.setSource(source);
|
||||
mappingDef.getPropertyValues().add("order", 0);
|
||||
String mappingName = parserContext.getReaderContext().registerWithGeneratedName(mappingDef);
|
||||
RootBeanDefinition annMappingDef = new RootBeanDefinition(DefaultAnnotationHandlerMapping.class);
|
||||
annMappingDef.setSource(source);
|
||||
annMappingDef.getPropertyValues().add("order", 0);
|
||||
String annMappingName = parserContext.getReaderContext().registerWithGeneratedName(annMappingDef);
|
||||
|
||||
Element interceptors = DomUtils.getChildElementByTagName(element, "interceptors");
|
||||
if (interceptors != null) {
|
||||
List<Element> beans = DomUtils.getChildElementsByTagName(interceptors, "bean");
|
||||
List<BeanDefinition> interceptorBeans = new ManagedList<BeanDefinition>(beans.size());
|
||||
for (Element bean : beans) {
|
||||
interceptorBeans.add(parserContext.getDelegate().parseBeanDefinitionElement(bean).getBeanDefinition());
|
||||
}
|
||||
mappingDef.getPropertyValues().add("interceptors", interceptorBeans);
|
||||
}
|
||||
|
||||
RootBeanDefinition bindingDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
|
||||
bindingDef.setSource(source);
|
||||
bindingDef.getPropertyValues().add("conversionService", getConversionService(element, source, parserContext));
|
||||
bindingDef.getPropertyValues().add("validator", getValidator(element, source, parserContext));
|
||||
|
||||
RootBeanDefinition adapterDef = new RootBeanDefinition(AnnotationMethodHandlerAdapter.class);
|
||||
adapterDef.setSource(source);
|
||||
adapterDef.getPropertyValues().add("webBindingInitializer", bindingDef);
|
||||
String adapterName = parserContext.getReaderContext().registerWithGeneratedName(adapterDef);
|
||||
RootBeanDefinition annAdapterDef = new RootBeanDefinition(AnnotationMethodHandlerAdapter.class);
|
||||
annAdapterDef.setSource(source);
|
||||
annAdapterDef.getPropertyValues().add("webBindingInitializer", bindingDef);
|
||||
String adapterName = parserContext.getReaderContext().registerWithGeneratedName(annAdapterDef);
|
||||
|
||||
CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source);
|
||||
parserContext.pushContainingComponent(compDefinition);
|
||||
parserContext.registerComponent(new BeanComponentDefinition(mappingDef, mappingName));
|
||||
parserContext.registerComponent(new BeanComponentDefinition(adapterDef, adapterName));
|
||||
parserContext.registerComponent(new BeanComponentDefinition(annMappingDef, annMappingName));
|
||||
parserContext.registerComponent(new BeanComponentDefinition(annAdapterDef, adapterName));
|
||||
parserContext.popAndRegisterContainingComponent();
|
||||
|
||||
return null;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* Copyright 2002-2009 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 java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.BeanDefinitionHolder;
|
||||
import org.springframework.beans.factory.xml.BeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.util.xml.DomUtils;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.beans.factory.xml.BeanDefinitionParser} that parses the {@code interceptors} element to configure
|
||||
* a set of global Spring MVC {@link HandlerInterceptor HandlerInterceptors}.
|
||||
* The set is expected to be configured by type on each registered HandlerMapping.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
class InterceptorsBeanDefinitionParser implements BeanDefinitionParser {
|
||||
|
||||
public BeanDefinition parse(Element element, ParserContext parserContext) {
|
||||
List<Element> beans = DomUtils.getChildElementsByTagName(element, "bean");
|
||||
for (Element bean : beans) {
|
||||
BeanDefinitionHolder beanHolder = parserContext.getDelegate().parseBeanDefinitionElement(bean);
|
||||
parserContext.getDelegate().decorateBeanDefinitionIfRequired(bean, beanHolder);
|
||||
parserContext.getReaderContext().registerWithGeneratedName(beanHolder.getBeanDefinition());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -29,6 +29,8 @@ public class MvcNamespaceHandler extends NamespaceHandlerSupport {
|
|||
|
||||
public void init() {
|
||||
registerBeanDefinitionParser("annotation-driven", new AnnotationDrivenBeanDefinitionParser());
|
||||
registerBeanDefinitionParser("interceptors", new InterceptorsBeanDefinitionParser());
|
||||
registerBeanDefinitionParser("view-controller", new ViewControllerBeanDefinitionParser());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
* Copyright 2002-2009 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 java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.support.ManagedMap;
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.beans.factory.xml.BeanDefinitionParser;
|
||||
import org.springframework.beans.factory.xml.ParserContext;
|
||||
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
||||
import org.springframework.web.servlet.mvc.ParameterizableViewController;
|
||||
import org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.beans.factory.xml.BeanDefinitionParser} that parses a {@code view-controller} element to register
|
||||
* a {@link ParameterizableViewController}. Will also register a {@link SimpleUrlHandlerMapping} for view controllers.
|
||||
*
|
||||
* @author Keith Donald
|
||||
* @since 3.0
|
||||
*/
|
||||
class ViewControllerBeanDefinitionParser implements BeanDefinitionParser {
|
||||
|
||||
private String handlerAdapterBeanName;
|
||||
|
||||
private String handlerMappingBeanName;
|
||||
|
||||
public BeanDefinition parse(Element element, ParserContext parserContext) {
|
||||
Object source = parserContext.extractSource(element);
|
||||
if (this.handlerAdapterBeanName == null) {
|
||||
RootBeanDefinition handlerAdapterDef = new RootBeanDefinition(SimpleControllerHandlerAdapter.class);
|
||||
handlerAdapterDef.setSource(source);
|
||||
this.handlerAdapterBeanName = parserContext.getReaderContext().registerWithGeneratedName(handlerAdapterDef);
|
||||
}
|
||||
RootBeanDefinition handlerMappingDef;
|
||||
if (this.handlerMappingBeanName == null) {
|
||||
handlerMappingDef = new RootBeanDefinition(SimpleUrlHandlerMapping.class);
|
||||
handlerMappingDef.setSource(source);
|
||||
handlerMappingDef.getPropertyValues().add("order", "1");
|
||||
this.handlerMappingBeanName = parserContext.getReaderContext().registerWithGeneratedName(handlerMappingDef);
|
||||
} else {
|
||||
handlerMappingDef = (RootBeanDefinition) parserContext.getReaderContext().getRegistry().getBeanDefinition(this.handlerMappingBeanName);
|
||||
}
|
||||
RootBeanDefinition viewControllerDef = new RootBeanDefinition(ParameterizableViewController.class);
|
||||
viewControllerDef.setSource(source);
|
||||
if (element.hasAttribute("view-name")) {
|
||||
viewControllerDef.getPropertyValues().add("viewName", element.getAttribute("view-name"));
|
||||
}
|
||||
Map<String, BeanDefinition> urlMap;
|
||||
if (handlerMappingDef.getPropertyValues().contains("urlMap")) {
|
||||
urlMap = (Map<String, BeanDefinition>) handlerMappingDef.getPropertyValues().getPropertyValue("urlMap").getValue();
|
||||
} else {
|
||||
urlMap = new ManagedMap<String, BeanDefinition>();
|
||||
handlerMappingDef.getPropertyValues().add("urlMap", urlMap);
|
||||
}
|
||||
urlMap.put(element.getAttribute("path"), viewControllerDef);
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -19,6 +19,7 @@ package org.springframework.web.servlet.handler;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
|
|
@ -130,6 +131,12 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
|
|||
* @see #adaptInterceptor
|
||||
*/
|
||||
protected void initInterceptors() {
|
||||
// TODO consider impact on backwards compatibility here
|
||||
Map<String, HandlerInterceptor> globalInterceptors = getApplicationContext().getBeansOfType(HandlerInterceptor.class);
|
||||
if (globalInterceptors != null) {
|
||||
this.interceptors.addAll(globalInterceptors.values());
|
||||
}
|
||||
|
||||
if (!this.interceptors.isEmpty()) {
|
||||
this.adaptedInterceptors = new HandlerInterceptor[this.interceptors.size()];
|
||||
for (int i = 0; i < this.interceptors.size(); i++) {
|
||||
|
|
|
|||
|
|
@ -56,14 +56,15 @@ import org.springframework.web.servlet.ModelAndView;
|
|||
* <td>viewName</td>
|
||||
* <td><i>null</i></td>
|
||||
* <td>the name of the view the viewResolver will use to forward to
|
||||
* (if this property is not set, an exception will be thrown during
|
||||
* initialization)</td>
|
||||
* (if this property is not set, a null view name will be returned
|
||||
* directing the caller to calculate the view name from the current request)</td>
|
||||
* </tr>
|
||||
* </table>
|
||||
* </p>
|
||||
*
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public class ParameterizableViewController extends AbstractController {
|
||||
|
||||
|
|
@ -84,14 +85,6 @@ public class ParameterizableViewController extends AbstractController {
|
|||
return this.viewName;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initApplicationContext() {
|
||||
if (this.viewName == null) {
|
||||
throw new IllegalArgumentException("Property 'viewName' is required");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return a ModelAndView object with the specified view name.
|
||||
* @see #getViewName()
|
||||
|
|
@ -99,7 +92,6 @@ public class ParameterizableViewController extends AbstractController {
|
|||
@Override
|
||||
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
|
||||
throws Exception {
|
||||
|
||||
return new ModelAndView(getViewName());
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,22 +16,6 @@
|
|||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="interceptors" minOccurs="0">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation source="java:org.springframework.web.servlet.HandlerInterceptor"><![CDATA[
|
||||
The ordered list of interceptors that intercept HTTP Servlet Requests mapped to Controllers.
|
||||
Interceptors allow a request to be pre/post processed before/after handling.
|
||||
Each inteceptor should be configured as an inner bean that implements either the org.springframework.web.servlet.HandlerInterceptor or org.springframework.web.context.request.WebRequestInterceptor interface.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element ref="beans:bean" maxOccurs="unbounded" />
|
||||
</xsd:sequence>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="conversion-service" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation source="java:org.springframework.core.convert.ConversionService"><![CDATA[
|
||||
|
|
@ -64,4 +48,46 @@
|
|||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:element name="interceptors">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation source="java:org.springframework.web.servlet.HandlerInterceptor"><![CDATA[
|
||||
The ordered set of interceptors that intercept HTTP Servlet Requests handled by Controllers.
|
||||
Interceptors allow a request to be pre/post processed before/after handling.
|
||||
Each inteceptor should be configured as an inner bean that implements either the org.springframework.web.servlet.HandlerInterceptor or org.springframework.web.context.request.WebRequestInterceptor interface.
|
||||
The interceptors in this set are automatically configured on each registered HandlerMapping.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element ref="beans:bean" maxOccurs="unbounded" />
|
||||
</xsd:sequence>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
<xsd:element name="view-controller">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation
|
||||
source="java:org.springframework.web.servlet.mvc.ParameterizableViewController"><![CDATA[
|
||||
Defines a simple Controller that selects a view to render the response.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="path" type="xsd:string" use="required">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
The URL path the view is mapped to.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
<xsd:attribute name="view-name" type="xsd:string">
|
||||
<xsd:annotation>
|
||||
<xsd:documentation><![CDATA[
|
||||
The name of the view to render. Optional.
|
||||
If not specified, the view name will be determined from the current HttpServletRequest by the DispatcherServlet's RequestToViewNameTranslator.
|
||||
]]></xsd:documentation>
|
||||
</xsd:annotation>
|
||||
</xsd:attribute>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
|
||||
</xsd:schema>
|
||||
|
|
@ -19,6 +19,7 @@ package org.springframework.web.servlet.config;
|
|||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Date;
|
||||
|
|
@ -34,7 +35,6 @@ import org.springframework.context.i18n.LocaleContextHolder;
|
|||
import org.springframework.core.convert.ConversionFailedException;
|
||||
import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.style.StylerUtils;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.format.annotation.DateTimeFormat.ISO;
|
||||
import org.springframework.format.support.FormattingConversionServiceFactoryBean;
|
||||
|
|
@ -50,7 +50,10 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.context.support.GenericWebApplicationContext;
|
||||
import org.springframework.web.servlet.HandlerExecutionChain;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
||||
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
|
||||
import org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter;
|
||||
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
|
||||
import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping;
|
||||
import org.springframework.web.servlet.theme.ThemeChangeInterceptor;
|
||||
|
|
@ -79,6 +82,7 @@ public class MvcNamespaceTests {
|
|||
DefaultAnnotationHandlerMapping mapping = container.getBean(DefaultAnnotationHandlerMapping.class);
|
||||
assertNotNull(mapping);
|
||||
assertEquals(0, mapping.getOrder());
|
||||
|
||||
AnnotationMethodHandlerAdapter adapter = container.getBean(AnnotationMethodHandlerAdapter.class);
|
||||
assertNotNull(adapter);
|
||||
assertNotNull(container.getBean(FormattingConversionServiceFactoryBean.class));
|
||||
|
|
@ -103,12 +107,8 @@ public class MvcNamespaceTests {
|
|||
assertEquals(4, container.getBeanDefinitionCount());
|
||||
container.refresh();
|
||||
|
||||
DefaultAnnotationHandlerMapping mapping = container.getBean(DefaultAnnotationHandlerMapping.class);
|
||||
assertNotNull(mapping);
|
||||
assertEquals(0, mapping.getOrder());
|
||||
AnnotationMethodHandlerAdapter adapter = container.getBean(AnnotationMethodHandlerAdapter.class);
|
||||
assertNotNull(adapter);
|
||||
assertNotNull(container.getBean(LocalValidatorFactoryBean.class));
|
||||
|
||||
TestController handler = new TestController();
|
||||
|
||||
|
|
@ -126,12 +126,8 @@ public class MvcNamespaceTests {
|
|||
assertEquals(4, container.getBeanDefinitionCount());
|
||||
container.refresh();
|
||||
|
||||
DefaultAnnotationHandlerMapping mapping = container.getBean(DefaultAnnotationHandlerMapping.class);
|
||||
assertNotNull(mapping);
|
||||
assertEquals(0, mapping.getOrder());
|
||||
AnnotationMethodHandlerAdapter adapter = container.getBean(AnnotationMethodHandlerAdapter.class);
|
||||
assertNotNull(adapter);
|
||||
assertNotNull(container.getBean(FormattingConversionServiceFactoryBean.class));
|
||||
|
||||
TestController handler = new TestController();
|
||||
|
||||
|
|
@ -149,12 +145,13 @@ public class MvcNamespaceTests {
|
|||
public void testInterceptors() throws Exception {
|
||||
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(container);
|
||||
reader.loadBeanDefinitions(new ClassPathResource("mvc-config-interceptors.xml", getClass()));
|
||||
assertEquals(4, container.getBeanDefinitionCount());
|
||||
assertEquals(6, container.getBeanDefinitionCount());
|
||||
container.refresh();
|
||||
|
||||
DefaultAnnotationHandlerMapping mapping = container.getBean(DefaultAnnotationHandlerMapping.class);
|
||||
assertNotNull(mapping);
|
||||
mapping.setRootHandler(new TestController());
|
||||
mapping.setDefaultHandler(new TestController());
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
request.addParameter("locale", "en");
|
||||
request.addParameter("theme", "green");
|
||||
|
|
@ -165,6 +162,65 @@ public class MvcNamespaceTests {
|
|||
assertTrue(chain.getInterceptors()[2] instanceof ThemeChangeInterceptor);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBeanDecoration() throws Exception {
|
||||
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(container);
|
||||
reader.loadBeanDefinitions(new ClassPathResource("mvc-config-bean-decoration.xml", getClass()));
|
||||
assertEquals(5, container.getBeanDefinitionCount());
|
||||
container.refresh();
|
||||
|
||||
DefaultAnnotationHandlerMapping mapping = container.getBean(DefaultAnnotationHandlerMapping.class);
|
||||
assertNotNull(mapping);
|
||||
mapping.setDefaultHandler(new TestController());
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
|
||||
HandlerExecutionChain chain = mapping.getHandler(request);
|
||||
assertEquals(2, chain.getInterceptors().length);
|
||||
assertTrue(chain.getInterceptors()[1] instanceof LocaleChangeInterceptor);
|
||||
LocaleChangeInterceptor interceptor = (LocaleChangeInterceptor) chain.getInterceptors()[1];
|
||||
assertEquals("lang", interceptor.getParamName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testViewControllers() throws Exception {
|
||||
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(container);
|
||||
reader.loadBeanDefinitions(new ClassPathResource("mvc-config-view-controllers.xml", getClass()));
|
||||
assertEquals(8, container.getBeanDefinitionCount());
|
||||
container.refresh();
|
||||
|
||||
DefaultAnnotationHandlerMapping mapping = container.getBean(DefaultAnnotationHandlerMapping.class);
|
||||
assertNotNull(mapping);
|
||||
mapping.setDefaultHandler(new TestController());
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
|
||||
HandlerExecutionChain chain = mapping.getHandler(request);
|
||||
assertEquals(3, chain.getInterceptors().length);
|
||||
assertTrue(chain.getInterceptors()[1] instanceof LocaleChangeInterceptor);
|
||||
|
||||
SimpleUrlHandlerMapping mapping2 = container.getBean(SimpleUrlHandlerMapping.class);
|
||||
assertNotNull(mapping2);
|
||||
|
||||
SimpleControllerHandlerAdapter adapter = container.getBean(SimpleControllerHandlerAdapter.class);
|
||||
assertNotNull(adapter);
|
||||
|
||||
request.setRequestURI("/foo");
|
||||
request.setMethod("GET");
|
||||
chain = mapping2.getHandler(request);
|
||||
assertEquals(3, chain.getInterceptors().length);
|
||||
assertTrue(chain.getInterceptors()[1] instanceof LocaleChangeInterceptor);
|
||||
ModelAndView mv = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
|
||||
assertNull(mv.getViewName());
|
||||
|
||||
request.setRequestURI("/bar");
|
||||
chain = mapping2.getHandler(request);
|
||||
assertEquals(3, chain.getInterceptors().length);
|
||||
assertTrue(chain.getInterceptors()[1] instanceof LocaleChangeInterceptor);
|
||||
ModelAndView mv2 = adapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
|
||||
assertEquals("baz", mv2.getViewName());
|
||||
}
|
||||
|
||||
@Controller
|
||||
public static class TestController {
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,16 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:mvc="http://www.springframework.org/schema/mvc"
|
||||
xmlns:p="http://www.springframework.org/schema/p"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
|
||||
|
||||
<mvc:annotation-driven />
|
||||
|
||||
<mvc:interceptors>
|
||||
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" p:paramName="lang" />
|
||||
</mvc:interceptors>
|
||||
|
||||
|
||||
</beans>
|
||||
|
|
@ -5,11 +5,11 @@
|
|||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
|
||||
|
||||
<mvc:annotation-driven>
|
||||
<mvc:interceptors>
|
||||
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
|
||||
<bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor" />
|
||||
</mvc:interceptors>
|
||||
</mvc:annotation-driven>
|
||||
<mvc:annotation-driven />
|
||||
|
||||
<mvc:interceptors>
|
||||
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
|
||||
<bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor" />
|
||||
</mvc:interceptors>
|
||||
|
||||
</beans>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:mvc="http://www.springframework.org/schema/mvc"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
|
||||
|
||||
<mvc:annotation-driven />
|
||||
|
||||
<mvc:view-controller path="/foo" />
|
||||
|
||||
<mvc:view-controller path="/bar" view-name="baz" />
|
||||
|
||||
<mvc:interceptors>
|
||||
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
|
||||
<bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor" />
|
||||
</mvc:interceptors>
|
||||
|
||||
</beans>
|
||||
|
|
@ -5,6 +5,6 @@
|
|||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
|
||||
|
||||
<mvc:annotation-driven/>
|
||||
|
||||
<mvc:annotation-driven />
|
||||
|
||||
</beans>
|
||||
|
|
|
|||
Loading…
Reference in New Issue