Thanks to @Alexey's answer which pushed me in the right direction.
This MyPageController
controller does the trick. Just like the DXA PageController
it is very greedy, that means it wants to match all requests. See code and explanation below.
@Controller
public class MyPageController {
// Inject the PageController, because we cannot override and change the RequestMapping
@Autowired
private PageController pageController;
// This mapping is more specific than the DXA PageController mapping
@RequestMapping(
value = {"/**"},
produces = {"text/html", "*/*"},
method = {RequestMethod.GET, RequestMethod.POST}
)
public String handleGetPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
String view = pageController.handleGetPage(request, response);
PageModel pageModel = (PageModel) request.getAttribute("pageModel");
pageModel = this.enrichModel(pageModel, request);
request.setAttribute("pageModel", pageModel);
return view;
}
// enrich the page model, with the DXA enrichModel() pattern
protected PageModel enrichModel(PageModel pageModel, HttpServletRequest httpServletRequest) throws Exception {
//do your thing here
pageModel.setTitle("Hello, World!");
return pageModel;
}
}
Indeed the @RequestMapping
cannot be overridden, but like Alexey shows that is not needed.
The RequestMapping
(route) needs to be unique which is a challenge, since the DXA PageController
registers a greedy catchall route. By adding the request method
to the RequestMapping
, the MyPageController
mapping is more specific than the DXA PageController
one.
From the DXA PageController
@RequestMapping(
value = {"/**"},
produces = {"text/html", "*/*"}
)
public String handleGetPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
From MyPageController
@RequestMapping(
value = {"/**"},
produces = {"text/html", "*/*"},
method = {RequestMethod.GET, RequestMethod.POST}
)
public String handleGetPage(HttpServletRequest request, HttpServletResponse response) throws Exception {