22
33import org .springframework .beans .factory .annotation .Autowired ;
44import org .springframework .boot .SpringApplication ;
5+ import org .springframework .boot .autoconfigure .AutoConfigureAfter ;
56import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
7+ import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
8+ import org .springframework .boot .autoconfigure .web .WebMvcAutoConfiguration ;
9+ import org .springframework .boot .bind .RelaxedPropertyResolver ;
610import org .springframework .boot .builder .SpringApplicationBuilder ;
711import org .springframework .boot .context .web .SpringBootServletInitializer ;
12+ import org .springframework .context .EnvironmentAware ;
13+ import org .springframework .context .annotation .Bean ;
814import org .springframework .context .annotation .ComponentScan ;
15+ import org .springframework .context .annotation .Configuration ;
16+ import org .springframework .core .Ordered ;
17+ import org .springframework .core .env .Environment ;
918import org .springframework .data .jpa .repository .JpaRepository ;
1019import org .springframework .stereotype .Controller ;
1120import org .springframework .ui .Model ;
1221import org .springframework .web .bind .annotation .RequestBody ;
1322import org .springframework .web .bind .annotation .RequestMapping ;
1423import org .springframework .web .bind .annotation .RequestMethod ;
1524import org .springframework .web .bind .annotation .RestController ;
25+ import org .springframework .web .servlet .view .velocity .VelocityConfigurer ;
26+ import org .springframework .web .servlet .view .velocity .VelocityViewResolver ;
1627
1728import javax .persistence .Entity ;
1829import javax .persistence .GeneratedValue ;
1930import javax .persistence .Id ;
31+ import javax .servlet .Servlet ;
2032import java .util .*;
2133
2234
@@ -37,6 +49,41 @@ protected SpringApplicationBuilder configure(SpringApplicationBuilder applicatio
3749
3850}
3951
52+ @ Configuration
53+ @ ConditionalOnClass ({Servlet .class })
54+ @ AutoConfigureAfter (WebMvcAutoConfiguration .class )
55+ class VelocityAutoConfiguration implements EnvironmentAware {
56+
57+ public static final String DEFAULT_PREFIX = "/templates/" ;
58+
59+ public static final String DEFAULT_SUFFIX = ".vm" ;
60+
61+ private RelaxedPropertyResolver environment ;
62+
63+ @ Override
64+ public void setEnvironment (Environment environment ) {
65+ this .environment = new RelaxedPropertyResolver (environment ,
66+ "spring.velocity." );
67+ }
68+
69+ @ Bean
70+ VelocityConfigurer velocityConfig () {
71+ return new VelocityConfigurer ();
72+ }
73+
74+ @ Bean
75+ VelocityViewResolver velocityViewResolver () {
76+ VelocityViewResolver resolver = new VelocityViewResolver ();
77+ resolver .setSuffix (this .environment .getProperty ("suffix" , DEFAULT_SUFFIX ));
78+ resolver .setPrefix (this .environment .getProperty ("prefix" , DEFAULT_PREFIX ));
79+ // Needs to come before any fallback resolver (e.g. a
80+ // InternalResourceViewResolver)
81+ resolver .setOrder (Ordered .LOWEST_PRECEDENCE - 20 );
82+ return resolver ;
83+
84+ }
85+ }
86+
4087/**
4188 * Represents one (very under-specified) <em>booking</em>
4289 * for a named person at a theoretical establishment like a restaurant.
0 commit comments