The Artima Developer Community
Sponsored Link

Java Buzz Forum
WebWork + JavaScript HOWTO

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Carlos Villela

Posts: 116
Nickname: cvillela
Registered: Jun, 2003

Carlos Villela is a Java developer working mostly with web technologies and AOP.
WebWork + JavaScript HOWTO Posted: Jun 27, 2003 6:37 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Carlos Villela.
Original Post: WebWork + JavaScript HOWTO
Feed Title: That's sooo '82!
Feed URL: http://www.jroller.com/rss/cv?catname=Technical
Feed Description: Carlos Villela's weblog. Everyday life, everyday software development, everyday musings.
Latest Java Buzz Posts
Latest Java Buzz Posts by Carlos Villela
Latest Posts From That's sooo '82!

Advertisement
As I mentioned on my previous post, I'm now coding my Actions on WebWork using JavaScript. I think this feature has been available on WebWork for a long time, but I found so few documentation about it I thought I needed to do something about it. So, here goes my lame and highly irresponsible HOWTO. Hope you enjoy it, and if there's something wrong in it, please and correct me. Feel free to be rude or whatever - I need to know more about the subject, badly :)

In the current 1.3.0 version of WebWork, you can code your actions using Java, JSP or JavaScript. I've seen lots of people using the first two ones, but nobody using the latter one. First, allow me some time to explain the good, and - I'm sure there are a handful of them - the bad points of using this approach.

Good:
  • Very easy to develop and prototype code
  • Cheap JavaScript professionals on the market just waiting to get their hands dirty on something that's not a broken browser implementation of JS
  • Dynamic typing - no more parseInt(), toString() and the like
  • Functions are first class objects, so you can have fun with some functional programming coolness
  • More flexible arrays
  • Global variables - yeah, they're a good point, when you're writing small scripts instead of OOP code full of bureaucracy
  • Syntax very clean and similar to Java, so most of the code is instantly readable by any Java developer
  • Lots of good documentation on Java and JavaScript integration
  • No typecasts ;)
Bad:
  • Performance hit, although minimal, as Rhino (the interpreter used internally by WebWork) does its job well
  • Those cheap JavaScript professionals often don't know OOP, so expect to lose some time on code reviews and giving some training
  • There are some dark corners, both on the language and on Rhino's implementation, and you'll need to be aware of them
  • Little debbuging support (that applies only to the WebWork implementation, not Rhino in general). I'll have to investigate a little more on this; I may be wrong here
  • Still, it's not (J|P)ython, and the language, in comparision, is very poor. Python can get a lot less readable for a Java developer, though (don't believe me? Try figuring out what this does at a single glance: [x(y[z++:1-z]) for x in y], and I'm sure you'll be puzzled for a few hours, while you read the Python manuals)
  • It's kinda kludgy to make JavaScript objects accessible in Java, but I can understand that, and I'm sure the Rhino guys did their best, but it's still not fully transparent

Okay, now, let's get our web server running, first. Make sure you have a sane web server that you can poke around, and be prepared to start and stop it some times.

First, create a new web application and follow the instructions from the WebWork documentation. When you have your jars, web.xml, views.properties (empty, for now) and webwork.properties all set up, make a test deployment just to make sure everything's alright. You'll end up with something like this:

/
  /js-howto

    /WEB-INF
      /classes
      /lib
      web.xml

Now, the fun times begin. Create a new JavaScript file, hello.js, inside WEB-INF/classes (important: JavaScript sources must be located somewhere in the classpath, and the easiest place for now is WEB-INF/classes), and two files, hello-input.vm and hello-success.vm, located on your webapp's root:

/WEB-INF/classes/hello.js:

// Form variables
var name;

// View redirection
var result;

if(name==null) {
  result = "input";

} else {
  result = "success";
}

/hello-input.vm:

<html>
  <body>
    <form action="foo.js.action">

      Enter your name: <input type="text" name="name">

      <br/><br/>

      <input type="submit"/>
    </form>
  </body>
</html>

/hello-success.vm

<html>
  <body>
    Hello, $results.name!
  </body>
</html>

There's something different here, huh? Yeap, all variables left over from the JavaScript execution are available in the $results read-only Map (read-only, in this case, meaning you can't put() or clear() it - not even from JavaScript).

Now, change your views.properties file to contain these lines:

hello.js.action=hello.js
hello.js.input=hello-input.vm
hello.js.success=hello-success.vm

And that's it. Deploy and run the code (if you're using vanilla Tomcat or Jetty, just go to localhost:8080/yourwebapp/hello.action) and everything's working. Voila! :)

Read: WebWork + JavaScript HOWTO

Topic: A Seat! A Seat! My Kingdom for a Seat! Previous Topic   Next Topic Topic: Aspect Oriented Programming with AspectJ by Ivan Kiselev

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use