Thursday, July 1, 2010

Sometimes I Don't Get Tags

Yes I have to admit this.

With all the drive to "separate" things, why do specialize tags exists? I mean I don't see it stopping, it's even growing.

If you don't know what I mean, let me explain.

Why do I see a lot of:
<framework:input>
instead of the much simpler and regular:
<input />
?
#replace framework with your favorite framework

So as I understand it in web development we have the client side and the server side, side of things. The client side consists of primarily HTML pages, CSS, javascript. The server side are either Java, Ruby, PHP and databases (these are simplistic elements for the sake of simplicity).

Now, why do frameworks like struts provide tags for UI? Is that not the problem of the UI person and the client side? Why do we have to hack HTML? Can we not write pure HTML and hack (fine tune) it with javascript with the data from the server? For me it will be much cleaner.

So far I only see Wicket is doing this approach.

It's a messed-up world out there, that's maybe the reason why software developers are handsomely paid.

Thursday, June 17, 2010

Struts 2 and jQuery without Fuss

I'm an amateur programmer (after 6 years of working - still considers myself as an amateur), and I scoured the web for a good tutorial or step by step HOWTO on using struts 2 and jquery together that will not make me want my hair pulled out. And alas, I found only one (1), so this is my attempt to be more helpful to amateurs like me.

So I decided to make my own and hopefully this will help others like me who struggled for days to get a grip of the 2 beasts (or beauties).

1. Download the the sample zip file from Struts 2 page here (this is a maven archetype by the way) and unzip.
2. I use Netbeans (6.8) as my IDE (you can also use Eclipse if you know how). Open the project (in Netbeans it is File -> Open Project  ->location of the unzipped file ->Coding_Actions_Struts2_Mvn and click the Open Project button).
3. Download jQuery library. Put it in the Web Pages folder.
4. Download json2.js  (needed for stringify and parse). Put it in the Web Pages folder.


Inspecting the project, you get to see that a lot is already available. Feel free to look around and see what is already available (click the figure below to see actual layout).



Important: You MUST make sure that all the libraries (jar files) are available, just to make sure right click Libraries and click Download Missing Dependencies.

Now let's edit some files:
pom.xml


       <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-json-plugin</artifactId>
            <version>2.1.8.1</version>
        </dependency>

#right click Libraries and click Download Missing Dependencies.

index.jsp
- add the following
<script src="jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="json2.js" type="text/javascript"></script>
<script src="client.js" type="text/javascript"></script>

I only retained the <s:form> for input and removed the <a> links

Create the client.js file and put it in the Web Pages folder.
  client.js is where we will put our Ajax goodies #see the attached code

struts.xml
edit the package name to extend the json-default
       <package name="basicstruts2" extends="struts-default,json-default">


change the result to this:
       <result type="json">
                <param name="excludeNullProperties">true</param>
                <param name="callbackParameter">callback</param>
         </result>




That's IT. You dont even need to change the Action classes.

Try it out.
Tip: try it out in firefox and download the firebug plugin to see the ajax posting (I have not added a loading gif to see if anything is processing)

And  don't forget to spread the link.

Here is the complete source code

mvn source

deployable war