Browse Source

Convenience constructors for common view prefix/suffix scenarios

Issue: SPR-13813
pull/758/merge
Juergen Hoeller 9 years ago
parent
commit
41ee25b1e7
  1. 11
      spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ViewResolutionTests.java
  2. 13
      spring-webmvc/src/main/java/org/springframework/web/servlet/view/InternalResourceViewResolver.java
  3. 18
      spring-webmvc/src/main/java/org/springframework/web/servlet/view/freemarker/FreeMarkerViewResolver.java
  4. 18
      spring-webmvc/src/main/java/org/springframework/web/servlet/view/groovy/GroovyMarkupViewResolver.java
  5. 18
      spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateViewResolver.java
  6. 4
      spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ViewResolutionIntegrationTests.java
  7. 4
      spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ViewResolverRegistryTests.java
  8. 4
      spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerViewTests.java

11
spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/ViewResolutionTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
@ -53,10 +53,7 @@ public class ViewResolutionTests { @@ -53,10 +53,7 @@ public class ViewResolutionTests {
@Test
public void testJspOnly() throws Exception {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/");
viewResolver.setSuffix(".jsp");
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver("/WEB-INF/", ".jsp");
standaloneSetup(new PersonController()).setViewResolvers(viewResolver).build()
.perform(get("/person/Corea"))
@ -68,7 +65,6 @@ public class ViewResolutionTests { @@ -68,7 +65,6 @@ public class ViewResolutionTests {
@Test
public void testJsonOnly() throws Exception {
standaloneSetup(new PersonController()).setSingleView(new MappingJackson2JsonView()).build()
.perform(get("/person/Corea"))
.andExpect(status().isOk())
@ -78,7 +74,6 @@ public class ViewResolutionTests { @@ -78,7 +74,6 @@ public class ViewResolutionTests {
@Test
public void testXmlOnly() throws Exception {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(Person.class);
@ -91,7 +86,6 @@ public class ViewResolutionTests { @@ -91,7 +86,6 @@ public class ViewResolutionTests {
@Test
public void testContentNegotiation() throws Exception {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setClassesToBeBound(Person.class);
@ -131,7 +125,6 @@ public class ViewResolutionTests { @@ -131,7 +125,6 @@ public class ViewResolutionTests {
@Test
public void defaultViewResolver() throws Exception {
standaloneSetup(new PersonController()).build()
.perform(get("/person/Corea"))
.andExpect(model().attribute("person", hasProperty("name", equalTo("Corea"))))

13
spring-webmvc/src/main/java/org/springframework/web/servlet/view/InternalResourceViewResolver.java

@ -66,6 +66,19 @@ public class InternalResourceViewResolver extends UrlBasedViewResolver { @@ -66,6 +66,19 @@ public class InternalResourceViewResolver extends UrlBasedViewResolver {
setViewClass(viewClass);
}
/**
* A convenience constructor that allows for specifying {@link #setPrefix prefix}
* and {@link #setSuffix suffix} as constructor arguments.
* @param prefix the prefix that gets prepended to view names when building a URL
* @param suffix the suffix that gets appended to view names when building a URL
* @since 4.3
*/
public InternalResourceViewResolver(String prefix, String suffix) {
this();
setPrefix(prefix);
setSuffix(suffix);
}
/**
* This resolver requires {@link InternalResourceView}.

18
spring-webmvc/src/main/java/org/springframework/web/servlet/view/freemarker/FreeMarkerViewResolver.java

@ -40,10 +40,28 @@ import org.springframework.web.servlet.view.AbstractTemplateViewResolver; @@ -40,10 +40,28 @@ import org.springframework.web.servlet.view.AbstractTemplateViewResolver;
*/
public class FreeMarkerViewResolver extends AbstractTemplateViewResolver {
/**
* Sets the default {@link #setViewClass view class} to {@link #requiredViewClass}:
* by default {@link FreeMarkerView}.
*/
public FreeMarkerViewResolver() {
setViewClass(requiredViewClass());
}
/**
* A convenience constructor that allows for specifying {@link #setPrefix prefix}
* and {@link #setSuffix suffix} as constructor arguments.
* @param prefix the prefix that gets prepended to view names when building a URL
* @param suffix the suffix that gets appended to view names when building a URL
* @since 4.3
*/
public FreeMarkerViewResolver(String prefix, String suffix) {
this();
setPrefix(prefix);
setSuffix(suffix);
}
/**
* Requires {@link FreeMarkerView}.
*/

18
spring-webmvc/src/main/java/org/springframework/web/servlet/view/groovy/GroovyMarkupViewResolver.java

@ -38,10 +38,28 @@ import org.springframework.web.servlet.view.AbstractTemplateViewResolver; @@ -38,10 +38,28 @@ import org.springframework.web.servlet.view.AbstractTemplateViewResolver;
*/
public class GroovyMarkupViewResolver extends AbstractTemplateViewResolver {
/**
* Sets the default {@link #setViewClass view class} to {@link #requiredViewClass}:
* by default {@link GroovyMarkupView}.
*/
public GroovyMarkupViewResolver() {
setViewClass(requiredViewClass());
}
/**
* A convenience constructor that allows for specifying {@link #setPrefix prefix}
* and {@link #setSuffix suffix} as constructor arguments.
* @param prefix the prefix that gets prepended to view names when building a URL
* @param suffix the suffix that gets appended to view names when building a URL
* @since 4.3
*/
public GroovyMarkupViewResolver(String prefix, String suffix) {
this();
setPrefix(prefix);
setSuffix(suffix);
}
@Override
protected Class<?> requiredViewClass() {
return GroovyMarkupView.class;

18
spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateViewResolver.java

@ -35,10 +35,28 @@ import org.springframework.web.servlet.view.UrlBasedViewResolver; @@ -35,10 +35,28 @@ import org.springframework.web.servlet.view.UrlBasedViewResolver;
*/
public class ScriptTemplateViewResolver extends UrlBasedViewResolver {
/**
* Sets the default {@link #setViewClass view class} to {@link #requiredViewClass}:
* by default {@link ScriptTemplateView}.
*/
public ScriptTemplateViewResolver() {
setViewClass(requiredViewClass());
}
/**
* A convenience constructor that allows for specifying {@link #setPrefix prefix}
* and {@link #setSuffix suffix} as constructor arguments.
* @param prefix the prefix that gets prepended to view names when building a URL
* @param suffix the suffix that gets appended to view names when building a URL
* @since 4.3
*/
public ScriptTemplateViewResolver(String prefix, String suffix) {
this();
setPrefix(prefix);
setSuffix(suffix);
}
@Override
protected Class<?> requiredViewClass() {
return ScriptTemplateView.class;

4
spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ViewResolutionIntegrationTests.java

@ -257,9 +257,7 @@ public class ViewResolutionIntegrationTests { @@ -257,9 +257,7 @@ public class ViewResolutionIntegrationTests {
@Bean
public FreeMarkerViewResolver freeMarkerViewResolver() {
FreeMarkerViewResolver viewResolver = new FreeMarkerViewResolver();
viewResolver.setSuffix(".ftl");
return viewResolver;
return new FreeMarkerViewResolver("", ".ftl");
}
@Bean

4
spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ViewResolverRegistryTests.java

@ -99,9 +99,7 @@ public class ViewResolverRegistryTests { @@ -99,9 +99,7 @@ public class ViewResolverRegistryTests {
@Test
public void customViewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/");
viewResolver.setSuffix(".jsp");
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver("/", ".jsp");
this.registry.viewResolver(viewResolver);
assertSame(viewResolver, this.registry.getViewResolvers().get(0));
}

4
spring-webmvc/src/test/java/org/springframework/web/servlet/view/freemarker/FreeMarkerViewTests.java

@ -156,9 +156,7 @@ public class FreeMarkerViewTests { @@ -156,9 +156,7 @@ public class FreeMarkerViewTests {
wac.getBeanFactory().registerSingleton("configurer", configurer);
wac.refresh();
FreeMarkerViewResolver vr = new FreeMarkerViewResolver();
vr.setPrefix("prefix_");
vr.setSuffix("_suffix");
FreeMarkerViewResolver vr = new FreeMarkerViewResolver("prefix_", "_suffix");
vr.setApplicationContext(wac);
View view = vr.resolveViewName("test", Locale.CANADA);

Loading…
Cancel
Save