SSM整合前端beetl模版引擎

 先引入beetl依赖。


    
      com.ibeetl
      beetl
      2.7.27
    

继承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中配置


    
        
    
    
        
    
    
        
        
        
    

定义一个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 getBean(Class clazz) {
        Collection 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  Collection getBeans(Class clazz) {
        try {
            Map map = getApplicationContext().getBeansOfType(clazz);
            return map.values();
        } catch (Exception var2) {
            return null;
        }
    }
}

 

你可能感兴趣的:(SSM整合前端beetl模版引擎)