WebResourceRegistry
AcrossWebModule allows for programatically registering web resources through the use of a WebResourceRegistry
.
A new WebResourceRegistry
is attached to every request and can be accessed as a request attribute or a handler method parameter.
Resources can be added in a group (eg: css, javascript) and with a specific location (eg: relative, views, external).
The groups can be used for retrieval of related resources, the location determines the WebResourceTranslator
that will be used.
Web resource translating happens before the actual view is rendered and allows for the data to be converted into a more appropriate format for the view rendering.
Using the WebResource.VIEWS
location for a web resource will have the relative path prefixed with the configured across web views resources prefix.
Resources can be identified with a unique key, ensuring they are added only once.
@Controller
public class MyController
{
@ModelAttribute
public void registerWebResources( WebResourceRegistry webResourceRegistry ) {
webResourceRegistry.add( WebResource.CSS, "//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css", WebResource.EXTERNAL );
webResourceRegistry.add( WebResource.CSS, "/static/mymodule/css/main.css", WebResource.VIEWS );
}
}
Views can access the WebResourceRegistry under the webResourceRegistry attribute.
It is however the view implementation that will determine how resources are handled: for example the Thymeleaf template rendering them in the head section of a page.
|
WebResourcePackage
To avoid adding single items, web resources can be bundled into a WebResourcePackage
and registered with the WebResourcePackageManager
.
Adding an entire package can then be done by calling WebResourceRegistry#addPackage
with the right package name.
@Component
public class BootstrapUiWebResources extends SimpleWebResourcePackage
{
public static final String VERSION = "3.3.5";
public static final String NAME = "bootstrap";
public BootstrapUiWebResources() {
setDependencies( JQueryWebResources.NAME ); // Install the jquery package first
setWebResources(
new WebResource( WebResource.CSS, NAME,
"//maxcdn.bootstrapcdn.com/bootstrap/" + VERSION + "/css/bootstrap.min.css",
WebResource.EXTERNAL ),
new WebResource( WebResource.JAVASCRIPT_PAGE_END, NAME,
"//maxcdn.bootstrapcdn.com/bootstrap/" + VERSION + "/js/bootstrap.min.js",
WebResource.EXTERNAL )
);
}
@Autowired
public void registerPackage( WebResourcePackageManager packageManager ) {
packageManager.register( NAME, this );
}
}