public class Sitemap extends org.jooby.internal.sitemap.JSitemap<Sitemap>
Generate sitemap.xml files using jsitemapgenerator.
{
use(new Sitemap());
get("/page1", ..);
get("/page2", ..);
}
The module exports a /sitemap.xml route.
The sitemap.xml specification requires an absolute url. The way we provide this
absolute url is at creation time or using the sitemap.url property:
{
use(new Sitemap("https://foo.bar"));
get("/page1", ..);
get("/page2", ..);
}
or
sitemap.url = "http://foo.bar"
The sitemap generator builds a sitemap.xml file with loc elements. You
can customize the output in one of two ways:
{
get("/")
.get("/page1", ..)
.get("/page2", ..)
.attr("changefreq", "weekly")
.attr("priority", "1");
}
We first group route under a common path: / and add some routers. Then for each
router we set the changefrequency and priority.
{
use(new Sitemap().with(r -> {
WebPage page = new WebPage();
page.setName(r.pattern());
page.setChangeFreq(ChangeFreq.ALWAYS);
page.setPriority(1);
return Arrays.asList(page);
}));
get("/")
.get("/page1", ..)
.get("/page2", ..);
}
Here we built WebPage objects and set frequency and priority.
Suppose you have a product route dynamically mapped as:
{
get("/products/:sku", ...);
}
Dynamic urls are supported via custom WebPageProvider:
{
use(new Sitemap().with(SKUPageProvider.class));
get("/products/:sku", ...);
}
SKUPageProvider.java:
public class SKUPageProvider implements WebPageProvider {
private MyDatabase db;
@Inject
public SKUPageProvider(MyDatabase db) {
this.db = db;
}
public List<WebPage> apply(Route.Definition route) {
if (route.pattern().startsWith("/products")) {
// multiple urls
return db.findSKUS().stream().map(sku -> {
WebPage webpage = new WebPage();
webpage.setName(route.reverse(sku));
return webpage;
}).collect(Collectors.toList());
}
// single url
WebPage webpage = new WebPage();
webpage.setName(route.pattern());
return Arrays.asList(webpage);
}
}
The filter(java.util.function.Predicate) option allows to excludes routes from final
output:
{
use(new Sitemap().filter(route -> !route.pattern().startsWith("/api")));
}
| Constructor and Description |
|---|
Sitemap()
Creates a new
Sitemap. |
Sitemap(String baseurl)
Creates a new
Sitemap and set the base url. |
| Modifier and Type | Method and Description |
|---|---|
Sitemap |
filter(Predicate<Route.Definition> filter)
filter
|
Sitemap |
with(Class<? extends WebPageProvider> wpp)
Set a custom
WebPageProvider. |
Sitemap |
with(WebPageProvider wpp)
Set a custom
WebPageProvider. |
configureequals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitconfigpublic Sitemap()
Sitemap.public Sitemap filter(Predicate<Route.Definition> filter)
The filter(java.util.function.Predicate) option allows to excludes routes from final
output:
{
use(new Sitemap().filter(route -> !route.pattern().startsWith("/api")));
}
filter in class org.jooby.internal.sitemap.JSitemap<Sitemap>public Sitemap with(WebPageProvider wpp)
WebPageProvider.
{
use(new Sitemap().with(r -> {
WebPage page = new WebPage();
page.setName(r.pattern());
page.setChangeFreq(ChangeFreq.ALWAYS);
page.setPriority(1);
return Arrays.asList(page);
}));
}
with in class org.jooby.internal.sitemap.JSitemap<Sitemap>wpp - A web page provider.public Sitemap with(Class<? extends WebPageProvider> wpp)
WebPageProvider.
{
use(new Sitemap().with(MyWebPageProvider.class));
}
The MyWebPageProvider will be created and injected by Guice.with in class org.jooby.internal.sitemap.JSitemap<Sitemap>wpp - A web page provider.Copyright © 2020. All rights reserved.