Creating a debug controller
@DebugWebController
Debug web controllers are just like any other controller bean with RequestMapping
methods, except they are annotated
with @DebugWebController
instead of the standard @Controller
. Any bean annotated with @DebugWebController
will be picked up by the DebugWebModule and will have its mappings prefixed with the debug web root path.
@DebugWebController
public class DebugEhcacheController
{
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test() {
return "th/mymodule/debug/test";
}
}
Thymeleaf template
Unless a named template is specified, the default Thymeleaf template from debug web will be applied to the output. The default template will add a top menu for all registered debug web controllers. Like any Across web Thymeleaf template, this implementation requires you to put the actual output in a content fragment.
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>My test debug controller</title>
</head>
<body th:fragment="content">
This is a test debug controller.
</body>
</html>
If you need additional javascript or custom CSS, you can register it using the WebResourceRegistry
and it will
automatically be added to the output by the layout template.
@DebugWebController
public class DebugEhcacheController
{
@ModelAttribute
public void registerCss( WebResourceRegistry registry ) {
registry.addWithKey( WebResource.CSS, "MyModule", "/css/mymodule/debug.css", WebResource.VIEWS );
}
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test() {
return "th/mymodule/debug/test";
}
}
Customizing the top menu
The default layout template builds and renders a top menu. This menu is built by publishing a DebugMenuEvent
. Any
AcrossEventHandler
- and all controllers are event handlers by default - can listen for this event and modify the menu.
The menu is built using a PathBasedMenuBuilder
, allowing the entire menu hierarchy to be specified using a path structure.
By default the menu item path is also used as the url for the menu endpoint.
All menu item urls will be processed by the debug web WebAppPathResolver and any relative path will be prefixed with the configured root path.
|
@DebugWebController
public class DebugEhcacheController
{
@Event
public void registerMenuItem( DebugMenuEvent event ) {
event.builder().item( "/test", "Test controller" );
}
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String test() {
return "th/mymodule/debug/test";
}
}
Linking to debug web controllers
The actual URL endpoint of debug web controllers depends on the application-specific configured root path.
When implementing custom debug web controllers you can ensure your relative links point to the correct controller by running them through the PrefixingPathContext
.
DebugWebModule has registered a prefixer named debugWeb, this prefixer can be used to correctly point to a debug web controller from any other controller.
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>My test debug controller</title>
</head>
<body th:fragment="content">
Relative to debug web root path:
* <a th:href="@{#webapp.path('/test')}">when rendered from a debug web controller</a>
* <a th:href="@{'@debugWeb:/test'}">always</a>
</body>
</html>
In code the DebugWeb
bean - implementation of the PrefixingPathContext
- can be used to generate the correct URL.
@DebugWebController
public class DebugEhcacheController
{
@Autowired
private DebugWeb debugWeb;
@RequestMapping(value = "/redirect", method = RequestMethod.GET)
public String test() {
return debugWeb.redirect( "/test" );
}
}