Using jQuery, the jQuery Form plugin and Stripes to create pretty Javascript and Java

2

I recently discovered jQuery, it a blazingly
fast Javascript-framework and has a very compact notation. Now some
guy made a plugin to submit plain html forms with Ajax and jQuery.
Just add some Javascript that says you want to submit the form via
Ajax and you don’t have to touch the html of the form. What’s wrong
with touching the html of the form I hear you think. In most web
applications the forms are generated with tag libraries (Struts,
Spring, Stripes) or a templating library (like Velocity) and it can
get a bit difficult to add Javascript properties to your form. Today
I also discovered that you can return Java objects as Javascript with
Stripes.

Most of you probably never heard of
Stripes so I will give you a short introduction and then you’ll learn
to Ajajas (Asynchronous Javascript And Java And Stripes ;) )

The code examples can be downloaded here

....

The title says pretty Javascript and
Java. A problem of the recent Ajax developments is that there are no
real standards/conventions and some frameworks are lagging behind.
The result of all this is ugly code. The project I’m currently
working on is developed with Spring and Dojo. Dojo uses JSON as a
communcation ‘language’, but Spring doesn’t understand this language.
So we included a JSON library for Java and are converting query
results to Java JSON objects in the service layer. This is quite bad,
you don’t want Spring to know that you’re using JSON at the client
side, you just want to pass a bunch of domain objects to your view.
This is were we can benefit from Stripes.

A jump start with Stripes

Stripes is a very nice framework which
has quite a lot nice features. The guys that made it took a good look
at Struts, Spring MVC, Web Work 2 and came up with something very
nice. Today we’re only using the form tags, Action Beans and minimal
configuration. If you want to know more about Stripes visit their
wiki
.
It’s quite good and all the basic stuff is in there.

At AMIS we use Maven2 for all our web
projects, so this project is made with Maven2 too (but if you want to
try it without Maven and guess which jars to include you can download
Stripes from this location.

Include the following dependency in
your pom.xml:

&lt;dependency&gt; <br />&nbsp;&nbsp;&nbsp; &lt;groupId&gt;net.sourceforge.stripes&lt;/groupId&gt; <br />&nbsp;&nbsp;&nbsp; &lt;artifactId&gt;stripes&lt;/artifactId&gt; <br />&nbsp;&nbsp;&nbsp; &lt;version&gt;1.4.1&lt;/version&gt; <br />&lt;/dependency&gt; <br />

(I know that there is a version 1.4.2,
but it isn’t on Ibiblio yet and 1.4.1 works fine too)

Now go to the getting started page of
Stripes

and copy the contents of web.xml, log4j.properties and
StripesResources.properties to your local project. The web.xml should
be in src/webapp/WEB-INF/ and both log4.properties and
StripesResources.properties are located in src/resources/

This is all the configuration you need
for Stripes. We’re making an application in which we can enter an
Employee id into a search box and submit that search box via Ajax and
display the search results under the search box.

First we need an Employee object:

package nl.amis.domain;

<code>
<br />
<font color="#7f0055"><strong>public&nbsp;class&nbsp;</strong></font><font color="#000000">Employee&nbsp;</font><font color="#000000">{</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>private&nbsp;</strong></font><font color="#7f0055"><strong>int&nbsp;</strong></font><font color="#000000">id;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>private&nbsp;</strong></font><font color="#000000">String&nbsp;name;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>private&nbsp;</strong></font><font color="#000000">String&nbsp;functionName;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>private&nbsp;</strong></font><font color="#7f0055"><strong>int&nbsp;</strong></font><font color="#000000">deptNo;</font><br />
<font color="#000000">}</font></code><br /><code><font color="#000000"></font></code>

Now let your IDE generate getters and setters.

The following step is to make an
ActionBean. An ActionBean is like a Struts Action and ActionForm
combined. It’s the access point of your application.

<code><font color="#7f0055"><strong>package&nbsp;</strong></font><font color="#000000">nl.amis.action;</font><br />
<br />
<font color="#7f0055"><strong>public&nbsp;class&nbsp;</strong></font><font color="#000000">FormActionBean&nbsp;</font><font color="#7f0055"><strong>implements</strong></font><br />
<font color="#000000">ActionBean&nbsp;</font><font color="#000000">{</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>private&nbsp;</strong></font><font color="#000000">ActionBeanContext&nbsp;context;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>private&nbsp;</strong></font><font color="#7f0055"><strong>int&nbsp;</strong></font><font color="#000000">id;</font><br />
<br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;</font><font color="#646464">@DefaultHandler&nbsp;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>public&nbsp;</strong></font><font color="#000000">Resolution&nbsp;submit</font><font color="#000000">()&nbsp;{</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">Employee&nbsp;emp&nbsp;=&nbsp;</font><font color="#7f0055"><strong>new&nbsp;</strong></font><font color="#000000">Employee</font><font color="#000000">()</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">emp.setId</font><font color="#000000">(</font><font color="#7f0055"><strong>this</strong></font><font color="#000000">.id</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">emp.setDeptNo</font><font color="#000000">(</font><font color="#990000">20</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">emp.setFunctionName</font><font color="#000000">(</font><font color="#2a00ff">&quot;CLERK&quot;</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">emp.setName</font><font color="#000000">(</font><font color="#2a00ff">&quot;KING&quot;</font><font color="#000000">)</font><font color="#000000">;</font><br />
<br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#7f0055"><strong>return&nbsp;new&nbsp;</strong></font><font color="#000000">JavaScriptResolution</font><font color="#000000">(</font><font color="#000000">emp</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br />
<font color="#000000">}</font></code>

Create getters and setters for context
and id. The submit method is the access point of your application.
The nice thing about Stripes is that you don’t need to pass the
request, response and some other objects (like Struts and Spring
MVC), when you want those object you can get them from the context
object. The DefaultHandler annotation is needed to provide a default
method. When you provide another method than submit, submit will
still be called. Today we’re not connecting to a databae, the only
employee is KING.

A JavaScriptResolution object is
returned. This is the magic, the Java object is converted to
JavaScript. By the way, this was all the Java code you’ll need today.

The Form

Before I start explaining what the
JavaScriptResolution returns I will explain how to submit a form with
Stripes.

Create a jsp page with html, head and
body tags. Include the Stripes tag library at the beginning of the
document:

&lt;%@ taglib prefix=&quot;stripes&quot;<br />uri=&quot;http://stripes.sourceforge.net/stripes.tld&quot; %&gt;
<br />

Now put the following tags inside the
body tag:

<code><font color="#000000">&lt;stripes:form</font><br />
<font color="#000000">beanclass=</font><font color="#2a00ff">&quot;nl.amis.action.FormActionBean&quot;&nbsp;</font><font color="#000000">id=</font><font color="#2a00ff">&quot;form&quot;</font><font color="#000000">&gt;</font><br />
<br />
<font color="#ffffff">&nbsp;&nbsp;</font><font color="#000000">id:&nbsp;&lt;stripes:text&nbsp;name=</font><font color="#2a00ff">&quot;id&quot;</font><font color="#000000">/&gt;&lt;br/&gt;</font><br />
<br />
<font color="#ffffff">&nbsp;&nbsp;</font><font color="#000000">&lt;stripes:submit&nbsp;name=</font><font color="#2a00ff">&quot;submit&quot;</font><font color="#000000">/&gt;</font><br />
<br />
<font color="#000000">&lt;/stripes:form&gt;</font></code>

The beanclass attribute is very cool in
my opinion, it could lead to some refactoring problems with some
IDE’s, but when you use an IDE like IntelliJ IDEA it saves you the
trouble of the url/class mapping. The id tag is needed so we can find
the form tag more easy, but when for some reason it isn’t possible to
put in an id attribute you can work around it relatively easy.

The <stripes:text> tag creates an
input tag in which we can enter an employee id. This employee-id is
set via the setId method in Java. The submit tag creates a submit
button, the name attribute contains the method that’s called in Java
(submit() in our case)

Go to the form in your browser, it
should look like this:

I’m currently searching for id 1337.
When I hit the submit button the result might be a bit strange, but
that’s because Stripes is returning Javascript.

To make this object available as a real
JavaScript object you have to evaluate the the code in JavaScript:

< >var obj = eval(result);>

At first sight this looks a bit
strange. Setting two variables where only one is needed and a
variable name with a semi-colon at the end. This last line will
return the object you need

When you display the object in FireBug
it looks like this:

Ajaxify your form

But first you have to submit the form
via Javascript and define a callback function to evaluate the code.
The trick is to send the form via Javascript just before it is
submitted in the normal way. I’ve done this a few times and was
thinking how to do it this time. Since I’m figuring out jQuery it
seemed like a good idea to use jQuery. After some searching I found
malsup’s Form plugin.
You can say to that plugin : “hey I have a form here, ajaxify it
for me and call function x when you’re ready”

The first step is to include jQuery and
the jQuery form plugin (for download links see the sources section
later in this article):

<code>
<font color="#000000">&lt;script&nbsp;type=</font><font color="#2a00ff">&quot;text/javascript&quot;&nbsp;</font><font color="#000000">src=</font><font color="#2a00ff">&quot;js/jquery.js&quot;</font><font color="#000000">&gt;&lt;/script&gt;</font><br />
<br />
<font color="#000000">&lt;script&nbsp;type=</font><font color="#2a00ff">&quot;text/javascript&quot;&nbsp;</font><font color="#000000">src=</font><font color="#2a00ff">&quot;js/form.js&quot;</font><font color="#000000">&gt;&lt;/script&gt;</font></code>

Now put some div’s in to display the
result:

<code><font color="#000000">id:&nbsp;&lt;div&nbsp;id=</font><font color="#2a00ff">&quot;empno&quot;</font><font color="#000000">&gt;&lt;/div&gt;&lt;br/&gt;</font><br />
<font color="#000000">name:&nbsp;&lt;div&nbsp;id=</font><font color="#2a00ff">&quot;name&quot;</font><font color="#000000">&gt;&lt;/div&gt;&lt;br/&gt;</font><br />
<font color="#000000">function:&nbsp;&lt;div&nbsp;id=</font><font color="#2a00ff">&quot;functionname&quot;</font><font color="#000000">&gt;&lt;/div&gt;&lt;br/&gt;</font><br />
<font color="#000000">department:&nbsp;&lt;div&nbsp;id=</font><font color="#2a00ff">&quot;deptno&quot;</font><font color="#000000">&gt;&lt;/div&gt;&lt;br/&gt;</font></code>
<code><font color="#000000"></font></code><code></code><code></code>The final action is including a small
Javascript:<code>
</code>
<code><font color="#000000">&lt;script&nbsp;type=</font><font color="#2a00ff">&quot;text/javascript&quot;</font><font color="#000000">&gt;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">$</font><font color="#000000">(</font><font color="#000000">document</font><font color="#000000">)</font><font color="#000000">.ready</font><font color="#000000">(</font><font color="#000000">function</font><font color="#000000">()&nbsp;{</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">var&nbsp;options&nbsp;=&nbsp;</font><font color="#000000">{</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">after:&nbsp;&nbsp;&nbsp;&nbsp;myAjaxSuccess</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><font color="#000000">;</font><br />
<br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">$</font><font color="#000000">(</font><font color="#ff6100">'#f</font><font color="#000000">orm</font><font color="#ff6100">').</font><font color="#000000">ajaxForm</font><font color="#000000">(</font><font color="#000000">options</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">})</font><font color="#000000">;</font><br />
<br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">function&nbsp;myAjaxSuccess</font><font color="#000000">(</font><font color="#000000">responseText,&nbsp;statusText</font><font color="#000000">)&nbsp;{</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">var&nbsp;obj&nbsp;=&nbsp;eval</font><font color="#000000">(</font><font color="#000000">responseText</font><font color="#000000">)</font><font color="#000000">;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">$</font><font color="#000000">(</font><font color="#ff6100">'#e</font><font color="#000000">mpno</font><font color="#ff6100">')[</font><font color="#990000">0</font><font color="#000000">]</font><font color="#000000">.innerHTML&nbsp;=&nbsp;obj.id;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">$</font><font color="#000000">(</font><font color="#ff6100">'#n</font><font color="#000000">ame</font><font color="#ff6100">')[</font><font color="#990000">0</font><font color="#000000">]</font><font color="#000000">.innerHTML&nbsp;=&nbsp;obj.name;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">$</font><font color="#000000">(</font><font color="#ff6100">'#f</font><font color="#000000">unctionname</font><font color="#ff6100">')[</font><font color="#990000">0</font><font color="#000000">]</font><font color="#000000">.innerHTML&nbsp;=&nbsp;obj.functionName;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">$</font><font color="#000000">(</font><font color="#ff6100">'#d</font><font color="#000000">eptno</font><font color="#ff6100">')[</font><font color="#990000">0</font><font color="#000000">]</font><font color="#000000">.innerHTML&nbsp;=&nbsp;obj.deptNo;</font><br />
<font color="#ffffff">&nbsp;&nbsp;&nbsp;&nbsp;</font><font color="#000000">}</font><br />
<font color="#000000">&lt;/script&gt;</font></code>

Reload the form and you’re finished.

$(document).ready ajaxifies the form
after the page is finished loading. There are a lot of options you
can include (check the source of form.js for documentation). The
after property sets the callback function of the form submission. In
the callback function I put the values of the returned object into
the div’s. $(‘#name’) is short for get all elements dat have id (id
is #, class is . and nothing is tagname) name. Because this function
returns an array we need the first element.

The result is quite useless right now,
but I hope you can see the possibilities of this construction.

Final words

The Stripes documentation is doing
almost the same thing with prototype, but I think my solution is a
little bit nicer. When you never did anything with jQuery you really
should check it out, it still amazes me what is possible with 15K of
Javascript.

The Form plugin is documented quite
good, usually the really nice stuff is documented so poorly it’s
useless unless you understand every line of code, so kudos to Malsup.

You should also give Stripes a shot, it
has some really nice features and integrates with Spring. The best
feature of Stripes are the layout tags, maybe I’ll write an article
about that too, but for now you have to visit the Stripes wiki.

Sources

Stripes

jQuery

Malsup’s jQuery Form plugin

Code on this page

Share.

About Author

2 Comments

  1. Jasper Fontaine on

    Nice article, thanks!

    And congratulations for being able to work with Stripes! I’m doing a big project with it right now, and am very very glad i chose Stripes as the MVC layer. The lines of code i have to write compared to Struts (and SpringMVC for that matter) are so incredibly few, clear and concise i’ll probably never go back.