先引入beetl依赖
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl</artifactId>
<version>2.7.27</version>
</dependency>
继承ibeetl的 BeetlSpringViewResolver
public class MyBeetlSpringViewResolver extends org.beetl.ext.spring.BeetlSpringViewResolver {
public BeetlSpringViewResolver() {
}
public String getPrefix() {
return super.getPrefix();
}
}
继承ibeetl的 WebAppResourceLoader
public class AppResourceLoader extends WebAppResourceLoader {
private final ClasspathResourceLoader classpathResourceLoader = new ClasspathResourceLoader();
private String classpathPrefix;
private Class clazz = this.getClass();
public AppResourceLoader() {
super((String)null);
}
private static String getPrefix() {
MyBeetlSpringViewResolverresolver = BeanFactory.getBean(MyBeetlSpringViewResolver.class);
return resolver.getPrefix();
}
public String getClasspathPrefix() {
return this.classpathPrefix;
}
public void setClasspathPrefix(String classpathPrefix) {
this.classpathPrefix = classpathPrefix;
}
public ClasspathResourceLoader getClasspathResourceLoader() {
return this.classpathResourceLoader;
}
private String getClasspathResourceKey(String key) {
key = key.replace("classpath:*", "");
if (this.classpathPrefix.endsWith("/") && key.startsWith("/")) {
return this.classpathPrefix + key.substring(1);
} else {
return !this.classpathPrefix.endsWith("/") && !key.startsWith("/") ? this.classpathPrefix + "/" + key : this.classpathPrefix + key;
}
}
public Resource getResource(String key) {
String key0 = key.replace(getPrefix(), "");
String classpathResourcePath = this.getClasspathResourceKey(key0);
if (key0.startsWith("classpath:*")) {
return this.classpathResourceLoader.getResource(classpathResourcePath);
} else {
URL url = this.clazz.getResource(classpathResourcePath);
return url != null ? this.classpathResourceLoader.getResource(classpathResourcePath) : super.getResource(key);
}
}
}
在spring-mvc中配置
<!-- 配置模板引擎 -->
<bean name="zoeAppResourceLoader" class="com.zey.sm.frame.util.AppResourceLoader">
<property name="classpathPrefix" value="/WEB-TPL"/>
</bean>
<bean name="beetlConfig" class="org.beetl.ext.spring.BeetlGroupUtilConfiguration" init-method="init">
<property name="resourceLoader" ref="zoeAppResourceLoader"/>
</bean>
<bean name="beetlViewResolver" class="com.zey.sm.frame.util.MyBeetlSpringViewResolver">
<property name="prefix" value="/views/"/>
<property name="suffix" value=".html"/>
<property name="contentType" value="text/html;charset=UTF-8"/>
</bean>
<bean id="beanFactory" class="com.zey.sm.frame.util.BeanFactory"></bean>
定义一个beanFactory
public final class BeanFactory implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public BeanFactory() {
}
//获取静态变量中的ApplicationContext.
private static ApplicationContext getApplicationContext() {
return applicationContext;
}
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
applicationContext = context;
}
public static <T> T getBean(Class<T> clazz) {
Collection<T> beans = getBeans(clazz);
if (beans != null && !beans.isEmpty()) {
T bean = null;
Object t;
for(Iterator var3 = beans.iterator(); var3.hasNext(); bean = (T) t) {
t = var3.next();
}
return bean;
} else {
return null;
}
}
public static <T> Collection<T> getBeans(Class<T> clazz) {
try {
Map<String, T> map = getApplicationContext().getBeansOfType(clazz);
return map.values();
} catch (Exception var2) {
return null;
}
}
}