2

Context : I'm a french developer working in a team to develop a crowdfunding platform based on JHipster.

In JHipster, when accessing http://domain.com/unknown, the retrieved page is a server-side error page.

What I'd like to know : - how thymeleaf knows it has to return error.html ? - where do the parameters come from and how are they, especially for variables like in

<span th:text="${error}">

--> where does error come from (same for status and message)? Is there some sort of server-side controller somewhere?

Another question which is a corollary : why is there 2 error pages in jhipster, one client and one server? Shouldn't there be just one client side page as JHipster is intended for SPA. Why isn't server always sending back index.html, letting client test is there is an error or not?

Last question which is a corollary too : is there some way to request dynamic server side (thymeleaf) templates in client side (angular) routing (in app.js file). That would be great for developping enriched SPAs.

PS : Thank you Julien for your work

HammerGuy
  • 101
  • 8

1 Answers1

7

What I'd like to know : - how thymeleaf knows it has to return error.html ?

Two parts. First, Spring is configured to use Thymeleaf for all server side views. In JHipster, the Application is annotated like this:

@ComponentScan
@EnableAutoConfiguration(exclude = {MetricFilterAutoConfiguration.class, MetricRepositoryAutoConfiguration.class})
public class Application {

The @EnableAutoConfiguration is the key to understanding how Thymeleaf is setup. It triggers ThymeleafAutoConfiguration which creates the Thymeleaf view resolver and whatnot. Note that JHipster has a ThymeleafConfiguration class, but this is only used for emails and is not related to your question.

Second part... Spring Boot uses "/error" as the default error view. From the docs:

Spring Boot provides an /error mapping by default that handles all errors in a sensible way, and it is registered as a ‘global’ error page in the servlet container. For machine clients it will produce a JSON response with details of the error, the HTTP status and the exception message. 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’).

When resolving the error view, Spring will add the correct suffix for the view resolver that you're using (the first part that I described). For example, .html for Thymleaf, .vm for Velocity, or .ftl for Freemarker.

Is there some sort of server-side controller somewhere?

Yes. JHipster is also using Spring Boot Actuator. Check out this section of the docs. The main class of interest is ErrorMvcAutoConfiguration. Specifically this part:

@Bean
@ConditionalOnMissingBean(value = ErrorAttributes.class, search = SearchStrategy.CURRENT)
public DefaultErrorAttributes errorAttributes() {
    return new DefaultErrorAttributes();
}

@Bean
@ConditionalOnMissingBean(value = ErrorController.class, search = SearchStrategy.CURRENT)
public BasicErrorController basicErrorController(ErrorAttributes errorAttributes) {
    return new BasicErrorController(errorAttributes);
}

where does error come from (same for status and message)?

See the code I just referenced above and then look in DefaultErrorAttributes:

Map<String, Object> errorAttributes = new LinkedHashMap<String, Object>();
errorAttributes.put("timestamp", new Date());
// and so on...

Another question which is a corollary : why is there 2 error pages in jhipster, one client and one server? Shouldn't there be just one client side page as JHipster is intended for SPA. Why isn't server always sending back index.html, letting client test is there is an error or not?

There needs to be a server side error handler. Not everything will map to an Angular route. For example, www.yoursite.com/doesnt_exist would cause the browser to make another request to the server.

Last question which is a corollary too : is there some way to request dynamic server side (thymeleaf) templates in client side (angular) routing (in app.js file). That would be great for developping enriched SPAs.

Yes. You can specify a JS function for templateUrl in app.js.

John R
  • 2,066
  • 1
  • 11
  • 17