Java model and renderer for search engine meta tags (e.g. alternative, canonical, description, keywords, robots).
import com.github.stefanbirkner.contarini.WebCrawlerInfo;
import com.github.stefanbirkner.contarini.render.*;
import static com.github.stefanbirkner.contarini.CommonWebCrawlerAdvice.*;
import static com.github.stefanbirkner.contarini.Alternate.*;
import static com.github.stefanbirkner.contarini.GoogleFeature.*;
import static com.github.stefanbirkner.contarini.render.VoidElementStyle.*;
…
WebCrawlerInfo info = new WebCrawlerInfo()
.withCanonical("http://your.domain/canonical")
.withDescription("dummy description")
.withKeywords("first keyword, second keyword")
.withAdvices(NO_ARCHIVE, NO_FOLLOW)
.withAlternates(
alternateLanguage("en-gb", "http://your.domain.co.uk/page")
alternateMedia("only screen and (max-width: 640px)", "http://m.your.domain.com/page"))
.disableGoogleFeatures(SITELINKS_SEARCH_BOX, TRANSLATION);
Style style = new Style().withVoidElementStyle(HTML_VOID);
WebCrawlerInfoRenderer renderer = new WebCrawlerInfoRenderer(style);
StringWriter html = new StringWriter();
renderer.write(info, html);
<link rel="canonical" href="http://your.domain/canonical">
<meta name="description" content="dummy description">
<meta name="keywords" content="first keyword, second keyword">
<meta name="robots" content="noarchive, nofollow">
<link rel="alternate" hreflang="en-gb" href="http://your.domain.co.uk/page">
<link rel="alternate" media="only screen and (max-width: 640px)" href="http://m.your.domain.com/page">
<meta name="google" content="nositelinkssearchbox">
<meta name="google" content="notranslate">
<dependency> <groupId>com.github.stefanbirkner</groupId> <artifactId>contarini</artifactId> <version>1.2.0</version> </dependency>
WebCrawlerInfo info = new WebCrawlerInfo().withCanonical("myCanonical.html");
new WebCrawlerInfoRenderer().writeMetaTagsForInfoToWriter(info, writer);
There is a JSP tag that renders the WebCrawlerInfo
. (It
uses the WebCrawlerInfoRenderer
internally.) Just add another
dependency to your project.
<dependency> <groupId>com.github.stefanbirkner</groupId> <artifactId>contarini-jsp</artifactId> <version>0.1.0</version> </dependency>
Now you can use the tag to render the info within the JSP.
<contarini:metaTags info="${info}"/>