SSM整合前端beetl模版引擎

 先引入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;
        }
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值