It was mentioned earlier that Springboot does not recommend using JSP, and Thymeleaf is used by default. This practice case is directly based on the exception handling.
1, add thymeleaf dependency, add the following dependency in the Pom file: <!--thymeleaf---<dependency> <groupId> Org.springframework.boot</groupid> <artifactId>spring-boot-starter-thymeleaf</artifactId> < /dependency>
2. Define Exception page: <! DOCTYPE html> Path Source Reference Source: Thymeleafproperties, Thymeleaftemplateavailabilityprovider can
3. Add exception handling method in the previous welcome control class:/** * Captures all exceptions within class * @param ex * @return */@ExceptionHandler public Modelandview Exceptionhandelb Ythymeleaf (Exception ex,httpservletrequest req) {Modelandview mav=new modelandview (); Mav.setviewname ("error"); Mav.addobject ("Exception", ex); Mav.addobject ("url", Req.getrequesturl ()); return MAV; }
4, at this time the system by default is the Thymeleaf template, once the exception will be defined in 2 in the directory to find the corresponding error.html page:
5, then we directly access the exception page, whether it is also possible, first through the address http://localhost:8083/error.html request:
and found that using Http://localhost:8083/error is the same result.
6, where the error message is clearly indicated that there is no corresponding exception object in the request domain, then we adjust the error.html page: <! DOCTYPE html> At this point, the direct request discovery is still the error page in the previous 5 steps, attempting to restart the service.
7, directly request the error page address:
This restart service verification indicates that the HTML page under this deployment path needs to be restarted before it can be reloaded. Some differences with Web-inf, the previous JSP page can directly modify the direct loading.
8. At this point we still keep the bean of the JSP view parser that we configured earlier, and try to jump to the JSP page:
Console info: Org.thymeleaf.exceptions.TemplateInputException:Error resolving template "Welcome", template might not exist or M Ight not being accessible by any of the configured Template resolvers at Org.thymeleaf.TemplateRepository.getTemplate (Templat erepository.java:246) ~[thymeleaf-2.1.5.release.jar:2.1.5.release] at org.thymeleaf.TemplateEngine.process ( templateengine.java:1104) ~[thymeleaf-2.1.5.release.jar:2.1.5.release] at org.thymeleaf.TemplateEngine.process ( templateengine.java:1060) ~[thymeleaf-2.1.5.release.jar:2.1.5.release] at org.thymeleaf.TemplateEngine.process ( templateengine.java:1011) ~[thymeleaf-2.1.5.release.jar:2.1.5.release] at Org.thymeleaf.spring4.view.ThymeleafView.renderFragment (thymeleafview.java:335) ~[ Thymeleaf-spring4-2.1.5.release.jar:2.1.5.release] Description not found before
If we cancel the dependency added in this 1, ask again:
The request was successful, so are we really unable to use thymeleaf with the JSP view template at the same time? The concrete argument is explained in the next practice.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.