public static class ServerPagesAppBundle.AppBuilder
extends java.lang.Object
| Constructor and Description |
|---|
AppBuilder(boolean mainContext,
java.lang.String name,
java.lang.String path,
java.lang.String uri,
java.lang.ClassLoader loader) |
| Modifier and Type | Method and Description |
|---|---|
ServerPagesAppBundle.AppBuilder |
attachAssets(java.lang.String path)
Add additional assets location.
|
ServerPagesAppBundle.AppBuilder |
attachAssets(java.lang.String subUrl,
java.lang.String path)
Essentially the same as
attachAssets(String), but attach classpath assets to application
sub url. |
ServerPagesAppBundle.AppBuilder |
attachWebjars()
Shortcut for
#attachAssets("META-INF/resources/webjars/")). |
ServerPagesAppBundle |
build() |
ServerPagesAppBundle.AppBuilder |
errorPage(int code,
java.lang.String path)
Show special page instead of response with specified status code.
|
ServerPagesAppBundle.AppBuilder |
errorPage(java.lang.String path)
Default error page (shown in case of exceptions and for all error return codes (>=400)).
|
ServerPagesAppBundle.AppBuilder |
filePattern(java.lang.String regex)
Differentiation of template call from static resource is based on fact: static resources
have extensions.
|
ServerPagesAppBundle.AppBuilder |
indexPage(java.lang.String name)
Declares index page (served for "/" calls).
|
ServerPagesAppBundle.AppBuilder |
mapViews(java.lang.String prefix)
Specify default mapping prefix for views rest resources.
|
ServerPagesAppBundle.AppBuilder |
mapViews(java.lang.String subUrl,
java.lang.String prefix)
Map view rest to sub url, using different prefix than default.
|
ServerPagesAppBundle.AppBuilder |
requireRenderers(java.lang.String... names)
Specifies required template types (view renderes) for application.
|
ServerPagesAppBundle.AppBuilder |
spaRouting()
Shortcut for
spaRouting(String) with default regexp. |
ServerPagesAppBundle.AppBuilder |
spaRouting(java.lang.String noRedirectRegex)
Enable single page application html5 routing support.
|
ServerPagesAppBundle.AppBuilder |
viewsConfigurationModifier(java.lang.String name,
ViewRendererConfigurationModifier modifier)
Dropwizard views configuration modification.
|
public AppBuilder(boolean mainContext,
java.lang.String name,
java.lang.String path,
java.lang.String uri,
java.lang.ClassLoader loader)
public ServerPagesAppBundle.AppBuilder mapViews(java.lang.String prefix)
For example, if you specify "com.mycompany/app1" as prefix then all registered rest resources, starting from this path will be mapped to gsp application (could be called relatively to application root).
Only one prefix may be declared: error will be thrown on duplicate declaration.
prefix - rest prefix to map as root viewsfor mapping other rest views on sub urlspublic ServerPagesAppBundle.AppBuilder mapViews(java.lang.String subUrl, java.lang.String prefix)
Only one prefix may be applied to one url (error will be thrown on duplicate registration)! But other mappings are possible for larger sub url (partially overlapping).
Additional views could also be mapped through extension registration:
ServerPagesBundle.extendApp(String).
Pay attention that additional asset locations may be required (attachAssets(String, String),
because only templates relative to view class will be correctly resolved, but direct templates may fail
to resolve.
subUrl - sub url to map views toprefix - rest prefix to map as root viewspublic ServerPagesAppBundle.AppBuilder requireRenderers(java.lang.String... names)
ServerPagesBundle.builder()).
Without declaring required renderer, application will simply serve template files "as is" when no appropriate renderer found (because template file will not be recognized as template).
Renderer name is a renderer configuration key, defined in ViewRenderer.getConfigurationKey().
names - required renderer namespublic ServerPagesAppBundle.AppBuilder spaRouting()
spaRouting(String) with default regexp.public ServerPagesAppBundle.AppBuilder spaRouting(java.lang.String noRedirectRegex)
noRedirectRegex - regex to match all cases when redirection not neededfor more info how it works,
for more info about regexppublic ServerPagesAppBundle.AppBuilder indexPage(java.lang.String name)
Pay attention that index is not set by default to "index.html" because most likely it would be some template handled with rest resource (and so it would be too often necessary to override default).
name - index file name (by default "")public ServerPagesAppBundle.AppBuilder errorPage(java.lang.String path)
path - either path to static resource (inside registered classpath path) or resource url
(without app name prefix)for registereing error page on exact return codepublic ServerPagesAppBundle.AppBuilder errorPage(int code, java.lang.String path)
Error pages should use ErrorTemplateView as (base) model
class in order to get access to context exception. It is not required, if error object itself not required
during rendering.
NOTE that error page is returned only if original request accept html response and otherwise no error page will be shown. Intention here is to show human readable errors only for humans.
IMPORTANT: GSP errors mechanism override ExceptionMapper and dropwizard-view ErrorEntityWriter mechanisms
because exception is detected before them and request is redirected to error page. Both ExceptionMapper
and EntityWriter would be called, but their result will be ignored (still, ExceptionMapper is useful
for errors logging). This was done to avoid influence of global ExceptionMapper's to be sure custom
error page used. It is possible to ignore GSP error mechanism for exact rest methods by using
ManualErrorHandling annotation.
code - error code to map page ontopath - either path to static resource (inside registered classpath path) or resource url
(without app name prefix)for global errors pagepublic ServerPagesAppBundle.AppBuilder attachAssets(java.lang.String path)
It is the same as separate extension registration with ServerPagesBundle.extendApp(String).
NOTE: extended paths are used in priority so some file exists on the same path, extended path will "override" primary location.
path - assets classpath path (may be in form of package (dot-separated))to register assets on specific sub urlpublic ServerPagesAppBundle.AppBuilder attachAssets(java.lang.String subUrl, java.lang.String path)
attachAssets(String), but attach classpath assets to application
sub url. As with root assets, multiple packages could be attached to url. Registration order is important:
in case if multiple packages contains the same file, file from the latest registered package will be used.
Example usage: suppose you need to serve fonts from some 3rd party jar, so you need to map this package
to sub url with attachAssetsForUrl("/fonts/", "com.some.package.with.fonts"). After that
all assets from registered package will be accessible by this url (e.g /fonts/myfont.ttf).
subUrl - sub url to serve assets frompath - assets classpath paths (may be in form of package (dot-separated))public ServerPagesAppBundle.AppBuilder attachWebjars()
#attachAssets("META-INF/resources/webjars/")).
Useful if you want to use resources from webjars. All webjars package resources under the same path
(e.g. META-INF/resources/webjars/jquery/3.4.1/dist/jquery.min.js), so after enabling webjars support
you can reference any resource from webjar (in classpath) (e.g. as
<script src="jquery/3.4.1/dist/jquery.min.js">).public ServerPagesAppBundle.AppBuilder filePattern(java.lang.String regex)
Pattern must return detected file name as first matched group (so direct template could be detected). Pattern is searched (find) inside path, not matched (so simple patterns will also work).
regex - regex for file request detection and file name extractiondefault patternpublic ServerPagesAppBundle.AppBuilder viewsConfigurationModifier(java.lang.String name, ViewRendererConfigurationModifier modifier)
ServerPagesBundle.ViewsBuilder#viewsConfiguration(io.dropwizard.views.ViewConfigurable)). But its
often required to "tune" template engine specifically for application. This method allows global views
configuration modification for exact server pages application.
The main use case is configuration of the exact template engine. For example, in case of freemarker this could be used to apply auto includes:
.viewsConfigurationModifier("freemarker", config -> config
// expose master template
.put("auto_include", "/com/my/app/ui/master.ftl"))
Note that configuration object is still global (because dropwizard views support is global) and so
multiple server pages applications could modify configuration. For example, if multiple applications will
declare auto includes (example above) then only one include will be actually used. Use
ServerPagesBundle.ViewsBuilder#printViewsConfiguration() to see the final view configuration.
name - renderer name (e.g. freemarker, mustache, etc.)modifier - modification callbackpublic ServerPagesAppBundle build()