Coder Social home page Coder Social logo

htmljgen's Introduction

htmlJgen --- HTML generation from plain Java

Create a simple DOM tree for HTML with Java

Also provides a way to handle servlet GET and POST parameters.

See the current javadoc for more information.

Test coverage reporting is also available.

Why that?

I was always wondering what the point is with JSP? How it came about is easy to understand. It is a way to have dynamic content inside a static HTML page. To avoid the CPU cost of interpreting a template language, JSP is compiled into Java classes.

But the HTML/JSP I work with contains 95% or more JSP tags, sprinkled with the odd original <div> here and there. In the worst case there are scriptlets of pure Java contained, but it has nothing to do anymore with "mostly HTML".

So it is all generated HTML anyway. It is programmed! But JSP is a terribly designed "programming language" mixing aspects of HTML, XML, Java scriptlets, expression language and the JSP itself. What a hodge-podge.

Rather use htmlJgen

For programming, we should use a decent programming language, properly defined syntax, strongly typed, reusing code used in different places by method calls, clean code and all that: Java.

And of course we do not want to write sequences of println(...) calls. htmlJgen is my attempt of a very simple class library to create an HTML structure programmatically and send it nicely formatted to the browser. It can be used as part of the servlet to create the HTML.

You create your page template at some central place, for example, like

final HtmlPage pageTemplate() {
  HtmlPage page = new HtmlPage("my cool app");
  page.addJs("/static/main.js");
  page.addCss("/static/main.css");
  page.addLink("image/png", "/static/favicon.png", "icon");
  ...
  return page;
}

Later on you add content to the page, typically generators for div elements:

page.addContent(renderTable(results));

where renderTable(List<Data> results) creates, for example, a table from result Data objects that were retrieved as part of the servlet's operation:

Html renderTable(List<Data> results) {
   Html div = new Html("div").setAttr("class", "resulttable");
   Html table = div.add("table");
   for(Data d : results) {
      table.add(renderResultRow(d));
   }
   return table;
}

Finally you send the data out with a method somehow accessible by most or all of your servlets, resembling:

public void sendPage(HttpServletResponse resp, HtmlPage page) {    
  resp.setContentType("text/html");    
  resp.setCharacterEncoding("UTF-8");

  try {
    Writer w = resp.getWriter();
    page.print(w);
  } catch (IOException e) {
    LOG.error("could not write response body", e);
  }
}

Where is the HTML structure?

You cannot read the HTML structure anymore? Well, me neither. But could you see it still in the last JSP you looked at?

Why are there no individual classes for each HTML element?

This would be nice indeed: encode all the rules of HTML into a class structure such that it is impossible to put a <div> into a <span> or add a selected attribute to a <p> element.

But it is a daring task to get all of this complete and correct, possibly catering for different HTML4, 4.01, 5 versions out there. Feel free to prepare a pull request.-)

I even retracted the EmptyElem.setClass(String classes) method again despite the fact that setAttr("class", "yada") is the most frequently used call. But once you start with these convenience methods it is hard to stop. And any stop would be arbitrary, so I kept it all clean and simple for now.

htmljgen's People

Contributors

haraldki avatar

Watchers

 avatar  avatar

htmljgen's Issues

Add UrlParam.fromAll() method

This method should convert not just one parameter of the respective name but all and return a list of them, very much like UrlManager.addAll(). Need to check whether UrlManager is really helpful and possibly deprecate and remove it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.