Thymeleaf - return rendered template to String - spring-boot

I was using this solution as an example:
Can I render Thymeleaf templates manually from a String?
but after processing template I am getting not rendered template:
my template (plans/test.html):
<!doctype html>
<html lang="pl">
<div th:text="${loki}"></div>
</html>
Java code that should renderd the template:
#RestController
public class PlansPageRestController {
#Autowired
TemplateEngine myTemplateEngine;
#RequestMapping(value = {"/public/plans"}, method = RequestMethod.POST, produces = "application/json")
public Map<String,String> getPlans(#RequestParam Map<String, String> requestParams) {
Context ctx = new Context();
ctx.setVariable("loki", "Some test value");
String htmlTemplate = myTemplateEngine.process("plans/test.html", ctx);
Map<String,String> result = new HashMap<>();
result.put("html", htmlTemplate );
result.put("result", "success" );
return result;
}
}
but as a result I am getting content of plans/test.html so:
<!doctype html>
<html lang="pl">
<div th:text="${loki}"></div>
</html>
I am working with spring boot 3.0.0 and regarding to pom I am using thymeleaf:
<artifactId>thymeleaf-spring6</artifactId>
<version>3.1.0.RELEASE</version>
Can anyone help me in finding what I am doing wrong?
my thymeleaf configuration:
#Configuration
public class ThymeleafConfiguration implements WebMvcConfigurer, ApplicationContextAware {
private ApplicationContext applicationContext;
#Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
#Bean
public TemplateEngine myTemplateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setEnableSpringELCompiler(true);
engine.setTemplateResolver(templateResolver());
engine.setDialect(new LayoutDialect());
return engine;
}
private ITemplateResolver templateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(applicationContext);
resolver.setPrefix("classpath:/templates/");
resolver.setTemplateMode(TemplateMode.HTML);
resolver.setCacheable(false);
resolver.setCacheTTLMs(0L);
resolver.setCharacterEncoding("UTF-8");
return resolver;
}
}

The reason your standard Thymeleaf expression is not being evaluated is because you have replaced the Thymeleaf "standard" dialect with the layout dialect:
engine.setDialect(new LayoutDialect());
(I should say "the Spring dialect", given you are using Spring-Thymeleaf.)
If you need to use the layout dialect, then you can add it to the engine - and still keep the standard dialect as well:
engine.addDialect(new LayoutDialect());
Or, you may not need the layout dialect at all (you do not use it in the sample HTML file in the question, but maybe you use it elsewhere). If that is the case, you can remove this line of code.
Just to add: The default Spring Boot settings should work without you needing to define any ThymeleafConfiguration class. But, again, there may be other places where you need to use a custom configuration (e.g. the UTF-8 encoding).

Related

spring boot thymeleaf does not work with LiteDeviceDelegatingViewResolver

I am working with spring boot 2.0.1.RELEASE, using spring-boot-starter-thymeleaf. And I have two version of pages for PC and Mobile phone.
Here is a simple version of my project structure
project structure
I would like to have the site automatically detect the PC browser and Mobile phone browser so that it can map the same requesting URL to different pages according to the type of the browsers
The html file is very simple as the following.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>This is from PC web browser</p>
</body>
</html>
Here is the code of my controller.
#Controller
public class MyController {
#RequestMapping("/")
public String mainDefault(){
return "home";
}
#RequestMapping("/home")
public String main(){
return "home";
}
}
To detect the device I wrote the following configuration class
#Configuration
public class Config implements WebMvcConfigurer {
#Autowired
private ApplicationContext applicationContext;
#Bean
public LiteDeviceDelegatingViewResolver liteDeviceDelegatingViewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setCharacterEncoding("UTF-8");
LiteDeviceDelegatingViewResolver resolver = new LiteDeviceDelegatingViewResolver(viewResolver);
resolver.setNormalPrefix("web/");
resolver.setMobilePrefix("mobile/");
resolver.setTabletPrefix("web/");
resolver.setOrder(1);
resolver.setEnableFallback(true);
return resolver;
}
#Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addDialect(new LayoutDialect());
templateEngine.setTemplateResolver(templateResolver());
templateEngine.setEnableSpringELCompiler(true);
return templateEngine;
}
#Bean
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setApplicationContext(this.applicationContext);
templateResolver.setTemplateMode(TemplateMode.HTML);
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setPrefix("/templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(".HTML5");
templateResolver.setCacheable(false);
return templateResolver;
}
#Override
public void addInterceptors(InterceptorRegistry registry) {
DeviceResolverHandlerInterceptor deviceResolverHandlerInterceptor = new DeviceResolverHandlerInterceptor();
SitePreferenceHandlerInterceptor sitePreferenceHandlerInterceptor = new SitePreferenceHandlerInterceptor();
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
registry.addInterceptor(localeChangeInterceptor);
registry.addInterceptor(deviceResolverHandlerInterceptor);
registry.addInterceptor(sitePreferenceHandlerInterceptor);
WebMvcConfigurer.super.addInterceptors(registry);
}
}
Then when I tried to run the Application. I always get the following error. It seems it can map to the right resource which is [/templates/web/home.html]. But it always say Could not open ServletContext resource [/templates/web/home.html]
If I visit from a mobile browser it map to [/templates/mobile/home.html] which is also correct.
Can any one help me? Thank you in advance.
2018-04-19 17:18:57.040 ERROR 17732 --- [nio-9020-exec-9] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-9020-exec-9] Exception processing template "web/home": An error happened during template parsing (template: "ServletContext resource [/templates/web/home.html]")
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "ServletContext resource [/templates/web/home.html]")
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:235) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:666) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
.........
Caused by: java.io.FileNotFoundException: Could not open ServletContext resource [/templates/web/home.html]
at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:159) ~[spring-web-5.0.5.RELEASE.jar:5.0.5.RELEASE]
at org.thymeleaf.spring5.templateresource.SpringResourceTemplateResource.reader(SpringResourceTemplateResource.java:103) ~[thymeleaf-spring5-3.0.9.RELEASE.jar:3.0.9.RELEASE]
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:223) ~[thymeleaf-3.0.9.RELEASE.jar:3.0.9.RELEASE]
... 52 common frames omitted
2018-04-19 17:18:57.047 DEBUG 17732 --- [nio-9020-exec-9] o.s.web.servlet.DispatcherServlet : Error rendering view [org.thymeleaf.spring5.view.ThymeleafView#139a953a] in DispatcherServlet with name 'dispatcherServlet'
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "ServletContext resource [/templates/web/home.html]")
You are trying to configure something that is already being configured for you.
Make sure you have the spring-boot-starter-thymeleaf and that you have the spring-mobile-autoconfguration in your list of dependencies.
NOTE: I assume you are using the snapshot builds so that you are compatible with regards to Spring and Spring Boot version.
Remove your Config class
Add the following to your application.properties
spring.mobile.devicedelegatingviewresolver.normalPrefix=web/
spring.mobile.devicedelegatingviewresolver.tabletPrefix=web/
spring.thymeleaf
Restart your application

Spring MVC VersionResourceResolver / ContentVersionStrategy not working correctly in JSP

I have a Spring MVC (4.3.0) application and have registered a VersionResourceResolver with added ContentVersionStrategy with the ResourceHandlerRegistry. I have the ResourceUrlEncodingFilter enabled.
#Bean
public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
return new ResourceUrlEncodingFilter();
}
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
boolean devMode = this.env.acceptsProfiles("local");
//boolean useResourceCache = !devMode;
Integer cachePeriod = devMode ? 0 : (60 * 60 * 24 * 365); //If dev clear cache else 1 year
registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources/")
.setCachePeriod(cachePeriod)
.resourceChain(false)
.addResolver(new VersionResourceResolver()
.addContentVersionStrategy("/**"))
.addTransformer(new AppCacheManifestTransformer());
}
When I access anything in /resources (JS, Images, CSS, etc) on a JSP page using the c:url or spring:url tags, the "versioned" URL does not show up (meaning: no hash code in the URL). Example:
<link href="<c:url value="/resources/css/views/login.css" />" rel="stylesheet">
Produces: /myapp/resources/css/views/login.css as the URL string when inspecting the page.
But, if I use a ResourceURLProvider in my Controller, I do see the hash code in the URL:
#Autowired
private ResourceUrlProvider mvcResourceUrlProvider;
#RequestMapping(value = { "/" }, method = RequestMethod.GET)
public String projectBaseRedirect() {
logger.debug("js = '" + this.mvcResourceUrlProvider.getForLookupPath("/resources/js/views/overview.js") + "'");
logger.debug("css = '" + this.mvcResourceUrlProvider.getForLookupPath("/resources/css/views/overview-page.css") + "'");
return "redirect:/admin/overview";
}
The log messages produce this:
2016-07-09 11:47:19 DEBUG AdminLoginController:35 - js = '/resources/js/views/overview-36d1ff98d627d92a72d579eca49dbd8a.js'
2016-07-09 11:47:19 DEBUG AdminLoginController:36 - css = '/resources/css/views/overview-page-d47f10e5bcf0fdd67bd8057479b523f0.css'
Why is this working in the controller but not on my JSP pages?
I am also using Spring Security (4.1.0)...
As it is missing in your example it is maybe missing in your project as well. You need a resource provider for the template engine you're using. In case of JSP, register a filter:
#Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
#Bean
public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
return new ResourceUrlEncodingFilter();
}
}
Or use a filter registration bean with that filter:
#Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
#Bean
public FilterRegistrationBean filterRegistrationBean() {
final FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new ResourceUrlEncodingFilter());
filterRegistrationBean.addUrlPatterns("/*");
return filterRegistrationBean;
}
}
Rossen's comment on Michael's answer was exactly the missing glue I needed. After I put this code in the class that extends AbstractAnnotationConfigDispatcherServletInitializer, it worked perfectly with the other code from my original post!
#Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
FilterRegistration.Dynamic fr = servletContext.addFilter("resourceUrlEncodingFilter",
new ResourceUrlEncodingFilter());
fr.setInitParameter("encoding", "UTF-8");
fr.setInitParameter("forceEncoding", "true");
fr.addMappingForUrlPatterns(null, true, "/*");
}

Collisions may occur when using Spring #Cacheable and SimpleKeyGenerator

When I use #Cacheable and call different method with same parameter, it generated a same key.
SimpleKeyGenerator generated key without cache names.
I use spring-boot 1.3.2 with spring 4.2.4.
Here is a sample:
#Component
public static class CacheableTestClass {
#Cacheable(cacheNames = "test-cacheproxy-echo1")
public String echo1(String text) {
return text;
}
#Cacheable(cacheNames = "test-cacheproxy-echo2")
public String echo2(String text) {
return "Another " + text;
}
}
And run a test:
assertEquals("OK", cacheableTestClass.echo1("OK"));
assertEquals("Another OK", cacheableTestClass.echo2("OK")); // Failure: expected 'Another OK', actual 'OK'.
So, is there a way to resolve this issue?
Thanks a lot.
Update
Here is my CacheManager configuration.
#Bean
#ConditionalOnMissingBean(name = "cacheRedisTemplate")
public RedisTemplate<Object, Object> cacheRedisTemplate(
RedisConnectionFactory redisConnectionFactory)
throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setHashKeySerializer(template.getKeySerializer());
return template;
}
#Bean
public RedisCacheManager cacheManager(#Qualifier("cacheRedisTemplate") RedisTemplate<Object, Object> cacheRedisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(cacheRedisTemplate);
cacheManager.setDefaultExpiration(
redisCacheProperties().getDefaultExpiration());
cacheManager.setExpires(redisCacheProperties().getExpires());
return cacheManager;
}
This has nothing to do with SimpleKeyGenerator but this is a redis-specific issue that does not use the name of the cache as a discriminant for the key it uses to store the value.
You need to invoke setUsePrefix(true) on your RedisCacheManager. This is what Spring Boot does when it auto-configures the cache manager for you. Note that it should have been the default and we're discussing how we can improve the out-of-the-box experience in a future release

Migration of thymeleaf version 2.14 to 3.0 not finding message properties

Thymeleaf version 2.14 was picking up message.properties file correctly. After migration to 3.0,it is giving error ??hello.MESSAGE_en_US??.
But in javacode by autowiring messsagesource
messageSource.getMessage( "hello.MESSAGE",null, Locale.getDefault()) ->Hello App
The project structure
src/main/
-java
-com.cando
-controllers
-resources
-messages_en.properties
-spring.properties
-webapp
-WEB-INF
-templates
-index.html
index.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Helllo</title>
</head>
<body>
<p th:text="#{hello.MESSAGE}">Hello App!</p>
</body>
</html>
messages_en.properties
hello.MESSAGE=Hello App
config
#Bean
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
resourceBundleMessageSource.setBasename("messages");
resourceBundleMessageSource.setBasename("spring");
return resourceBundleMessageSource;
}
In your config file you setBasename() to messages and then override value to spring. I think ResourceBundleMessageSource just doesn't see your messages file, and you should add it like: setBasenames("messages","spring"). Give it a try and tell if it works :)
I think i found solution
While you configure SpringTemplateEngine it's possible to explicitly set messageSource and messageResolver.
For me, this works.
My code in kotlin:
#Configuration
#ComponentScan
open class ThymeleafConfig {
#Autowired
lateinit var applicationContext: ApplicationContext
#Autowired
lateinit var messageSource: MessageSource
#Autowired
lateinit var messageResolver: SpringMessageResolver
#Bean
open fun viewResolver(): ViewResolver = ThymeleafViewResolver().apply
{
templateEngine = templateEngine()
characterEncoding = "UTF-8"
}
fun templateEngine(): TemplateEngine = SpringTemplateEngine().apply {
enableSpringELCompiler = true
setTemplateResolver(templateResolver())
setMessageSource(messageSource)
addMessageResolver(messageResolver)
}
fun templateResolver(): ITemplateResolver = SpringResourceTemplateResolver().apply {
prefix = "file:./web/templates/"
suffix = ".html"
templateMode = TemplateMode.HTML
cacheTTLMs = 0
isCacheable = false
setApplicationContext(applicationContext)
}
}
#Component
class MessageResolverImpl: SpringMessageResolver() { }
and messageSource bean:
#Bean
open fun messageSource(): MessageSource {
val source = ReloadableResourceBundleMessageSource()
source.setBasename("classpath:messages")
source.setUseCodeAsDefaultMessage(true)
source.setDefaultEncoding("UTF-8")
return source
}
Hope this will help you

Root Context path in spring application

I am running application using tomcat server, when server start i will get url
http://localhost:8080/TestApp/
and displaying index.jsp file but when i click link in index file it is displaying url like
http://localhost:8080/testsuccess
but it should display like
http://localhost:8080/TestApp/testsuccess
can any please help me to solve this.
SpringConfiguration.java
#Configuration
#EnableWebMvc
#ComponentScan("com.testapp")
#EnableTransactionManagement
public class SpringConfiguration {
#Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
SpringWebAppInitializer.java
public class SpringWebAppInitializer implements WebApplicationInitializer {
#Override
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
appContext.register(SpringConfiguration.class);
ServletRegistration.Dynamic dispatcher = container.addServlet(
"SpringDispatcher", new DispatcherServlet(appContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
MyController.java
#Controller
public class MyFirstController
{
#RequestMapping(value = "/" , method = RequestMethod.GET)
public String testApp() throws Exception{
return "index";
}
#RequestMapping(value = "/testsuccess", method = RequestMethod.GET)
public String testAppSuccess() {
Map<String, Object> model = new HashMap<String, Object>();
return "success";
}
}
got it i should use
Next
it will give the context path.
I think your problem comes from the link inside your index.jsp. They might look like ... You should use either jstl or spring tag lib to handle links / urls in your pages. They both have the ability to prepend the deployment / context path of your application.
jstl example:
with taglib xmlns:c="http://java.sun.com/jsp/jstl/core" included you can create an anchor like ..
spring example:
with taglib xmlns:spring="http://www.springframework.org/tags" your link will be created in two steps:
<spring:url value="/testsuccess" var="myurl" htmlEscape="true"/>
...
Update: Wrong function name in jstl taglib version.

Resources