Skip to content

Using JSP as a templating language for GWT

I wanted to share a solution to a problem a client of mine had–needing the same (or very similar) content in two places–in a lightbox, managed by GWT, and in an HTML page.  The content itself had GWT widgets inside it (that would need to be functional in both places).

The solution was to expose the HTML content as a JSONP file for GWT consumption (and just have the HTML displayed for the HTML page).  To do this,

  • we build the HTML page (in this case, using JSTL and JSP, but any other HTML view technology would work).
  • then we build code that retrieves the page (I used HttpClient)
  • and encodes the entire HTML content as an attribute of a json object (I used json-simple)
  • and exposes it to the world via a URL

Now that we have the content as JSON encoded HTML, we need to consume it:

  • first, create a JSONP consumer, as outlined here
  • then, when you get the content, pull it into an HTMLPanel, not just a HTML widget
  • create new instances of your GWT widgets and attach them to your known span ids using the HTMLPanel.add() method.
  • if your content depends on external css files, you’ll need to add and remove them via JSNI when the lightbox is shown (if you can’t integrate the CSS into the existing site)
  • if your lightbox and html content differ slightly, you can hide or show spans via your server side presentation layer to configure your in lightbox widgets

Benefits

  • One location for display–one place to make changes
  • No layout in GWT–everything happens in HTML
  • Cross domain–since the content is exposed via JSONP, it can be consumed by javascript on any domain.

Detriments

  • Security–you’re exposing yourself to some possible security issues, though since you control both the endpoints, they shouldn’t be too bad.  Read this excellent document carefully to understand the implications.
  • Complexity–lots of moving pieces
  • Caching–JSONP calls are cached by the browser, and a control refresh doesn’t seem to refresh them.  I end up clearing my cache a lot when debugging.
  • Two calls to the server for every lightbox shown.
  • Not sure if it would work in developer mode–possibly with an external server.  Haven’t tried this.

I haven’t played around with any of the GWT templating solutions, but I don’t believe they would work in this case, because of the need for a plain, crawlable HTML page.

Two books of interest to freelancers

I recently read two finance books that might be of interest to freelancers.

The first is The Money Book For Freelancers, Part-Timers, and the Self-Employed by Joseph D’Agnese and Denise Kiernan.  I was pleasantly surprised to see a finance book aimed directly at people with sporadic income.  This book is written in an easy going style, with examples drawn from the author’s lives, as well as those of their friends.

The major takeaway is that you should pay yourself first–when a check comes in, allocate a percentage for taxes, a percentage for retirement, and a percentage for an emergency fund.  Do that first, so you aren’t even tempted to spend it.  I do this because of my corporate structure and SIMPLE IRA, but if you’re a new freelancer, or someone who is a schedule C worker, then it’s great advice.

Then, after a while, when you’re comfortable with the system, you can start to save for other goals.  One of the strongest parts of the book was when they encouraged you to sit down and write out your particular financial goals.  It’s tough to do, because when you do so, then you’re confronted with the hard work of trying to achieve them, but it’s great advice.

While this book was full of tips for dealing with the income side, I felt like the outgo side was covered as well.  They had advice about breaking up your expenses into required (taxes, retirement, emergency), overhead (rent/mortgage, food, insurance), and variable (eating out, classes).  Once you know your overhead and required expenses, if you get a windfall (that big client finally paid!) you can siphon that off into a separate account and use that for future months of expenses.  The authors also did a good job talking nuts and bolts, going down to the details of what type of bank accounts you need.

They didn’t really cover some things that are on the periphery of financial concerns–corporate structure, handling employees and contractors and types of insurance, for instance.  But doing so would probably have distracted from the main point of the book, so I understand.

If you’re a freelancer, and this strategy doesn’t work for you, you probably want to check this book out.

I also read Are you a Stock or a Bond by Moshe A. Milevsky.  This book is a fascinating look at the best way for modern savers with 401(k)s and IRAs to replicate what the WWII generation had–a pension.

The hook of the book is that savers should consider their lifetime earning potential and job prospects when planning a retirement asset allocation strategy.  Those who are in a more stable job (the author uses himself as an example–a tenured professor) should consider their earnings more bond-like and thus invest more in stocks (to the point of borrowing to buy stocks).  Those in a more tenuous job (farmer? I don’t remember his example) should do the opposite.

The hook, while thought provoking, is not really the focus of the book.  In fact, I was a bit disappointed that he didn’t spend more time telling you how to determine if your income stream was more stock like or more bond like.  Instead, he charges off into what you should do with your accumulated savings when you retire.

The short answer–buy an annuity.  The reason for this is that the guaranteed income from an annuity is impossible to replicate from the same amount of capital invested individually.  Why?  Because an annuity spreads payments across a population in which some will die before others.  Annuities essentially transfer wealth from those who die early to those who die later.  This asset class reduces your greatest financial risk at retirement–running out of money.  Just like a pension.

Definitely a thought provoking book, and one that would be especially useful to people approaching retirement.