Jwebutils Html 5, Css 3 &Amp; Json Using Java From Wireweb Web Design}

September 24, 2017 0 Comments

jwebutils – HTML 5, CSS 3 & JSON using Java from Wireweb web design

by

Wireweb

A few months back while I was sat working on a project I came across the need for an HTML 5 friendly Java library. Something that could aid my attempts at creating a tag library and to generate AJAX response messages using HTML 5 and JSON. I knew I’ve seen some or at least one of these libraries around before, in fact I think the Apache Software Foundation has a project for this, namely Jakarta ECS.

ASF host a great number of brilliant projects for daily use but I felt that I needed something a bit more elaborate and I also wanted support for JSON and eventually CSS 3 properties. This is where I came up with jwebutils, a Java library for creating HTML 5, CSS 3 and JSON markup. It has a few core capabilities which I thought I’d mention in this article.

HTML 5

It supports all HTML 5 tags, wow, well to the degree where you still have to type in the value in the attribute your self, so for instance you could do something like this to create a div tag with a class attribute set to helloworld.

final Div div = new Div().styleClass(“helloworld”).body(“Hello World”);

Now calling div.toString() will print out a nice HTML 5 div tag.

Hello World

JSON

Now this is another core feature of jwebutils, it lets us use Java to create what will be JSON markup. In order to create a JSON object all you need to do is use the JsonObject class.

[youtube]http://www.youtube.com/watch?v=cOx_Zx95hxM[/youtube]

final JsonObject jsonObject = new JsonObject(“”);

Now lets add a member to that object.

jsonObject.member(new JsonKeyValuePair(“someNumber”, 6));

This will add an Integer member to the object with the name someNumber and the value 6. When printing this object out using its toString() method we’ll get some valid JSON markup.

{“someNumber” : 6}

That might not be the most exiting example but you get the point and in the same manner you can add any of the standard types such as String, Float, Double, Boolean, Character and others. Strings and characters are of course special cases and will be handled for you, where certain characters will be escaped and the whole string will be surrounded by quote signs.

The library also has a JsonMarshaller believe it or not, it will take your object and convert it into a JsonObject, how brilliant is that, you can simply just output your POJO’s as JSON.

In order to create a marshaller and marshall an object we need to create the marshaller and call the marshall() method.

final MyObject myObject = new MyObject();

final JsonMarshaller jsonMarshaller = new JsonMarshaller(MyObject.class);

final JsonObject jsonObject = jsonMarshaller.marshall(myObject);

Now using the created JsonObjects toString() method will get us a nice JSON object.

{

“string” : “string”,

“integerPrimitive” : 6,

“longPrimitive” : 7,

“shortPrimitive” : 12,

“bytePrimitive” : 127,

“booleanPrimitive” : true,

“floatPrimitive” : 14.23,

“doublePrimitive” : 3.141592653589793,

“characterPrimitive” : “A”

}

Of course the object I used for this was called MyObject and contained those members with those values, you’ll just have to take my word on that.

Conclusion

Unfortunately I’ve not got around to building a first release of this library but I hope to do so soon. Other brilliant features also include User agent detection and some minor CSS 3 support.

Wireweb Warrington Web Design, Cheshire and the surrounding areas. We offer quality, professional websites, personal service, you speak directly with the designer to discuss your requirements. Currently offering Website Starter Package for 500, inc 5 page design, hosting, email addresses, .co.uk domain and SEO. Please enquire to see how we can help your company get your internet advertising started.Visit us at www.wireweb.co.uk or call us on 01925 815034.

Article Source:

jwebutils – HTML 5, CSS 3 & JSON using Java from Wireweb web design
}