Friday, October 15, 2010

Mixing Scala and Java in a JSF 2.0 / Java EE 6 project

You can use Scala programming language in a JSF 2.0 / Java EE 6 web application just fine.
And it works very well!

For starters, this is what you need:


Steps to create a mixed Scala-Java JSF 2.0 web project:
  1. Install Eclipse IDE Java EE with the above plugins
  2. Create a new Dynamic Web Project - JSF 2.0 like usual
  3. After project creation, change the perspective to Scala
  4. Right click the Project, click Configuration -> click Add Scala nature
  5. Make sure Scala Library is on the Java Build Path
  6. Make sure Scala Library is on the Web Deployment Assembly
  7. Start to code a Scala bean or class and use them just like Java classes! *with pleasure* :D

Troubleshooting


If Scala classes don't work, and you encounter these error messages:

WARNING: WEB9052: Unable to load class places.City, reason: java.lang.NoClassDefFoundError: scala/ScalaObject
INFO: Initializing Mojarra 2.0.2 (FCS b10) for context '/scalajsf'
SEVERE: Unable to load annotated class: places.City, reason: java.lang.NoClassDefFoundError: scala/ScalaObject

Then you need to add Scala library to the web deployment assembly.

  1. Right-click your Project, and click Properties...
  2. Click Deployment Assembly
  3. Click Add..., then choose Scala Library. The dialog should look like the picture below.
  4. Click OK to close the dialog.
  5. Rebuild & republish the web project and your Scala JSF 2.0 web project should run fine.

No comments:

Post a Comment