-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathGlobalMessageControllerAdvice.java
More file actions
47 lines (44 loc) · 1.92 KB
/
Copy pathGlobalMessageControllerAdvice.java
File metadata and controls
47 lines (44 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.digitalsanctuary.spring.user.web;
import java.util.Locale;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.context.request.WebRequest;
import lombok.RequiredArgsConstructor;
/**
* Controller advice that provides global model attributes across all MVC controllers.
*
* <p>This class handles automatic resolution of message keys from request parameters,
* allowing controllers to redirect with a {@code messageKey} parameter that gets
* automatically resolved to a localized message and added to the model.</p>
*
* <p>Usage example: Redirect to {@code /somepage?messageKey=user.created.success} and
* the resolved message will be available as the {@code message} model attribute.</p>
*
* @author Devon Hillard
* @see org.springframework.web.bind.annotation.ControllerAdvice
* @see org.springframework.context.MessageSource
*/
@ControllerAdvice(annotations = Controller.class)
@RequiredArgsConstructor
public class GlobalMessageControllerAdvice {
private final MessageSource messages;
/**
* Adds a localized message to the model if a `messageKey` GET parameter is present.
*
* @param request the web request
* @param model the model
*/
@ModelAttribute
public void addMessageFromKey(WebRequest request, org.springframework.ui.Model model) {
// Retrieve the `messageKey` parameter from the request
String messageKey = request.getParameter("messageKey");
if (messageKey != null) {
Locale locale = request.getLocale();
// Use the messageKey itself as the default if no translation is found
String message = messages.getMessage(messageKey, null, messageKey, locale);
model.addAttribute("message", message);
}
}
}