Over a million developers have joined DZone.

A jQuery Inspired Server Side View Model for Java

· Java Zone

Learn more about the advantages of moving from a monolithic to microservices architecture.  Brought to you in partnership with IBM.

In HTML applications, jQuery has changed the way people thing about view rendering. Instead of an input or a text field in the view pulling data into it, the jQuery code pushes data into the view. How could this look in a server side situation like Java?

In this code example, I read an HTML template file from the classpath, set the value of an input field and append more data based on a template (also in the HTML file).

public class ContactServlet extends HttpServlet {
 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String nameQuery = req.getParameter("nameQuery");
 
        // Read a pure html template from the classpath
        Xhtml document = Xhtml.fromResource(getClass().getResource("html/contact/index.html"));
 
        // Set the value of the nameQuery input field in the HTML
        document.getForm("#findForm").set("nameQuery", nameQuery);
 
        Element contactsEl = document.select("#contacts");
        Element contactTmpl = contactsEl.take(".contact");
        for (Contact contact : findContacts(nameQuery)) {
            // Fill in the .contact template of the HTML with data
            Element contactHtml = contactTmpl.copy().text(contact.print());
            // And add it to the #contacts element
            contactsEl.add(contactHtml);
        }
 
        document.write(resp.getWriter());
        resp.setContentType("text/html");
    }
}

This is a simplified version of the HTML:

<html>
  <form method='get' id="findForm">
	<input type='text' name='nameQuery' value='null' />
    <input type='submit' name='findContact' value="Find contact" />
  </form>
  <ul id='contacts'>
    <li class='contact'>Johannes (4444444)</li>
  </ul>
</html>

This is a third way from the alternatives of templated views like Velocity and JSP and from component models like JSF. In this model, the view, the model and the binding of the model variables to the view are all separate.

Disclaimer: In this example, I’ve used my still in pre-alpha XML library with the working name of Eaxy. You can get similiar results with libraries like jSoup and JOOX.

Caveat: I’ve never tried this on a grand scale. It’s an idea that compels me for three reasons: First, it’s very explicit. Nothing happens through @annotation, conventions or some special syntax in a template. Second, it’s very unit testable. There is nothing tying this code to running in a web application server. Finally, it’s easy to get to this code through incremental steps. I initially wrote the example application with code that embedded the HTML as strings in Java code and refactored to use the Java Query approach.

Could this approach be worth trying out more?

From Idea to Application gives you the architecture to quickly build, manage and run a range of applications (web, mobile, big data, new smart devices, etc.) on an open-standard, cloud-based platform. See why developers are using IBM Bluemix. Brought to you in partnership with IBM.

Topics:

Published at DZone with permission of Johannes Brodwall, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}