com.ctlok.springframework.web.servlet.view.rythm.RythmViewResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-webmvc-rythm Show documentation
Show all versions of spring-webmvc-rythm Show documentation
This is a plugin for Spring to adapt Rythm template engine
package com.ctlok.springframework.web.servlet.view.rythm;
import com.ctlok.springframework.web.servlet.view.rythm.tag.*;
import com.ctlok.springframework.web.servlet.view.rythm.variable.HttpServletRequestVariable;
import com.ctlok.springframework.web.servlet.view.rythm.variable.ImplicitVariable;
import org.rythmengine.Rythm;
import org.rythmengine.template.ITemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.web.servlet.view.AbstractTemplateViewResolver;
import javax.servlet.ServletContext;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* @author Lawrence Cheung
*
*/
public class RythmViewResolver extends AbstractTemplateViewResolver {
private static final Logger LOGGER = LoggerFactory.getLogger(RythmViewResolver.class);
private final RythmConfigurator configurator;
public RythmViewResolver(final RythmConfigurator configurator) {
super();
this.configurator = configurator;
this.setViewClass(this.requiredViewClass());
this.setContentType("text/html");
}
@Override
protected Class> requiredViewClass() {
return RythmView.class;
}
@Override
protected void initServletContext(ServletContext servletContext) {
super.initServletContext(servletContext);
this.setupSpringRythmConfig();
this.initRythm();
}
protected void initRythm(){
Rythm.init(configurator.generateConfig());
if (configurator.getTags() != null){
for (final ITemplate tag: configurator.getTags()){
LOGGER.debug("Register tag: [{}]", tag.__getName());
//deprecated from rythm-b7
//Rythm.registerTag(tag);
Rythm.engine().registerTemplate(tag);
}
}
if (configurator.isPreCompiledRoot() != null
&& configurator.getRootDirectory() != null
&& configurator.isPreCompiledRoot()){
final File root = new File(this.getServletContext().getRealPath(configurator.getRootDirectory()));
for (final File templateFile: this.findTemplateFile(root)){
LOGGER.debug("Pre compile template: [{}]", templateFile.getAbsolutePath());
Rythm.engine().getTemplate(templateFile);
}
}
LOGGER.info("Rythm version [{}] setup success.", Rythm.engine().version());
}
protected void setupSpringRythmConfig(){
this.configBuildInImplicitVariables();
this.configBuildInTag();
}
protected void configBuildInImplicitVariables(){
if (configurator.getImplicitVariables() == null){
configurator.setImplicitVariables(new ArrayList());
}
configurator.getImplicitVariables().add(new HttpServletRequestVariable());
}
protected void configBuildInTag(){
if (configurator.getTags() == null){
configurator.setTags(new ArrayList());
}
final AutowireCapableBeanFactory factory = this.getApplicationContext().getAutowireCapableBeanFactory();
for (final Class extends ITemplate> clazz: this.defaultTagClasses()){
final Object tag = factory.autowire(
clazz, AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR, false);
configurator.getTags().add((ITemplate) tag);
}
}
protected List> defaultTagClasses(){
List> classes = new ArrayList>();
classes.add(Url.class);
classes.add(FullUrl.class);
classes.add(Message.class);
classes.add(Secured.class);
classes.add(DateFormat.class);
return classes;
}
protected List findTemplateFile(final File root){
final List templateFiles = new ArrayList();
if (root.isDirectory()){
for (final File file: root.listFiles()){
if (file.isFile()){
templateFiles.add(file);
} else {
templateFiles.addAll(findTemplateFile(file));
}
}
}
return templateFiles;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy