A handful of months back even though I was sat operating on a project I came across the need to have for an Html five friendly Java library. A thing that may well help my attempts at producing a tag library and to produce AJAX response messages Utilizing Html five and JSON. I knew I’ve observed some or at least a single of those libraries about just before, in truth I believe the Apache Software program Foundation has a project for this, namely Jakarta ECS.
ASF host a excellent quantity of brilliant tasks for every day use but I felt that I required Some thing a bit much more elaborate and I also wanted assistance for JSON and ultimately CSS three properties. This is exactly where I came up with jwebutils, a Java library for making Html five, CSS three and JSON markup. It has a couple of core attributes which I believed I’d mention in this post.
Html five It supports all Html five tags, wow, nicely to the degree exactly where you nonetheless have to kind in the worth in the attribute your self, so for example you may possibly do Anything like this to produce a div tag with a class attribute set to helloworld. final Div div = new Div().styleClass(“helloworld”).physique(“Hello World”);
Now calling div.toString() will print out a nice Html five div tag. Hello World
JSON Now this is an additional core function of jwebutils, it lets us use Java to generate what will be JSON markup. In order to make a JSON object all you require to do is use the JsonObject class. final JsonObject jsonObject = new JsonObject(“”);
Now lets add a member to that object. jsonObject.member(new JsonKeyValuePair(“someNumber”, six));
This will add an Integer member to the object with the name someNumber and the worth six. When printing this object out Working with its toString() strategy we’ll get some valid JSON markup. “someNumber” : 6
That may possibly not be the most exiting instance but you get the point and in the very same manner you can add any of the common varieties such as String, Float, Double, Boolean, Character and other individuals. Strings and characters are of course specific circumstances and will be handled for you, exactly where specific characters will be escaped and the complete string will be surrounded by quote indicators.
The library also has a JsonMarshaller assume it or not, it will take your object and convert it into a JsonObject, how brilliant is that, you can basically only output your POJO’s as JSON.
In order to generate a marshaller and marshall an object we will need to generate the marshaller and call the marshall() system. final MyObject myObject = new MyObject(); final JsonMarshaller jsonMarshaller = new JsonMarshaller(MyObject.class); final JsonObject jsonObject = jsonMarshaller.marshall(myObject);
Now Making use of the designed JsonObjects toString() approach 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 utilised for this was referred to as MyObject and contained these members with these values, you are going to merely have to take my word on that.
Conclusion Regrettably I’ve not got about to creating a very first release of this library but I hope to do so soon. Other brilliant functions also incorporate User agent detection and some minor CSS three help.