如何在JSP页面中直接编写scala脚本?

Bro*_*die 8 jsp scala

是否有任何scala JSP引擎,或者会有什么?我知道scala web框架提升,但它似乎更像标签.我正在寻找像PHP这样的脚本的方法.

谢谢.

huy*_*hjl 5

编辑:检查http://scalate.fusesource.org/index.html:Scala的模板引擎:如JSP没有废话,但添加了Scala的凉意.我在查看#scala twitter feed时偶然发现了这一点.


你可以看看http://github.com/alandipert/step.它看起来像一个活跃的项目,足以让你编写与xhtml代码混合的Scala代码.你是否可以像PHP一样编写脚本,我不知道.其中一个不同之处在于,使用JSP/PHP,您可以在HTML页面中包含一个程序,其中step包含一些xmlScala文件.

Scala会有一些学习曲线,sbt但我认为利用Scala是值得的.

您可以做的另一件事是编写一个自定义JSP标记,它允许您通过解释器运行一些Scala代码.我做了一个概念证明,这似乎有效:

/**
 * Proof of concept, you can run Scala code in a JSP tag.
 * Works with jetty and sbt.
 */
class ScalaScriptTag extends BodyTagSupport {

  override def doAfterBody():Int = {
    try { 
      val settings = new Settings(str => println(str))
      // interpreter classloader does not seem to pick up classes from the parent
      settings.classpath.value = 
        "lib_managed/compile/jsp-api-2.1-6.1.14.jar;" + 
        "lib_managed/compile/servlet-api-2.5-6.1.14.jar"
      var i = new Interpreter(settings) {
        override def parentClassLoader():ClassLoader = {
          return Thread.currentThread().getContextClassLoader();
        }
      }
      i.bind("pageContext", "javax.servlet.jsp.PageContext", pageContext)
      val source = Source.fromString(getBodyContent.getString)
      for (line <- source.getLines) { i.interpret(line) }
    } catch {
      case ioe: IOException => 
        throw new JspException(ioe.getMessage())
    }
    Tag.SKIP_BODY
  }

}                       
Run Code Online (Sandbox Code Playgroud)