-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sadeychai
committed
Aug 9, 2023
0 parents
commit debacef
Showing
25 changed files
with
3,178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
|
||
|
||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
rebel.xml | ||
/target/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
**本组件主要功能是让高于1.4.x版本的springboot支持velocity模板** | ||
|
||
使用方式 | ||
=== | ||
pom引入: | ||
```xml | ||
<dependency> | ||
<groupId>com.sadeychai.boot</groupId> | ||
<artifactId>spring-boot-velocity-starter</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</dependency> | ||
``` | ||
|
||
application.properties配置: | ||
|
||
```properties | ||
spring.velocity.enabled=true | ||
spring.velocity.cache= false | ||
spring.velocity.charset=UTF-8 | ||
spring.velocity.properties.input.encoding=UTF-8 | ||
spring.velocity.properties.output.encoding=UTF-8 | ||
spring.velocity.check-template-location=true | ||
spring.velocity.resource-loader-path=classpath:/templates/ | ||
spring.velocity.suffix=.vm | ||
spring.velocity.toolbox-config-location=/velocity/toolbox.xml | ||
#layout支持 | ||
spring.velocity.layout-view-resolver= true | ||
spring.velocity.layout-url= /src/page/layout/default.vm | ||
spring.velocity.screen-content-key= content | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.sadeychai.boot</groupId> | ||
<artifactId>spring-boot-velocity-starter</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
<name>spring-boot-velocity-starter</name> | ||
<description>spring-boot-velocity-starter</description> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.7.10</version> | ||
</parent> | ||
|
||
<properties> | ||
<velocity.version>1.7</velocity.version> | ||
<velocity-tools.version>2.0</velocity-tools.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.apache.velocity</groupId> | ||
<artifactId>velocity</artifactId> | ||
<version>${velocity.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.velocity</groupId> | ||
<artifactId>velocity-tools</artifactId> | ||
<version>${velocity-tools.version}</version> | ||
<exclusions> | ||
<exclusion> | ||
<artifactId>struts-core</artifactId> | ||
<groupId>org.apache.struts</groupId> | ||
</exclusion> | ||
<exclusion> | ||
<artifactId>servlet-api</artifactId> | ||
<groupId>javax.servlet</groupId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-webmvc</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.annotation</groupId> | ||
<artifactId>javax.annotation-api</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>org.junit.vintage</groupId> | ||
<artifactId>junit-vintage-engine</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-configuration-processor</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>javax.servlet-api</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<resources> | ||
<resource> | ||
<directory>src/main/resources</directory> | ||
<filtering>true</filtering> | ||
<includes> | ||
<include>**/*.vm</include> | ||
</includes> | ||
</resource> | ||
<resource> | ||
<directory>src/main/resources/META-INF/</directory> | ||
<!-- 打包META-INF文件 --> | ||
<targetPath>META-INF</targetPath> | ||
</resource> | ||
</resources> | ||
</build> | ||
</project> |
171 changes: 171 additions & 0 deletions
171
src/main/java/org/springframework/boot/autoconfigure/velocity/VelocityAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
/* | ||
* Copyright 2012-2016 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 | ||
* | ||
* https://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.boot.autoconfigure.velocity; | ||
|
||
import java.io.IOException; | ||
import java.util.Properties; | ||
import javax.annotation.PostConstruct; | ||
import javax.servlet.Servlet; | ||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.apache.velocity.app.VelocityEngine; | ||
import org.apache.velocity.exception.VelocityException; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnNotWebApplication; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; | ||
import org.springframework.boot.autoconfigure.template.TemplateLocation; | ||
import org.springframework.boot.autoconfigure.web.ConditionalOnEnabledResourceChain; | ||
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.boot.web.servlet.view.velocity.EmbeddedVelocityViewResolver; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.ui.velocity.VelocityEngineFactory; | ||
import org.springframework.ui.velocity.VelocityEngineFactoryBean; | ||
import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter; | ||
import org.springframework.web.servlet.view.velocity.VelocityConfig; | ||
import org.springframework.web.servlet.view.velocity.VelocityConfigurer; | ||
import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver; | ||
import org.springframework.web.servlet.view.velocity.VelocityViewResolver; | ||
|
||
/** | ||
* {@link EnableAutoConfiguration Auto-configuration} for Velocity. | ||
* | ||
* @author Andy Wilkinson | ||
* @author Brian Clozel | ||
* @since 1.1.0 | ||
* @deprecated as of 1.4 following the deprecation of Velocity support in Spring Framework | ||
* 4.3 | ||
*/ | ||
@Configuration | ||
@ConditionalOnClass({VelocityEngine.class, VelocityEngineFactory.class}) | ||
@AutoConfigureAfter(WebMvcAutoConfiguration.class) | ||
@EnableConfigurationProperties(VelocityProperties.class) | ||
@Deprecated | ||
public class VelocityAutoConfiguration { | ||
|
||
private static final Log logger = LogFactory.getLog(VelocityAutoConfiguration.class); | ||
|
||
private final ApplicationContext applicationContext; | ||
|
||
private final VelocityProperties properties; | ||
|
||
public VelocityAutoConfiguration(ApplicationContext applicationContext, | ||
VelocityProperties properties) { | ||
this.applicationContext = applicationContext; | ||
this.properties = properties; | ||
} | ||
|
||
@PostConstruct | ||
public void checkTemplateLocationExists() { | ||
if (this.properties.isCheckTemplateLocation()) { | ||
TemplateLocation location = new TemplateLocation( | ||
this.properties.getResourceLoaderPath()); | ||
if (!location.exists(this.applicationContext)) { | ||
logger.warn("Cannot find template location: " + location | ||
+ " (please add some templates, check your Velocity " | ||
+ "configuration, or set spring.velocity." | ||
+ "checkTemplateLocation=false)"); | ||
} | ||
} | ||
} | ||
|
||
@Deprecated | ||
protected static class VelocityConfiguration { | ||
|
||
@Autowired | ||
protected VelocityProperties properties; | ||
|
||
protected void applyProperties(VelocityEngineFactory factory) { | ||
factory.setResourceLoaderPath(this.properties.getResourceLoaderPath()); | ||
factory.setPreferFileSystemAccess(this.properties.isPreferFileSystemAccess()); | ||
Properties velocityProperties = new Properties(); | ||
velocityProperties.setProperty("input.encoding", | ||
this.properties.getCharsetName()); | ||
velocityProperties.putAll(this.properties.getProperties()); | ||
factory.setVelocityProperties(velocityProperties); | ||
} | ||
|
||
} | ||
|
||
@Configuration | ||
@ConditionalOnNotWebApplication | ||
@Deprecated | ||
public static class VelocityNonWebConfiguration extends VelocityConfiguration { | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public VelocityEngineFactoryBean velocityConfiguration() { | ||
VelocityEngineFactoryBean velocityEngineFactoryBean = new VelocityEngineFactoryBean(); | ||
applyProperties(velocityEngineFactoryBean); | ||
return velocityEngineFactoryBean; | ||
} | ||
|
||
} | ||
|
||
@Configuration | ||
@ConditionalOnClass(Servlet.class) | ||
@ConditionalOnWebApplication | ||
@Deprecated | ||
public static class VelocityWebConfiguration extends VelocityConfiguration { | ||
|
||
@Bean | ||
@ConditionalOnMissingBean(VelocityConfig.class) | ||
public VelocityConfigurer velocityConfigurer() { | ||
VelocityConfigurer configurer = new VelocityConfigurer(); | ||
applyProperties(configurer); | ||
return configurer; | ||
} | ||
|
||
@Bean | ||
public VelocityEngine velocityEngine(VelocityConfigurer configurer) | ||
throws VelocityException, IOException { | ||
return configurer.getVelocityEngine(); | ||
} | ||
|
||
/** | ||
* @Modifier sadeychai | ||
* on 2023/08/08 for support layout view resolver | ||
*/ | ||
@Bean | ||
@ConditionalOnMissingBean(name = "velocityViewResolver") | ||
@ConditionalOnProperty(name = "spring.velocity.enabled", matchIfMissing = true) | ||
public VelocityViewResolver velocityViewResolver() { | ||
VelocityViewResolver resolver = new EmbeddedVelocityViewResolver(); | ||
if (properties.isLayoutViewResolver()) { | ||
resolver = new VelocityLayoutViewResolver(); | ||
} | ||
this.properties.applyToMvcViewResolver(resolver); | ||
return resolver; | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
@ConditionalOnEnabledResourceChain | ||
public ResourceUrlEncodingFilter resourceUrlEncodingFilter() { | ||
return new ResourceUrlEncodingFilter(); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.