Scala 的 JSP 或 .ascx 等效项?

发布于 2024-08-10 00:32:14 字数 567 浏览 2 评论 0原文

我正在 Scala 中开发一个小型 MVC“框架”(它真的非常小)。我希望能够将视图文件编写为 Scala 代码,这样我就可以从编译器获得很多帮助。预编译很棒,但我真正想要的是一种让 servlet 容器根据请求自动编译某些文件(我的视图文件)的方法,这样我就不必关闭 Jetty 并立即编译所有源文件,然后再次启动它只是为了查看 HTML 的微小变化。

我经常使用 .NET 中的 .ascx 文件执行此操作(该文件仅包含一个 scriptlet 标记,其中包含一堆 C# 代码,其中使用 XmlWriter 写出标记),我喜欢这个工作流程。您只需进行更改,然后刷新浏览器,但它仍在编译!

我对 Java 没有太多经验,但使用 JSP 似乎也可以做到这一点。我想知道这种事情在 Scala 中是否可能。

我已经研究过自己构建这个(在此处查看更多信息:http://www.nabble .com/Compiler-API-td12050645.html),但如果有的话我宁愿使用其他东西。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紅太極 2024-08-17 00:32:14

如果您想要类似于 JSP/ASP/Erb 但使用 Scala 代码的东西,您可能需要看看 Scalate

Scalate 是一个基于 Scala 的模板引擎,它允许您使用强大的 Scala 表达式,而不是有限的 JSP/JSF/JSTL EL 表达式语言 - 同时完全静态类型化,以便在编辑/编译时检查模板是否有错误 - 并重新加载模板在编辑时即时进行。

对于 JSP/ASP 风格的模板,请尝试 Scalate 中的 Ssp 模板,它们是很像JSP。

如果您主要生成 HTML/XML 标记,我还建议提供 Scaml 尝试一下 Scalate 中的模板 - 它们是 HAML 的 Scala 版本,这会导致真正的 DRY 模板

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

苍景流年 2024-08-17 00:32:14

当我通过在 Scala 中使用嵌入式 xml 进行模板化编写 servlet 来跳过 JSP/框架时,我想到了这一点:

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
}

}

我的解决方案有两部分:

  1. 使用 fsc 而不是 scalac
  2. 使用 FireBug,特别是它的 edit 按钮。

我发现自己经常对样式表(不需要重新启动 Jetty)进行小的更改,或者使用可能的 HTML 替代方案。最好的方法是右键单击 HTML,单击“检查元素”,然后按 Firebug 控制台中的“编辑”按钮,并当场编辑它。这意味着每次进行更改时都不再需要恢复站点的状态。

当你看起来正确时,将更改复制到 scala 并点击 make。

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.

幸福还没到 2024-08-17 00:32:14

有很多替代方案。例如,一种替代方案是使用 JRebel(以前称为 JavaRebel)和更改时的后台编译过程(例如使用 Maven 的 mvn scala:cc)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文