I have a spring-boot + thymeleaf application - so im basicly using no xml files for configuration. Classic html templates work fine for me, but i am having troubles with xsl. I followed this tutorial Spring MVC XstlView and XsltViewResolver Example but ended up with java.io.FileNotFoundException: Could not open ServletContext resource [/templates/show.xsl]
This is how my configuration class looks like
#Configuration
#EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter {
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/" };
#Bean
#Description("Thymeleaf template resolver serving HTML 5")
public ClassLoaderTemplateResolver htmlTemplateResolver() {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("templates/");
templateResolver.setCacheable(false);
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5");
templateResolver.setCharacterEncoding("UTF-8");
return templateResolver;
}
#Bean
#Description("Thymeleaf template resolver serving XML")
public ClassLoaderTemplateResolver xmlTemplateResolver() {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setPrefix("templates/");
templateResolver.setCacheable(false);
templateResolver.setSuffix(".xml");
templateResolver.setTemplateMode("XML");
templateResolver.setCharacterEncoding("UTF-8");
return templateResolver;
}
#Bean
#Description("Thymeleaf template engine with Spring integration")
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addTemplateResolver(htmlTemplateResolver());
templateEngine.addTemplateResolver(xmlTemplateResolver());
return templateEngine;
}
#Bean
#Description("Thymeleaf view resolver")
public ViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setCharacterEncoding("UTF-8");
return viewResolver;
}
#Bean
public ViewResolver getXSLTViewResolver(){
XsltViewResolver xsltViewResolver = new XsltViewResolver();
xsltViewResolver.setOrder(1);
xsltViewResolver.setSourceKey("xmlSource");
xsltViewResolver.setViewClass(XsltView.class);
xsltViewResolver.setViewNames(new String[] {"show"});
xsltViewResolver.setPrefix("templates/");
xsltViewResolver.setSuffix(".xsl");
return xsltViewResolver;
}
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!registry.hasMappingForPattern("/webjars/**")) {
registry.addResourceHandler("/webjars/**").addResourceLocations(
"classpath:/META-INF/resources/webjars/");
}
if (!registry.hasMappingForPattern("/**")) {
registry.addResourceHandler("/**").addResourceLocations(
CLASSPATH_RESOURCE_LOCATIONS);
}
}
}
From controller i am trying to redirect to my xsl template like this:
#RequestMapping(value="/form", method=RequestMethod.POST, params="action=show")
public ModelAndView show(#ModelAttribute Team team) throws MarshalException {
Source source = new StreamSource(new ByteArrayInputStream(xmlService.getXmlStream(team).toByteArray()));
// adds the XML source file to the model so the XsltView can detect
ModelAndView model = new ModelAndView("show");
model.addObject("xmlSource", source);
return model;
}
This is how my project files look like.
I would appreciate any suggestions how to make it work. Thank you
If you are using spring boot, then, no need to mention any configuration setting like, TemplateMode, Prefix, Suffix, etc. Spring boot automatically configures the template as per default setting.
Do no mention any configuration in #Configuration class or application.properties file.
Try this, it worked for me.
Related
I have a spring boot application which is working as an OAuth2 client.
I'm using Thymeleaf 3 as template engine. These are the Thymeleaf 3 related dependencies in my build.gradle.
compile('org.thymeleaf:thymeleaf:3.0.1.RELEASE')
compile('org.thymeleaf:thymeleaf-spring4:3.0.1.RELEASE')
compile('nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:2.0.4')
compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity4:3.0.1.RELEASE')
It makes no difference if I reference the dependencies like this:
ext["thymeleaf.version"] = "3.0.2.RELEASE"
ext["thymeleaf-layout-dialect.version"] = "2.1.1"
dependencies {
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
}
I want to be able to use Thymeleaf 3's HTML and Javascript template modes.
HTML mode works, but in javascript mode the messageSource is not working properly.
Here is my WebMvcConfiguration class:
#Configuration
public class WebMVCConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {
private static final String CHARACTER_ENCODING = "UTF-8";
private ApplicationContext applicationContext;
#Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
#Bean
public ViewResolver htmlViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine(htmlTemplateResolver()));
resolver.setContentType("text/html");
resolver.setCharacterEncoding(CHARACTER_ENCODING);
resolver.setViewNames(new String[] {"*.html"});
return resolver;
}
#Bean
public ViewResolver javascriptViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine(javascriptTemplateResolver()));
resolver.setContentType("application/javascript");
resolver.setCharacterEncoding(CHARACTER_ENCODING);
resolver.setViewNames(new String[] {"*.js"});
return resolver;
}
private TemplateEngine templateEngine(ITemplateResolver templateResolver) {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(templateResolver);
return engine;
}
private ITemplateResolver htmlTemplateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(applicationContext);
resolver.setPrefix("templates/");
resolver.setCacheable(false);
resolver.setTemplateMode(TemplateMode.HTML);
resolver.setSuffix(".html");
return resolver;
}
public ITemplateResolver javascriptTemplateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(applicationContext);
resolver.setPrefix("classpath:/static/js/");
resolver.setCacheable(false);
resolver.setTemplateMode(TemplateMode.JAVASCRIPT);
// resolver.setSuffix(".js");
return resolver;
}
}
Please note that I had to use "classpath:/static/js/" in javascriptTemplateResolver, because when I used only "static/js/", I got the following exception:
java.io.FileNotFoundException: Could not open ServletContext resource [/static/js/headerconfig.js]
Also I had to comment out setSuffix, because with it I get the following exception:
java.io.FileNotFoundException: class path resource [static/js/typeutils.js.js] cannot be opened because it does not exist
I think this already indicates the main problem but I can't figure out what may cause it.
I have a controller for handling javascript templates:
#Controller
public class JavascriptController {
#RequestMapping(method = RequestMethod.GET, value = "/js/{template}.js")
public String jsMapping(#PathVariable("template") String template, Model model) {
model.addAttribute("myAttribute", "Attribute works!");
return template;
}
}
My messages.properties files are located in
src/main/resources/messages.properties
src/main/resources/messages_hu.properties
I have an HTML file in src/main/resources/templates/ folder, which references a javascript file like so:
<script th:src="#{js/typeutils.js}"></script>
The referenced javascript file (js/typeutils.js):
var a = [[${myAttribute}]];
var b = [[#{test}]];
console.log(a);
console.log(b);
When I run the application and check the javascript console, this is what's being printed out:
Attribute works!
??test_hu_HU??
So the model attribute was successfully passed to the javascript file, and the localization has been detected, but the message wasn't found for 'test'.
It seems as if the javascript template mode behaves completely different from HTML template mode.
How should if fix the javascript template mode config in order for it to process the messages.properties files, too?
Thanks!
I figured out the solution (by looking through thymeleaf sourcecode), which might not be perfect, but it works!
Unlike the example given by Baeldung, the following solved the problem for me:
#Configuration
public class WebMVCConfig implements WebMvcConfigurer, ApplicationContextAware {
private static final String CHARACTER_ENCODING = "UTF-8";
private ApplicationContext applicationContext;
#Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
#Bean
public ViewResolver htmlViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
resolver.setContentType("text/html");
resolver.setCharacterEncoding(CHARACTER_ENCODING);
resolver.setViewNames(new String[] { "*.html" });
return resolver;
}
#Bean
public ViewResolver javascriptViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
resolver.setContentType("application/javascript");
resolver.setCharacterEncoding(CHARACTER_ENCODING);
resolver.setViewNames(new String[] { "*.js" });
return resolver;
}
#Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setMessageSource(messageSource());
engine.addTemplateResolver(htmlTemplateResolver());
engine.addTemplateResolver(javascriptTemplateResolver());
return engine;
}
private ITemplateResolver htmlTemplateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setOrder(0);
resolver.setCheckExistence(true);
resolver.setApplicationContext(applicationContext);
resolver.setPrefix("classpath:templates/");
resolver.setCacheable(false);
resolver.setTemplateMode(TemplateMode.HTML);
resolver.setSuffix(".html");
return resolver;
}
public ITemplateResolver javascriptTemplateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(applicationContext);
resolver.setOrder(1);
resolver.setCheckExistence(true);
resolver.setPrefix("classpath:/static/js/");
resolver.setCacheable(false);
resolver.setTemplateMode(TemplateMode.JAVASCRIPT);
return resolver;
}
#Bean
public MessageSource messageSource() {
ResourceBundleMessageSource msgSource = new ResourceBundleMessageSource();
msgSource.setAlwaysUseMessageFormat(false);
msgSource.setBasename("messages");
msgSource.setDefaultEncoding(CHARACTER_ENCODING);
msgSource.setFallbackToSystemLocale(true);
msgSource.setUseCodeAsDefaultMessage(false);
return msgSource;
}
}
Basically what happens is we set templateEngine to be a bean, so that we override ThymeleafDefaultConfiguration's default templateEngine implementation bean. This means that each time a template needs to be resolved, the same templateEngine will be used for sure.
We set the order of the htmlTemplateResolver to be 0, and the order of the javascriptTemplateResolver to be 1, so that we will try to resolve each template first in HTML template mode, then in Javascript mode.
It's important, too, that we set the checkExistence flag of the SpringResourceTemplateResolvers to true, because this way if a template can't be found, we will try with the next TemplateResolver.
There is one drawback of this solution, which is we will try to resolve javascript templates unnecessary in HTML mode first, then in JAVASCRIPT mode, so there is an extra step when resolving javascript resources.
I will try to solve the problem better but for now, this works for me.
I am using two template resolvers for my Spring Boot Application ServletContextTemplateResolver and ClassLoaderTemplateResolver.
I am using ServletContextTemplateResolver for /WEB-INF/* html templates for my web pages. I will on the other hand, use ClassLoaderTemplateResolver for my /mail/ html templates. Here's the code:
#Bean(name ="templateResolver")
public ServletContextTemplateResolver getTemplateResolver() {
ServletContextTemplateResolver templateResolver = new
ServletContextTemplateResolver();
templateResolver.setPrefix("/WEB-INF/templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("XHTML");
templateResolver.setOrder(Integer.valueOf(1));
return templateResolver;
}
#Bean(name ="emailTemplateResolver")
public ClassLoaderTemplateResolver getEmailTemplateResolver() {
ClassLoaderTemplateResolver templateResolver = new
ClassLoaderTemplateResolver();
templateResolver.setOrder(Integer.valueOf(2));
templateResolver.setPrefix("/mail/");
templateResolver.setSuffix(".html");
return templateResolver;
}
It shows the following error when calling the mail template:
Resource resolution by ServletContext with
org.thymeleaf.resourceresolver.ServletContextResourceResolver can only
be performed when context implements org.thymeleaf.context.IWebContext
[current context: org.thymeleaf.context.Context]
How can I force it to use ClassLoaderTemplateResolver for /mail/* templates?
You are probably rendering an html file and want to keep the resultant html file in a string variable which later you will be using it to email by using html to pdf conversion. So the following code will help you in Spring boot:
#Controller
public class jataController {
#Autowired
private TemplateEngine templateEngine;
#GetMapping(value = "/manual-thym")
#ResponseBody
public void justSample() {
Context context = new Context();
String filename = "templates/view/generated-ticket.html";
String html = renderHtml(filename, context);
System.out.println("template\n" + html);
}
private String renderHtml(String filename, Context context) {
ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCacheable(false);
templateResolver.setOrder(1);
templateResolver.setCharacterEncoding("UTF-8");
templateEngine.setTemplateResolver(templateResolver);
String html = templateEngine.process(filename, context);
return html;
}
}
I have a weird problem with my configuration. I'm trying to configure apache tiles with thymeleaf. I'm trying to do the same things they are on theirs documentation page.
I have following configuration file:
#Configuration
public class ViewConfig {
#Bean
public ViewResolver tilesViewResolver() {
ThymeleafViewResolver vr = new ThymeleafViewResolver();
vr.setTemplateEngine(templateEngine());
vr.setViewClass(ThymeleafTilesView.class);
vr.setCharacterEncoding("UTF-8");
vr.setOrder(Ordered.LOWEST_PRECEDENCE);
return vr;
}
#Bean
public ViewResolver thymeleafViewResolver() {
ThymeleafViewResolver vr = new ThymeleafViewResolver();
vr.setTemplateEngine(templateEngine());
vr.setCharacterEncoding("UTF-8");
vr.setOrder(Ordered.HIGHEST_PRECEDENCE);
// all message/* views will not be handled by this resolver;
vr.setExcludedViewNames(new String[]{"message/*"});
return vr;
}
#Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addDialect(new TilesDialect());
templateEngine.setTemplateResolver(templateResolver());
return templateEngine;
}
#Bean
public ThymeleafTilesConfigurer tilesConfigurer() {
ThymeleafTilesConfigurer ttc = new ThymeleafTilesConfigurer();
ttc.setValidateDefinitions(false);
ttc.setDefinitions(new String[]{"/templates/tiles-defs.xml"});
return ttc;
}
#Bean
public TemplateResolver templateResolver() {
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
resolver.setSuffix(".html");
resolver.setPrefix("templates/");
resolver.setTemplateMode("HTML5");
resolver.setCharacterEncoding("UTF-8");
return resolver;
}
}
I've added TemplateResolver for my purposes, but removing it doesn't help (for having the same configuration as documentation).
This is my resource folder structure:
And the problem is that I've got following exception:
Caused by: java.io.FileNotFoundException: ServletContext resource [/templates/tiles-defs.xml] cannot be resolved to URL because it does not exist
The most interesting is that I've checked target/classes (which is in classpath) and there is the file templates/tiles-defs.xml.
I've tried a couple of times to change the definition path (also I've tried to remove definition and get it from default WEB-INF/tiles.xml path) but no outcome - same exception.
What I'm doing wrong?
Here is how I solved it:
#Bean
public ThymeleafTilesConfigurer tilesConfigurer() {
ThymeleafTilesConfigurer ttc = new ThymeleafTilesConfigurer();
//ttc.setValidateDefinitions(false);
ttc.setDefinitions(ThymeleafProperties.DEFAULT_PREFIX + "tiles-defs.xml");
return ttc;
}
My application's Thymeleaf config is set as:
#Bean
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
templateResolver.setCacheable(false);
templateResolver.setTemplateMode("HTML5");
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setPrefix(HTML_VIEWS);
templateResolver.setSuffix(".html");
return templateResolver;
}
#Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addDialect(new SpringSecurityDialect());
templateEngine.addDialect(new LayoutDialect(new GroupingStrategy()));
templateEngine.setTemplateResolver(templateResolver());
return templateEngine;
}
#Bean
public ViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setCharacterEncoding("UTF-8");
viewResolver.setCache(false);
viewResolver.setOrder(1);
return viewResolver;
}
OS (CentOS 7) default encoding is UTF-8 and all generated files get UTF-8 enconding. Still I cannot seem to be able to display characters correctly. I tried a bunch of suggestions found here, to no avail.
I have also tried, as per some suggestions here, setting a CharacterEncodingFilter prior to CsrfFilter in my Spring Security configuration. Also, the app is persisting the data WITH the strange characters.
My securitu configuration starts with:
#Override
protected void configure(HttpSecurity http) throws Exception {
CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
encodingFilter.setEncoding("UTF-8");
encodingFilter.setForceEncoding(true);
http.addFilterBefore(encodingFilter,CsrfFilter.class);
// more security configs
}
What am I missing?
The damned messages_pt_BR.properties file had a different encoding from the rest of the app... It decided to go with ISO-8859-1. Beats me why!
I'm having trouble setting up my Spring MVC (not using Boot, because I started it prior to discovering Spring Initializr) with Thymeleaf to show messages from my resource bundle.
Main configuration class for the app is:
#Configuration
#EnableWebMvc
#ComponentScan(basePackages = "b.c.m.h")
public class HrmConfiguration extends WebMvcConfigurerAdapter {
#Bean
public ViewResolver viewResolver() {
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
templateResolver.setTemplateMode("HTML5");
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setPrefix("/WEB-INF/html/");
templateResolver.setSuffix(".html");
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.addDialect(new SpringSecurityDialect());
engine.addDialect(new LayoutDialect(new GroupingStrategy()));
engine.setTemplateResolver(templateResolver);
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(engine);
viewResolver.setCache(false);
return viewResolver;
}
#Bean(name = "messageSource")
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("/i18n/messages");
messageSource.setFallbackToSystemLocale(false);
messageSource.setCacheSeconds(0);
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
#Bean(name="localeResolver")
public LocaleResolver localeResolver(){
SessionLocaleResolver resolver = new SessionLocaleResolver();
resolver.setDefaultLocale(new Locale("pt", "BR"));
return resolver;
}
#Bean
public UrlTemplateResolver urlTemplateResolver() {
return new UrlTemplateResolver();
}
// other beans and configs
}
The general view of the application is:
And the messages_pt_BR.properties file is:
#messages
spring.messages.basename=i18n/messages
app.name=APPLICATION NAME
But, when I try to show the APPLICATION NAME somewhere on the page, with this:
<p th:text="#{app.name}">app.name</p>
I get only
??app.name_pt_BR??
I have browsed a number of possible solutions and tutorials, including:
I18n in Spring boot + Thymeleaf
http://www.concretepage.com/spring-4/spring-4-mvc-internationalization-i18n-and-localization-l10n-annotation-example
How to display messages in Thymeleaf and Spring Boot?
Still I cannot get it working.
Browser and OS are set to locale en_US.
How to get tihs solved?
Working with Spring Boot here, but I was able to reproduce your issue and it's tied to the ReloadableResourceBundleMessageSource once I changed it to ResourceBundleMessageSource it worked like it should.
#Bean(name = "messageSource")
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("/i18n/messages");
messageSource.setFallbackToSystemLocale(false);
messageSource.setCacheSeconds(0);
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
Update:
Found that you need to provide full pathing for the ReloadableReourceBundleMessageSource so if your /il8n directory is within your classpath you need to set your basename to classpath:/il8n/messages
see my post here: Why does Thymeleaf International only works with ResourceBundleMessageSource
#Bean(name="localeResolver")
public LocaleResolver localeResolver(){
SessionLocaleResolver resolver = new SessionLocaleResolver();
resolver.resolver.setDefaultLocale(new Locale("en"));
return resolver;
}
changed your localresolver bean this might help you . if you have the browser set up to en locale how come the application read the pt_BR change that to _en in the msg properties file
or you can change your browser locale to Portuguese - BRAZIL (BR) (pt_BR) without changing code in anywhere
I found the answer to my problem on this question:
Spring 4 with thymeleaf Internationalization not identify message from resource properties file
Basically, what solved the issue was to separate the following #Bean ...
#Bean
public ViewResolver viewResolver() {
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
templateResolver.setTemplateMode("HTML5");
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setPrefix("/WEB-INF/html/");
templateResolver.setSuffix(".html");
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.addDialect(new SpringSecurityDialect());
engine.addDialect(new LayoutDialect(new GroupingStrategy()));
engine.setTemplateResolver(templateResolver);
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(engine);
viewResolver.setCache(false);
return viewResolver;
}
... into three distinct beans like:
#Bean
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
templateResolver.setCacheable(false);
templateResolver.setTemplateMode("HTML5");
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setPrefix(HTML_VIEWS);
templateResolver.setSuffix(".html");
return templateResolver;
}
#Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addDialect(new SpringSecurityDialect());
templateEngine.addDialect(new LayoutDialect(new GroupingStrategy()));
templateEngine.setTemplateResolver(templateResolver());
return templateEngine;
}
#Bean
public ViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setCharacterEncoding("UTF-8");
viewResolver.setCache(false);
viewResolver.setOrder(1);
return viewResolver;
}
From that, I got it working as I expected.
Thanks for the insights and help!