JSP or .ascx equivalent for Scala?

I am working on a small MVC map (it is very small) in Scala. I would like to be able to write my files as Scala code so that I can get a lot of help from the compiler. The precompilation is great, but I really want the servlet container to automatically compile certain files (my view files) upon request, so I don’t need to close Jetty and compile all the source files at once, then run it again to see small changes in my HTML.

I do this a lot with .ascx files in .NET (the file will contain only one scriptlet tag with a bunch of C # code, inside which markup is written using XmlWriter), and I like this workflow. You just make changes and then update your browser, but it is still compiled!

I do not have much experience with Java, but it seems that this can be done with JSP as well. I am wondering if something like this is possible in Scala.

I myself studied this myself (see more details here: http://www.nabble.com/Compiler-API-td12050645.html ), but I would prefer to use something else if there is one.

+7
scala jsp ascx
source share
3 answers

If you want something like JSP / ASP / Erb, but using the Scala code, you can take a look at Scalate .

Scalate is a Scala-based template engine that allows you to use powerful Scala expressions instead of the limited expression language JSP / JSF / JSTL EL - while it is fully statically entered so that the templates are checked for editing / compilation time for errors - and the templates are reloaded "on fly "when they are being edited.

For JSP / ASP template style, try Ssp templates in Scalate, which are very similar to JSP.

If you mostly generate HTML / XML markup, I would also recommend trying Scaml templates in Scalate - they are Scala versions of HAML that lead to really DRIED templates

+9
source share

This comes from me when I skip JSP / frameworks, write servlets in Scala with built-in xml for templates:

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 consists of two parts:

  • Use fsc instead of scalac
  • Use FireBug , in particular its edit button.

The constant little changes that I find for myself are a stylesheet (which doesn't require a Jetty restart) or a game with possible HTML alternatives. The best way to do this is to right-click the HTML, click Inspect Element, then click the edit button in the firebug console and edit it in place. This means that you do not restore the state of the site every time you make changes.

When you look at it correctly, copy the changes to Scala and click make.

+3
source share

There are many alternatives. For example, one option is to use JRebel (formally JavaRebel) and the background compilation process when changing (for example, mvn scala:cc with Maven, for example).

+2
source share

All Articles