I want to show my custom exception that is being thrown in Java code in Thymeleaf.
@Override
public UserDetails loadUserByUsername(String benutzername) throws UsernameNotFoundException {
Benutzer benutzer = userRepository.findByBenutzername(benutzername);
if (benutzer == null || ! benutzer.isActive()) {
throw new UsernameNotFoundException("Wrong username or password was provided!");
}
<div th:if="${param.error}" >
<div class="alert alert-danger">
Here should my custom exception appear from above.
</div>
</div>
I throw a new UsernameNotFoundException with a custom exception and this exception should be shown in my template, however, I do not know how this should be done.
Define the property in application.properties to enable the stacktrace to be included as expression attribute in Thymeleaf view.
and then create an error page named
error.htmllike below.Refer this for full example.