Sunday, November 16, 2008

Struts 2.0 and Scala

Recently I tried to do some Scala code with Struts 2.0. Unfortunately Scala can't handle nested annotations (yet) so some stuff will be quite hard. You can download the whole project here. Nevertheless, let's get started:

The core is the Scala code which looks like:
    1 import com.opensymphony.xwork2.ActionSupport;
2
3 package edu.ucsd.extension {
4 class HelloWorldAction extends ActionSupport {
5 @scala.reflect.BeanProperty
6 private var name: String = "bubba"
7
8 val SUCCESS = "success";
9
10
11 override def execute(): String = {
12 SUCCESS;
13 }
14 }
15 }



The nifty thing about scala is that the annotation @scala.reflect.BeanProperty saves you all the getters and setters. The sad thing is I couldn't figure out how to access the SUCCESS constant from the parent class so I made it a constant inside scala with didregarding the scala naming conventions. You could also leave the execute method out since the parent class' default implementation returns SUCCESS but I wanted to show it anyway.

The rest of the app relies on zeroconf and code behind -- so there is no configuration and once compiled you can access this action as helloWorld (zeroconf) -- the success will render helloWorld-success.jsp (code behind)


0 Comments:

Post a Comment

<< Home