Whyser
Whyser

Reputation: 2247

HTML-components wrapped as Java objects?

I'm building a website for fun (using Servlets and JSP pages (Java)) and I just simply hate the whole process of writing html and then squeezing in the data retrieved from the server inside each html document. And not to mention looping stuff out, inside the html document. I just think the whole process is flawed and I'm sure there are better ways. I know there are templates that can be used to simplify stuff like looping through stuff, but still...

By html-component I mean something like what is described in this article: http://coding.smashingmagazine.com/2012/10/23/road-reusable-html-components/

So what I wanna do is wrap an html-component as a java object. Super basic example:

public class BaseHtmlObject
{
 private String m_Content = null;
 public BaseHtmlObject(JSONObject someData)
 {
  m_Content = someData.getString("content");
  ...etc
 }
 public String toHtml()
 {
  return "<div>"+m_Content+"</div">
 }
 public addStyle(String attr, String value)
 {
  //...
 }
 ...
}

What I wonder is, is there any places/"marketplaces" with pre-made html-objects like this, that can be reused? If not, why don't they exist? Also if this is called something special in the world of web programming please let me know so I can search for it myself.

I do understand this approach can be hard to debug, but if you debug each object/component individually it shouldn't be so hard and this OOP approach seems so much easier to manage.

Upvotes: 0

Views: 63

Answers (1)

JB Nizet
JB Nizet

Reputation: 691933

There are many web frameworks that have a component-based approach: Wicket, JSF, Tapestry, GWT, and probably others. Look at them, and please don't invent yet another one :-)

Upvotes: 1

Related Questions