Rafał Wrzeszcz - Wrzasq.pl

ChillDev-Web - web page model library for Java

Thursday, 12 June 2014, 20:49

Last time I'm trying to migrate more and more towards technologies different than PHP, which I always found primitive and messed-up, but was skilled enought to do some fancy stuff with it. Even though I often play with different technologies and languages, I always liked Java and I'm moving more and more into it with my projects. For me, especially after years of PHP development, the biggest disadvantage is lack of many presentation-related features in JSP layer and MVC frameworks. I've decided to spend sime time trying my skills and reminding myself a bit of Java with creating library for JSP presentation technology that would help managing page meta-data. Basically is an attempt to port ChillDevViewHelpersBundle, my Symfony2 bundle, for Java/Spring.


Before you use it, I need to warn you! Although I consider myself an experienced developer, I didn't develop much in Java. I believe there are some parts in the library, that are implemented wrong way. Feel free to review the code and publish your patches. Use with caution and expect further progress.


You can install the library from Maven Central Repository - it is divided into sub-packages to allow more granular dependency management:

core model classes with just a logic representation of page meta data;
JSP tags library;
Spring Framework integration, which allows to pre-define basic model data in application configuration and resolves page meta model with Spring beans which guarantees to expose model set by controllers and other layers in JSP views.

JSP tags

First of all, this package is a JSP taglib, so let's start from web-tags package. It provides set of tags that allows you to build you page header model prior to printing it. To use this taglib you need to import http://chilldev.pl/java/jsp/web namespace.

The <xmlns> element allows you to aggregate XML namespaces:

<xmlns namespace="http://ogp.me/ns#" alias="og"/>

<link>, <stylesheet> and <script> elements allow you to define references to external resources (<stylesheet> is just a shorthand notation for <link> to a stylesheet):

<stylesheet href="/normalize.css"/>
<script src="/prototype.js"/>

Note: The notation above is recommended for HTML5 documents, where type attribute has default value.

You can also manage your meta-info with <meta-name>, <meta-http-equiv> and <meta-property> elements. They all produce (X)HTML <meta> elements, just with different key attribute:

<metaHttpEquiv key="Pragma" value="no-cache"/>
<metaName key="description" value="Your page description"/>
<metaProperty key="og:title" value="Display title"/>

At last, but not least, you can manage your page title with <titleAdd> element:

<titleAdd part="Administrator panel"/>


Ok, we know how to define model elements with tags, but what to do with them? Another set of tags is responsible for dumping container model data to output:

<%@ taglib prefix="web" uri="http://chilldev.pl/java/jsp/web" %>

Spring integration

But usually you have much more than a view. Entire logic, that lays somewhere in our code. If you develop with Spring Framework you can use web-spring which integrates this library with Spring beans container.


With this package, you can use your application context configuration to pre-define some core parts of your page model. In particular, you can specify markup format which should be generated (which is XHTML by default, not HTML!). Basically, there is a corresponding element for every aspect of page model, that can be configured by JSP tags). To configure page model via Spring configuration you need to import http://chilldev.pl/java/spring/web namespace.

The main element, which you need to define is <handle-page-model> - it registers Spring-based page model resolver and sets up generator into specified markup mode (without this tag all other won't make any effect, page model will be resolved inregardless of Spring beans container):

<handle-page-model xhtml="ture"/>

After that, you can define all the other stuff:

<xmlns namespace="http://www.w3.org/1999/xhtml" alias=""/>
<xmlns namespace="http://ogp.me/ns#" alias="og"/>
<xmlns namespace="http://ogp.me/ns/fb#" alias="fb"/>
<title separator=" :: ">
    <part>My page</part>
<meta-name key="author">Me</meta-name>
<script src="/prototype.js" flow="ASYNC"/>
<link href="/blog.rss" type="application/rss+xml">
<stylesheet href="/normalize.css" media="print"/>

Working with model

But this Spring integration package gives you more - the page model is registred as a bean and it's scope is set to request, so it means you can use it in your logic (you can acquire page model from bean container) and you can be sure that you will always operate on the model that is related to your current request context.


This is MIT-licensed open-source project published on GitHub. You don't need to ask if you can, just do all the awesome things you want!

Tags: XHTML, Web, Spring, SEO, JSP, Tutorial, ChillDev, Java