Using Vaadin Error View instead of Spring Boot's Whitelabel error page - spring

I'm using Spring Boot with Vaadin and. By using the #Autowired SpringNavigator, I have also set the error view:
navigator.setErrorView(ErrorView.class);
But when I type wrong URL like http://localhost:8080/asdf .
I always get the Spring Boot's Whitelabel error page. I know that I can set custom HTML page for the error like
/resources/public/error/404.html
but this is HTML way, I would like to use Vaadin with components,
the best solution would be the mentioned error view.

Related

passing a Spring Exception to a Thymeleaf page

Having added the error.html Thymeleaf template to replace the default error page on Spring how can I get access to the exception on that page?
So I have custom exception thrown on some controller. How to access that exception on the Thymeleaf page?
I want this to work globally, so any controller thrown exception should be accessible.
Case 1: For customized error pages with spring default error info
There are some predefined objects in Thymeleaf to show error information, e.g. ${error}, ${exception} and so on. These objects can be used in your customized error page.
This article will help you more. Custom Error Page with Thymeleaf
Case 2: For customized error pages with customized error info
There are #ControllerAdvice and #ExceptionHandler used to handle exceptions in controllers globally. A certain exception can be add to Modal's attribute and therefore accessed in thymeleaf templates. However, please be noted that the solution does not work in Webflux.
The blog shows details about the solution. Exception Handling in Spring MVC

What is exactly server.error.path property?

In Spring Boot, what is the purpose of server.error.path property in application.properties file?
The documentation just says:
Path of the error controller
But I want a clear description of this property with an example.
server.error.path - used as part of url for error pages.
site.getBaseUrl() + "/error"
For example some error happen on server side and you decide redirect user to error page like this:
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/images/custom-error-page-aws-404-example.png
Code example of error controller you can find here:
https://www.logicbig.com/tutorials/spring-framework/spring-boot/implementing-error-controller.html
You can use this property in #RequestMapping("/error"). But instead of "/error" you can use "${server.error.path}"
UPDATE:
Also, Spring Boot BasicErrorController use server.error.path property
Property server.error.path in spring boot application used to define an error path while dealing with custom error handler. In Spring we create custom error handler using functional interface ErrorController, ths interface has a String type method getErrorPath which helps us to return the error page path(our error page as view).
But from Spring 2.3.0 this getErrorPath() method has been deprecated and replaced with server.error.path to manage the error path.
e.g. server.error.path=/error
For more detail about interface ErrorController, please refer Spring doc for ErrorController

Why i can't see desired data in jsp file

I am new in Java EE and I am building a web application with maven, spring mvc
and hibernate.
I have a problem with the jsp. I can not show the data of object with the expression / jstl
language. In console doesn't show any error. Any suggestions?
Pom.xml:
Controller:
Inicio.jsp:
I guess in the controller you are setting the object with key
model.addObject("usuario", usuarioo);
The first character is small 'u'.
And in your jsp you are trying to access using Usuario. Thus the value is not getting printed.
Kindly use something like
${usuario.nombre}

Mapping /error does not work in spring boot

I am developing a spring boot application, using boot v 1.2.5, using thymeleaf as the view technology
In my Mvc Configuration file
#Configuration
public class MVCConfig extends WebMvcConfigurerAdapter {
#Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/message").setViewName("message");
registry.addViewController("/error").setViewName("errors/error");
registry.addViewController("/login").setViewName("authentication/login");
}
The problem is that the first and last mapping is working fine, but the middle one "error" is not working at all !!
I am using the same rules as the other two templates, same prefix, same directory structure, so it is supposed to work like the other two !!
But, when i point my browser to /error, spring boot show the fallback error page saying that there is no mapping for /error
when i change it to:
registry.addViewController("/error").setViewName("errors/error");
it works fine for /errors
Spring boot documentation states about the /error handling:
"For browser clients there is a ‘whitelabel’ error view that renders the same data in HTML format (to customize it just add a View that resolves to ‘error’)."
so, it says to add a view that resolve to error, that's what i did, but it does not work !!
so, what is the problem? why /error does not work?
You've added a view controller for /error, not a view. This is then being overridden by Boot's own controller with a /error mapping. As a result, you get the default error page.
All you need to do is to provide an error view. As you're using Thymeleaf, you can do so by creating a file named src/main/resources/templates/error.html.

Custom page not found and other error webpages in Spring 3.0

I want to display a custom 404 page not found error page (among others). I'm using Spring 3.0 and don't know how to do this.. I know I can specify a jsp page in web.xml to handle 404 errors. But I want Spring's environment for my error pages. So I tried simply returning a ModelAndView that's basically an error page. But the problem there is once I do this:
response.sendError(HttpServletResponse.SC_NOT_FOUND);
Then the whole request just gets forwarded back to the container's default 404 page. How are we supposed to handle error pages in Spring 3.0?
In Servlet 2.4, response.sendError() and response.setStatus() are treated differently. The former is handled by container, but the latter gives option to provide the response yourself. That means, you have to use response.setStatus(HttpServletResponse.SC_NOT_FOUND). Please also see How do I return a 403 Forbidden in Spring MVC?

Resources