Commit 3404850b authored by Dave Syer's avatar Dave Syer

Merge branch '1.1.x'

parents fcebf9d3 95d65c2f
...@@ -21,7 +21,6 @@ import java.util.Map; ...@@ -21,7 +21,6 @@ import java.util.Map;
import org.springframework.boot.autoconfigure.template.AbstractTemplateViewResolverProperties; import org.springframework.boot.autoconfigure.template.AbstractTemplateViewResolverProperties;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
/** /**
* {@link ConfigurationProperties} for configuring FreeMarker * {@link ConfigurationProperties} for configuring FreeMarker
...@@ -63,11 +62,4 @@ public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties ...@@ -63,11 +62,4 @@ public class FreeMarkerProperties extends AbstractTemplateViewResolverProperties
this.templateLoaderPath = templateLoaderPath; this.templateLoaderPath = templateLoaderPath;
} }
/**
* Apply the given properties to a {@link FreeMarkerViewResolver}.
* @param resolver the resolver to apply the properties to.
*/
public void applyToViewResolver(FreeMarkerViewResolver resolver) {
super.applyToViewResolver(resolver);
}
} }
...@@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.template; ...@@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.template;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.util.Assert;
import org.springframework.web.servlet.view.AbstractTemplateViewResolver; import org.springframework.web.servlet.view.AbstractTemplateViewResolver;
/** /**
...@@ -158,10 +159,18 @@ public abstract class AbstractTemplateViewResolverProperties { ...@@ -158,10 +159,18 @@ public abstract class AbstractTemplateViewResolverProperties {
} }
/** /**
* Apply the given properties to a {@link AbstractTemplateViewResolver}. * Apply the given properties to a {@link AbstractTemplateViewResolver}. Use Object in
* @param resolver the resolver to apply the properties to. * signature to avoid runtime dependency on MVC, which means that the template engine
* can be used in a non-web application.
*
* @param viewResolver the resolver to apply the properties to.
*/ */
protected void applyToViewResolver(AbstractTemplateViewResolver resolver) { public void applyToViewResolver(Object viewResolver) {
Assert.isInstanceOf(AbstractTemplateViewResolver.class, viewResolver,
"ViewResolver is not an instance of AbstractTemplateViewResolver :"
+ viewResolver);
AbstractTemplateViewResolver resolver = (AbstractTemplateViewResolver) viewResolver;
resolver.setPrefix(getPrefix()); resolver.setPrefix(getPrefix());
resolver.setSuffix(getSuffix()); resolver.setSuffix(getSuffix());
resolver.setCache(isCache()); resolver.setCache(isCache());
...@@ -175,6 +184,7 @@ public abstract class AbstractTemplateViewResolverProperties { ...@@ -175,6 +184,7 @@ public abstract class AbstractTemplateViewResolverProperties {
// The resolver usually acts as a fallback resolver (e.g. like a // The resolver usually acts as a fallback resolver (e.g. like a
// InternalResourceViewResolver) so it needs to have low precedence // InternalResourceViewResolver) so it needs to have low precedence
resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 5); resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 5);
} }
} }
...@@ -118,7 +118,7 @@ public class VelocityAutoConfiguration { ...@@ -118,7 +118,7 @@ public class VelocityAutoConfiguration {
@Bean @Bean
public VelocityEngine velocityEngine(VelocityConfigurer configurer) public VelocityEngine velocityEngine(VelocityConfigurer configurer)
throws VelocityException, IOException { throws VelocityException, IOException {
return configurer.createVelocityEngine(); return configurer.getVelocityEngine();
} }
@Bean @Bean
......
...@@ -92,12 +92,10 @@ public class VelocityProperties extends AbstractTemplateViewResolverProperties { ...@@ -92,12 +92,10 @@ public class VelocityProperties extends AbstractTemplateViewResolverProperties {
this.toolboxConfigLocation = toolboxConfigLocation; this.toolboxConfigLocation = toolboxConfigLocation;
} }
/** @Override
* Apply the given properties to a {@link VelocityViewResolver}. public void applyToViewResolver(Object viewResolver) {
* @param resolver the resolver to apply the properties to. super.applyToViewResolver(viewResolver);
*/ VelocityViewResolver resolver = (VelocityViewResolver) viewResolver;
public void applyToViewResolver(VelocityViewResolver resolver) {
super.applyToViewResolver(resolver);
resolver.setToolboxConfigLocation(getToolboxConfigLocation()); resolver.setToolboxConfigLocation(getToolboxConfigLocation());
resolver.setDateToolAttribute(getDateToolAttribute()); resolver.setDateToolAttribute(getDateToolAttribute());
resolver.setNumberToolAttribute(getNumberToolAttribute()); resolver.setNumberToolAttribute(getNumberToolAttribute());
......
...@@ -49,6 +49,7 @@ ...@@ -49,6 +49,7 @@
<module>spring-boot-sample-tomcat-multi-connectors</module> <module>spring-boot-sample-tomcat-multi-connectors</module>
<module>spring-boot-sample-tomcat8-jsp</module> <module>spring-boot-sample-tomcat8-jsp</module>
<module>spring-boot-sample-traditional</module> <module>spring-boot-sample-traditional</module>
<module>spring-boot-sample-velocity</module>
<module>spring-boot-sample-web-freemarker</module> <module>spring-boot-sample-web-freemarker</module>
<module>spring-boot-sample-web-groovy-templates</module> <module>spring-boot-sample-web-groovy-templates</module>
<module>spring-boot-sample-web-method-security</module> <module>spring-boot-sample-web-method-security</module>
......
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<!-- Your own application should inherit from spring-boot-starter-parent -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-samples</artifactId>
<version>1.1.6.BUILD-SNAPSHOT</version>
</parent>
<artifactId>spring-boot-sample-velocity</artifactId>
<name>spring-boot-sample-velocity</name>
<description>Spring Boot Web Velocity Sample</description>
<url>http://projects.spring.io/spring-boot/</url>
<organization>
<name>Pivotal Software, Inc.</name>
<url>http://www.spring.io</url>
</organization>
<properties>
<main.basedir>${basedir}/../..</main.basedir>
<m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<useSystemClassLoader>false</useSystemClassLoader>
</configuration>
</plugin>
</plugins>
</build>
</project>
/*
* Copyright 2012-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 sample.velocity;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.ui.velocity.VelocityEngineUtils;
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class SampleVelocityApplication implements CommandLineRunner {
@Value("${application.message}")
private String message;
@Autowired
private VelocityEngine engine;
@Override
public void run(String... args) throws Exception {
Map<String, Object> model = new HashMap<String, Object>();
model.put("time", new Date());
model.put("message", this.message);
System.out.println(VelocityEngineUtils.mergeTemplateIntoString(this.engine,
"welcome.vm", "UTF-8", model));
}
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleVelocityApplication.class, args);
}
}
From application: $time
Message: $message
\ No newline at end of file
/*
* Copyright 2012-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 sample.velocity;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.OutputCapture;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertTrue;
/**
* Basic integration tests for Velocity application with no web layer.
*
* @author Dave Syer
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SampleVelocityApplication.class)
public class SampleVelocityApplicationTests {
@ClassRule
public static OutputCapture output = new OutputCapture();
@Test
public void testVelocityTemplate() throws Exception {
String result = SampleVelocityApplicationTests.output.toString();
assertTrue("Wrong output: " + result, result.contains("Hello, Andy"));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment