Last active
November 7, 2016 21:00
-
-
Save mpbalmeida/1ad72f512a794be314c1e2857d344d42 to your computer and use it in GitHub Desktop.
Gist using Vraptor 4 and Thymeleaf, based on https://github.com/claudineij/vraptor-poc/tree/master/src/main/java/br/com/caelum/vraptor/poc/thymeleaf
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
Gist using Vraptor 4 and Thymeleaf, based on https://github.com/claudineij/vraptor-poc/tree/master/src/main/java/br/com/caelum/vraptor/poc/thymeleaf | |
<dependency> | |
<groupId>br.com.caelum</groupId> | |
<artifactId>vraptor</artifactId> | |
<version>4.2.0-RC3</version> | |
</dependency> | |
<dependency> | |
<groupId>org.thymeleaf</groupId> | |
<artifactId>thymeleaf</artifactId> | |
<version>3.0.2.RELEASE</version> | |
</dependency> |
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
package br.com.faroltech.vraptor-thymeleaf; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.net.URL; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Locale; | |
import java.util.Map; | |
import java.util.Properties; | |
import org.thymeleaf.exceptions.TemplateProcessingException; | |
import org.thymeleaf.messageresolver.StandardMessageResolver; | |
import org.thymeleaf.templateresource.ITemplateResource; | |
import org.thymeleaf.util.StringUtils; | |
import br.com.faroltech.minhaigreja.core.base.utils.F; | |
public class CustomMessageResolver extends StandardMessageResolver { | |
private static final String PROPERTIES_FILE_EXTENSION = ".properties"; | |
@Override | |
protected Map<String, String> resolveMessagesForTemplate(String template, ITemplateResource templateResource, | |
Locale locale) { | |
List<String> computeMessageResourceNamesFromBase = computeMessageResourceNamesFromBase("messages", locale); | |
File file = createFileProperties(computeMessageResourceNamesFromBase); | |
if (file != null) { | |
FileInputStream fis = null; | |
try { | |
Properties properties = new Properties(); | |
fis = new FileInputStream(file); | |
properties.load(fis); | |
setDefaultMessages(properties); | |
} catch (Throwable t) { | |
} finally { | |
F.close(fis); | |
} | |
} | |
return super.resolveMessagesForTemplate(template, templateResource, locale); | |
} | |
private File createFileProperties(List<String> computeMessageResourceNamesFromBase) { | |
ClassLoader classLoader = getClass().getClassLoader(); | |
File file = null; | |
for (String string : computeMessageResourceNamesFromBase) { | |
URL resource = classLoader.getResource(string); | |
if (resource != null) { | |
file = new File(classLoader.getResource(string).getFile()); | |
if (file.exists()) { | |
return file; | |
} | |
} | |
} | |
return null; | |
} | |
private static List<String> computeMessageResourceNamesFromBase( | |
final String resourceBaseName, final Locale locale) { | |
final List<String> resourceNames = new ArrayList<String>(5); | |
if (StringUtils.isEmptyOrWhitespace(locale.getLanguage())) { | |
throw new TemplateProcessingException( | |
"Locale \"" + locale.toString() + "\" " + | |
"cannot be used as it does not specify a language."); | |
} | |
if (!StringUtils.isEmptyOrWhitespace(locale.getCountry())) { | |
resourceNames.add( | |
resourceBaseName + "_" + locale.getLanguage() + "_" + locale.getCountry() + PROPERTIES_FILE_EXTENSION); | |
} | |
if (!StringUtils.isEmptyOrWhitespace(locale.getVariant())) { | |
resourceNames.add( | |
resourceBaseName + "_" + locale.getLanguage() + "_" + locale.getCountry() + "-" + locale.getVariant() + PROPERTIES_FILE_EXTENSION); | |
} | |
resourceNames.add(resourceBaseName + "_" + locale.getLanguage() + PROPERTIES_FILE_EXTENSION); | |
resourceNames.add(resourceBaseName + PROPERTIES_FILE_EXTENSION); | |
return resourceNames; | |
} | |
} |
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
package br.com.faroltech.vraptor-thymeleaf; | |
import javax.annotation.PostConstruct; | |
import javax.enterprise.context.ApplicationScoped; | |
import javax.inject.Inject; | |
import javax.servlet.ServletContext; | |
import org.thymeleaf.TemplateEngine; | |
import org.thymeleaf.messageresolver.StandardMessageResolver; | |
import org.thymeleaf.templateresolver.ServletContextTemplateResolver; | |
import br.com.caelum.vraptor.core.MethodInfo; | |
@ApplicationScoped | |
public class ThymeleafComponent { | |
@Inject | |
private ThymeleafPathResolver resolver; | |
private ServletContextTemplateResolver templateResolver; | |
private TemplateEngine templateEngine; | |
@Inject private ServletContext servletContext; | |
@PostConstruct | |
private void init() { | |
templateResolver = new ServletContextTemplateResolver(servletContext); | |
templateResolver.setTemplateMode(resolver.getTemplateMode()); | |
templateResolver.setPrefix(resolver.getPrefix()); | |
templateResolver.setSuffix("." + resolver.getExtension()); | |
templateResolver.setCharacterEncoding(resolver.getCharacterEncoding()); | |
templateResolver.setCacheTTLMs(resolver.getCache()); | |
templateResolver.setCacheable(false); | |
templateEngine = new TemplateEngine(); | |
StandardMessageResolver standardMessageResolver = new CustomMessageResolver(); | |
templateEngine.setMessageResolver(standardMessageResolver); | |
templateEngine.setTemplateResolver(templateResolver); | |
} | |
public TemplateEngine getTemplateEngine() { | |
return templateEngine; | |
} | |
public String getTemplateName(final MethodInfo methodInfo) { | |
final String to = resolver.pathFor(methodInfo.getControllerMethod()); | |
return to.replace(templateResolver.getPrefix(), "").replace(templateResolver.getSuffix(), ""); | |
} | |
} |
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
package br.com.faroltech.vraptor-thymeleaf;.result; | |
import java.io.IOException; | |
import javax.enterprise.inject.Specializes; | |
import javax.inject.Inject; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.thymeleaf.context.WebContext; | |
import br.com.caelum.vraptor.core.MethodInfo; | |
import br.com.caelum.vraptor.http.MutableRequest; | |
import br.com.caelum.vraptor.http.MutableResponse; | |
import br.com.caelum.vraptor.view.DefaultPageResult; | |
import br.com.caelum.vraptor.view.ResultException; | |
import br.com.faroltech.vraptor-thymeleaf.ThymeleafComponent; | |
@Specializes | |
public class ThymeleafPageResult extends DefaultPageResult { | |
private static final Logger logger = LoggerFactory.getLogger(ThymeleafPageResult.class); | |
@Inject private MutableRequest request; | |
@Inject private MutableResponse response; | |
@Inject private MethodInfo methodInfo; | |
@Inject private ThymeleafComponent thymeleafConfig; | |
/** | |
* @deprecated CDI eyes only | |
*/ | |
protected ThymeleafPageResult() { | |
super(null, null, null, null, null); | |
} | |
@Override | |
public void defaultView() { | |
try { | |
final WebContext ctx = new WebContext(request, response, request.getServletContext(), request.getLocale()); | |
final String templateName = thymeleafConfig.getTemplateName(methodInfo); | |
logger.debug("forwarding template name {}", templateName); | |
thymeleafConfig.getTemplateEngine().process(templateName, ctx, response.getWriter()); | |
} catch (IOException e) { | |
throw new ResultException(e); | |
} | |
} | |
} |
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
package br.com.faroltech.vraptor-thymeleaf; | |
import javax.enterprise.inject.Specializes; | |
import javax.inject.Inject; | |
import org.thymeleaf.templatemode.TemplateMode; | |
import br.com.caelum.vraptor.http.FormatResolver; | |
import br.com.caelum.vraptor.view.DefaultPathResolver; | |
@Specializes | |
public class ThymeleafPathResolver extends DefaultPathResolver { | |
/** | |
* @deprecated CDI eyes only | |
*/ | |
protected ThymeleafPathResolver() { | |
this(null); | |
} | |
@Inject | |
public ThymeleafPathResolver(FormatResolver resolver) { | |
super(resolver); | |
} | |
@Override | |
protected String getPrefix() { | |
return "/WEB-INF/templates/"; | |
} | |
@Override | |
protected String getExtension() { | |
return "html"; | |
} | |
protected TemplateMode getTemplateMode() { | |
return TemplateMode.HTML; | |
} | |
protected Long getCache() { | |
return 3600000L; | |
} | |
protected String getCharacterEncoding() { | |
return "UTF-8"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment