JSF with scala managed beans

So, I’ve started playing with Scala and my professor Dr.Cay Horstmann suggested that we should see how scala can be used in improving web applications which use JSF and JPA.
So as a first step, I started out by picking the java quiz example from chapter 3 of Dr. Horstmann’s very famous “Core JavaServer Faces” book. After getting it work with Java Beans. I decided to use scala to write the beans, because of the advantages scala provides by automatically generating getters and setters, etc.
So, I wrote a simple Scala program, built a jar out of it, and added that jar to the build path of the JSF app, and tried to call the scala code. It failed recognize the code and netbeans autocomplete didnt work(I picked netbeans because I wanted to work with glassfish v3 and I also feel netbeans has better JSF support than Eclipse).
I tried the same from Eclipse and while code autocompletion worked, the program failed to work because of class loader issues, until I realised that I needed the scala libraries on the java build path for Java to be able to call the scala code. Once I figured this out, I wrote the Scala classes I needed:


import scala.reflect._

class Problem(@BeanProperty val question: String, @BeanProperty val answer:String)
{
    def isCorrect(response:String):Boolean=
    {
        response.trim().equalsIgnoreCase(answer);
    }
}

class QuizBean
{
    var CurrentProblem:Int = 0;

    var tries:Int = 0;

    var score:Int = 0;

    var response:String = "";

    var correctAnswer:String = "";

    val p1 = new Problem("What trademarked slogan describes Java Development?Write once,.....", "run anywhere");
    val p2 = new Problem("What are the first 4 bytes of every class file(in hexadecimal)?", "CAFEBABE");
    val p3 = new Problem("What does this statement print? System.out.println(1+\"2\");", "12");

    val problems: Array[Problem] = Array(p1,p2,p3);

    def QuizBean()
    {
        startOver();
    }

    def startOver()
    {
        CurrentProblem = 0;
        score= 0;
        tries = 0;
        response = "";
    }

   def getQuestion():String =
   {
       val p : Problem = problems(CurrentProblem);
       p.getQuestion;
   }

   def getAnswer():String=
   {
       correctAnswer;
   }

   def getScore():Int=
   {
       score;
   }

   def getResponse():String =
   {
       response;
   }

  def setResponse(newvalue:String)
   {
       response = newvalue;
   }

   def answerAction():String =
   {
       tries = tries + 1;
       if(problems(CurrentProblem).isCorrect(response))
       {
           score = score + 1;
           nextProblem();
           if(CurrentProblem == problems.length)
               return "done";
           else
               return "success";
       }

       else if(tries == 1)
       {
           return "again";
       }
       else
       {
           nextProblem();
           if(CurrentProblem == problems.length) return "done";
           else return "failure";
       }
   }

   def startOverAction():String =
   {
      startOver();
      "startOver";
   }

   def nextProblem()
   {
       val p : Problem = problems(CurrentProblem);
       correctAnswer = p.getAnswer;
       CurrentProblem = CurrentProblem + 1;
       tries = 0;
       response = "";
   }
}

Once again, I built the scala code into a jar file, added the jar onto the JSF app build path, along with the scala libraries. I was still not confident that it would work,but the application worked with the simplest change. All I needed to do was to point to the scala class from the faces-config.xml file.
<managed-bean>
<managed-bean-name>quiz</managed-bean-name>
<managed-bean-class>scalacode.QuizBean</managed-bean-class>//This is the scala class.
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>

After that, all I had to do was “run” the project and it worked like before :-). Life is good again.

PS: please excuse the bad formatting, I havent figured out how to preserve code format here yet.