If you're not a fan of text templates to create HTML pages and you are looking for a simple low-level solution with AJAX support, you might appreciate this library.

 

architecture

Features

An objects of the Element class are used instead of long HTML strings in ǰava code. What makes this solution interesting? The simple API is worth mentioning. Because once we get the root element of the model, we only need to know three of its methods to build the rest of the HTML page:
  1. setAttribute() - the method assigns an attribute with a value to the current element (note: I added the name of the whole method after the suggestion in the discussion).
  2. addElement() - the method adds a new element to the object with the name of the first argument and returns its instance. The next arguments can contain a list of CSS style classes of the new element.
  3. addText() - method adds arbitrary text to the element body and handles the writing of special characters. An alternative method addRawText() is available for writing raw text (for example, for inserting JavaScript body).
Using these methods, it is possible to create virtually any HTML page with properly enclosed tags - without the user having to pay much attention to HTML header creation and other formalities. After the object model is built, the close() method is called on the root element to send the content to the PrintWriter. Since the Element class implements the Closeable interface from the standard Java library, you just need to wrap it with a try() block. What are the other benefits of the Element class?
  1. The Element class provides predefined methods (API) for inserting selected HTML elements, such as: title, paragraph, form, table and several others.
  2. The root element (HtmlElement) contains the default (miminal) HTML header content and a method for writing the body of the HTML page. The default header element is obtained by the getHead() method and the body element by the getBody() method.
  3. The internal elements of the resulting HTML code are automatically indented with spaces.
  4. Most typos are detected by the compiler, but the validity of the HTML elements used remains the responsibility of the developer.
  5. While closing internal elements is not mandatory, closing them with try-with-resources leads to a Java source code structure that resembles formatted HTML/XML.
architecture

Some other features

Let's look at other interesting features.
  1. The default implementation is an envelope over the PrintWriter class, so all texts written to Element are sent directly to the writer. The close() method then closes the open tags and empties the buffer. The advantage of such a solution is good performance and minimal memory consumption when building bulky pages.
  2. However, if we need a full memory model, it can be activated by changing the configuration when building the root element. This may make sense in cases where we are going to add some additional (backwards) elements to the model. The build of the original elements remains unchanged.
  3. The configuration can also be used to change the indentation characters for nested elements, line wrapping, setting the national page environment, character set and several other properties. Since version 2.03, support for custom formatting of data types has been added; the default implementation uses the toString() method.
  4. When the root element is built, it sets (among other things) the necessary PrintWriter properties such as the response header code or browser cache suppression.
  5. However, the Element class can also find its use outside of servlets - for example, in a REST implementation with output of type: "text/html".
  6. PrintWriter class and so all text written to Element is sent directly to the writer. The close() method then closes the open tags and empties the buffer. The advantage of such a solution is good performance and minimal memory consumption when building bulky pages.
  7. However, if we need a full memory model, it can be activated by changing the configuration when building the root element. This may make sense in cases where we are going to add some additional (backwards) elements to the model. The build of the original elements remains unchanged.
  8. The configuration can also be used to change the indentation characters for nested elements, line wrapping, setting the national page environment, character set and several other properties. Custom formatting of data types is supported; the default implementation uses the toString() method.
  9. When the root element is built, it sets (among other things) the necessary PrintWriter properties such as the response header code or browser cache suppression.
  10. However, the Element class can also find its use outside of servlets - for example, in a REST implementation with output of type: "text/html". The library offers simple AJAX support that dispenses with coding custom JavaScripts.

 

sample

Quick start:

How do you write your first HTML page based on Element object? See a body the method HttpServlet.doGet():
try (HtmlElement html = HtmlElement.niceOf(response) {
  html.addBody().addHeading("Hello, World!");
}
A more complex example demonstrates the use of a simple web form.
try (HtmlElement html = HtmlElement.niceOf(
      response, "/style.css")) {
  try (Element body = html.addBody()) {
    body.addHeading("Simple form");
      try (Element form = body
            .addForm("form-inline")) {
        form.addLabel("control-label")
            .addText("Note:");
        form.addInput("form-control", "col-lg-1")
            .setNameValue(NOTE, NOTE.of(request));
        form.addSubmitButton("btn", "btn-primary")
            .addText("Submit");
        }
    }
}

enum Attrib implements HttpParameter {
  NOTE;
  @Override public String toString() {
    return name().toLowerCase();
  }
}
See the result:

Some more sample code can be found in this Maven project.

architecture

Licence:

Source code was released under license Apache License, Version 2.0.

Copyright 2020-2022 Pavel Ponec, ujorm.org Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

 

architecture

Download:

See the source on a GitHub or use the next dependency in a Maven project for core:
<dependency>
    <groupId>org.ujorm</groupId>
    <artifactId>ujo-web</artifactId>
    <version>${ujorm.version}</version>
</dependency>
Get the latest version of the source code by the git statement:
git clone https://github.com/pponec/ujorm.git

 

architecture

Similar projects:

Here are links to some other Java projects with similar concerns:
  • A lot of interesting information can be found on this blog.: Modern Type-Safe Template Engines a's abilities (interfaces, annotations, generics etc.) to create a more powerful, extensible and object oriented system to expose component state.
  • dom4j - an open-source Java library for working with XML, XPath and XSLT.

 

architecture
Some more information can be found in external articles.:

 

architecture

Support: