tags:

views:

368

answers:

3

I'm working on a small MVC "framework" (it's really very small) in Scala. I'd like to be able to write my view files as Scala code so I can get lots of help from the compiler. Pre-compiling is great, but what I really want is a way to have the servlet container automatically compile certain files (my view files) on request so I don't have to shut down Jetty and compile all my source files at once, then start it up again just to see small changes to my HTML.

I do this a lot with .ascx files in .NET (the file will contain just one scriptlet tag with a bunch of C# code inside which writes out markup using an XmlWriter) and I love this workflow. You just make changes and then refresh your browser, but it's still getting compiled!

I don't have a lot of experience with Java, but it seems possible to do this with JSP as well. I'm wondering if this sort of thing is possible in Scala.

I have looked into building this myself (see more info here: http://www.nabble.com/Compiler-API-td12050645.html) but I would rather use something else if it's out there.

+2  A: 

There are lots of alternatives. One alternative, for instance, is to use JRebel (formely JavaRebel), and a background compilation process on-change (such as mvn scala:cc with Maven, for example).

Daniel
Is there a way to combine scala:cc with jetty:run? JRebel looks great, but unfortunately it isn't free.
Daniel Worthington
I could be way off here, and maybe this was just wrong information, but I could swear I saw on Twitter or somewhere else online that the JRebel folks were giving away JRebel for scala projects. Something about promoting Scala growth. Maybe post something on the java posse forums? I bet they'd have an answer
marc esher
You're right, Marc. There's documentation on the Lift wiki on getting a free license and setting up JRebel with Tomcat or Jetty http://wiki.liftweb.net/index.php?title=JavaRebel
Daniel Worthington
You may just use two windows, one with scala:cc running, and the other with jetty:run.
Daniel
A: 

This comes up fro me when I skip JSP/frameworks by writing servlets in Scala with embedded xml for templating:

class MyServlet extends HttpServlet {

def get(req) = {
 var title = "hello world"
 var link = "somepage"
 <html>
   <head><title>{ title }</title></head>
   <body><a href={ "/" + link }>Click</a></body>
 </html>
}

def doGet(req: HttpServletRequest, res: HttpServletResponse) = {
 val out = new PrintWriter(res.getOutputStream())
 out.println(get(req))
 out.close
}

}

My solution has two parts:

  1. Use fsc instead of scalac
  2. Use FireBug, specifically its edit button.

The constant small changes I find myself making are to the style sheet (which does not require restarting Jetty), or playing with possible HTML alternatives. The best way to do that is to right-click the HTML, click Inspect Element, then press the edit button in the firebug console, and edit it on the spot. This means no more recovering of the site's state every time you make a change.

When you get it looking right, copy the changes over to scala and hit make.

David Crawshaw
I do love me some firebug, but I really prefer to do all my editing work in my favorite editor. There are also a lot of benefits to using code to write out HTML. For example, you can conditionally print out an attribute (like checked="checked" on a checkbox). I find myself adding these kinds of more complicated things in code all the time and I'd like the save-refresh story to work.
Daniel Worthington
+2  A: 

If you want something thats kinda like JSP/ASP/Erb but using Scala code you might want to take a look at Scalate.

Scalate is a Scala based template engine which allows you to use powerful Scala expressions instead of the limited JSP/JSF/JSTL EL expression language - while being completely statically typed so that templates are checked at edit/compile time for errors - and templates are reloaded on the fly as they are edited.

For JSP/ASP style of templates then try the Ssp templates in Scalate which are very JSP-like.

If you are mostly generating HTML/XML markup I'd also recommend giving the Scaml templates in Scalate a try - they are Scala versions of HAML which lead to really DRY templates

James Strachan