Created
February 8, 2019 01:54
-
-
Save philwebb/891dcfeb2c2d1f4f1ba6e02b831e703a to your computer and use it in GitHub Desktop.
Null rendering in thymeleaf
This file contains hidden or 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 com.example.demo; | |
import java.util.Collection; | |
import org.springframework.beans.factory.ObjectProvider; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.context.annotation.Bean; | |
import org.thymeleaf.context.IExpressionContext; | |
import org.thymeleaf.dialect.IDialect; | |
import org.thymeleaf.spring5.SpringTemplateEngine; | |
import org.thymeleaf.spring5.dialect.SpringStandardDialect; | |
import org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator; | |
import org.thymeleaf.standard.expression.IStandardVariableExpression; | |
import org.thymeleaf.standard.expression.IStandardVariableExpressionEvaluator; | |
import org.thymeleaf.standard.expression.StandardExpressionExecutionContext; | |
import org.thymeleaf.templateresolver.ITemplateResolver; | |
@SpringBootApplication | |
public class DemoApplication { | |
@Bean | |
public SpringTemplateEngine templateEngine( | |
Collection<ITemplateResolver> templateResolvers, | |
ObjectProvider<IDialect> dialects) { | |
SpringTemplateEngine engine = new SpringTemplateEngine(); | |
templateResolvers.forEach(engine::addTemplateResolver); | |
dialects.orderedStream().forEach(engine::addDialect); | |
engine.setDialect(new ExtendedSpringDialect()); | |
return engine; | |
} | |
public static void main(String[] args) { | |
SpringApplication.run(DemoApplication.class, args); | |
} | |
private static class ExtendedSpringDialect extends SpringStandardDialect { | |
@Override | |
public IStandardVariableExpressionEvaluator getVariableExpressionEvaluator() { | |
return NullSafeSPELVariableExpressionEvaluator.INSTANCE; | |
} | |
} | |
private static class NullSafeSPELVariableExpressionEvaluator implements | |
IStandardVariableExpressionEvaluator { | |
private static final NullSafeSPELVariableExpressionEvaluator INSTANCE = new NullSafeSPELVariableExpressionEvaluator(); | |
@Override | |
public Object evaluate(IExpressionContext context, | |
IStandardVariableExpression expression, | |
StandardExpressionExecutionContext expContext) { | |
return postProcess(SPELVariableExpressionEvaluator.INSTANCE.evaluate(context, | |
expression, expContext)); | |
} | |
private Object postProcess(Object result) { | |
if (result == null) { | |
return "- -"; | |
} | |
return result; | |
} | |
} | |
} |
This file contains hidden or 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
<!DOCTYPE HTML> | |
<html xmlns:th="http://www.thymeleaf.org"> | |
<head> | |
<title>Getting Started: Serving Web Content</title> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | |
</head> | |
<body> | |
<p th:text="'Hello, ' + ${{first}} + ' ' + ${{last}} + '!'" /> | |
</body> | |
</html> |
This file contains hidden or 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 com.example.demo; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.servlet.ModelAndView; | |
@Controller | |
public class MyController { | |
@RequestMapping("/hello") | |
public ModelAndView hello() { | |
ModelAndView modelAndView = new ModelAndView("hello"); | |
modelAndView.addObject("first", "Phil"); | |
modelAndView.addObject("last", null); | |
return modelAndView; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment