1, Overview
2, the code
1), pom.xml
Here Note: After springboot 2.2.0 freemarker default file suffix: ftlh. 2.2.1 is used in this case, the suffix ftlh
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> </dependencies>
2)、application.properties
# Must be closed whitelabel, otherwise you can not navigate to the page error ftlh
server.error.whitelabel.enabled=false
server.error.include-stacktrace=always
3), java code and https://www.cnblogs.com/yaoyuan2/p/11873110.html consistent
4)、5xx.ftlh
<!DOCTYPE html> <html lang="en"> <head> <style> table td{ vertical-align:top; border:solid 1px #888; padding:10px; } </style> </head> <body> <h1>My FreeMarker 5xx Error Page</h1> <table> <tr> <td>Date</td> <td>${timestamp?datetime}</td> </tr> <tr> <td>Error</td> <td>${error}</td> </tr> <tr> <td>Status</td> <td>${status}</td> </tr> <tr> <td>Message</td> <td>${message}</td> </tr> <tr> <td>Exception</td> <td>${exception!""}</td> </tr> <tr> <td>Trace</td> <td> <pre>${trace}</pre> </td> </tr> </table> </body> </html>
Note: freemarker in $ {exception} is empty, result in an error, and then perform less than the front page template. So, here coupled with! "", Expressed as empty or not found "" .
5)、404.ftlh
<!DOCTYPE html> <html lang="en"> <head> <style> table td{ vertical-align:top; border:solid 1px #888; padding:10px; } </style> </head> <body> <h1>My FreeMarker 404 Error Page</h1> <table> <tr> <td>Date</td> <td>${timestamp?datetime}</td> </tr> <tr> <td>Error</td> <td>${error}</td> </tr> <tr> <td>Status</td> <td>${status}</td> </tr> <tr> <td>Message</td> <td>${message}</td> </tr> <tr> <td>Exception</td> <td>${exception!"No exception thrown"}</td> </tr> <tr> <td>Trace</td> <td> <pre>${trace!"No Stacktrace available"}</pre> </td> </tr> </table> </body> </html>
6)、error.ftlh
<!DOCTYPE html> <html lang="en"> <head> <style> table td{ vertical-align:top; border:solid 1px #888; padding:10px; } </style> </head> <body> <h1>My FreeMarker Custom Global Error Page</h1> <table> <tr> <td>Date</td> <td>${timestamp?datetime}</td> </tr> <tr> <td>Error</td> <td>${error}</td> </tr> <tr> <td>Status</td> <td>${status}</td> </tr> <tr> <td>Message</td> <td>${message}</td> </tr> <tr> <td>Exception</td> <td>${exception!"No exception"}</td> </tr> <tr> <td>Trace</td> <td> <pre>${trace!"No trace"}</pre> </td> </tr> </table> </body> </html>
3, execution
to sum up:
1, the directory structure is:
templates
|_error.ftlh
|_error
|_5xx.ftlh
|_404.ftlh
2, application.properties must be added:
server.error.whitelabel.enabled=false
If you want to display trace, but also added:
server.error.include-stacktrace=always