Katkat

Answer Spring Boot and Thymeleaf engineering questions

Walmart LabsSoftware Engineer · Technical Screen

Last updated

This problem tests your understanding of Spring Boot MVC architecture, specifically how to manage session-scoped state, optimize view rendering, and implement robust server-side validation using Thymeleaf.

The Scenario

You are building an internal administration portal using Spring Boot and Thymeleaf. The application requires a user to navigate through a multi-step form to create a new product inventory record. The data must persist across multiple HTTP requests until the final submission.

The Task

  1. Design a robust way to maintain the state of the form across different pages. Explain the trade-offs between using @SessionAttributes versus a custom session-scoped bean.
  2. Implement a validation mechanism for the form data. If validation fails on the final submission, the user must be redirected back to the specific step where the invalid field exists, displaying the error message next to the input field using Thymeleaf's th:errors attribute.
  3. How would you configure a global ExceptionHandler to ensure that if a session times out during this process, the user is redirected to a custom 'Session Expired' view rather than a generic 500 error page?

Technical Requirements

  • Provide a code snippet demonstrating the Controller method handling the final submission.
  • Explain how you would prevent CSRF (Cross-Site Request Forgery) attacks in this form, assuming the default Spring Security configuration is present.

Example Context

Imagine the user is on step 3 of 4. They submit the form, and the server detects that the 'price' field is negative. The user should be returned to the 'step 3' view with the BindingResult showing the error.

Constraints

  • Use standard Spring Boot 3.x conventions.
  • Ensure the implementation is thread-safe for concurrent user requests.

Hints

Hint 1

Consider that @SessionAttributes stores state in the HttpSession, which can lead to memory pressure if many users maintain large objects.

Hint 2

Use the BindingResult object to capture and propagate validation errors from the backend to the Thymeleaf template.

Hint 3

Look into @ControllerAdvice and the @ExceptionHandler annotation to handle SessionTimeoutException globally.

Follow-up questions

  • How would you improve the performance of this page if it were required to display 10,000 product rows instead of a single form?
  • How do you ensure accessibility in your Thymeleaf templates so that screen readers correctly identify validation error messages?
  • If the application needs to scale horizontally, what changes must be made to the session management strategy?