是否有类似于 Razor for Java Spring Web 应用程序的模板语言?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7192534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:00:57  来源:igfitidea点击:

Is there a templating language similar to Razor for Java Spring web applications?

javaasp.net-mvcspring-mvctemplate-enginetemplating

提问by Tim H

I'm in love with razor templates in .NET MVC 3. Is there anything close for java?

我爱上了 .NET MVC 3 中的 razor 模板。java 有什么接近的吗?

I'd be looking for something where I could avoid using JSTL tags and instead do something like this:

我会寻找一些可以避免使用 JSTL 标签的东西,而是做这样的事情:

<c:if test=${bla}>
   <span>my html</span>
</c:if>

and instead do

而是做

@if(bla)
{
  <span>my html</span>
}

I'm assuming there must be similar

我假设必须有类似的

回答by Gelin Luo

I'd like to introduce my work: Rythm template engine, a lightweight and super fast template engine in Java using the Razorlike syntax. Rythm has rich features and supports page layout/inheritance, customized tags (either in template or java class), dynamic reload at dev mode and much more. The benchmarkshows Rythm is 2 to 3 times faster than Velocityon a normal page!

我想介绍一下我的工作:Rythm 模板引擎,一个轻量级、超快速的 Java 模板引擎,使用类似Razor 的语法。Rhythm 具有丰富的功能,支持页面布局/继承、自定义标签(在模板或 java 类中)、开发模式下的动态重新加载等等。该指标显示的节奏比较快2至3倍的速度正常的页面上!

The API is simple:

API很简单:

  1. render with inline string:

    String output = Rythm.render("@args String who;hello @who!", "world");

  2. render with template file:

    String output = Rythm.render("hello.txt", "world");

  1. 使用内联字符串渲染:

    String output = Rythm.render("@args String who;hello @who!", "world");

  2. 使用模板文件渲染:

    String output = Rythm.render("hello.txt", "world");

A brief introduction to Rythm: http://software-lgl.blogspot.com.au/2012/03/rythm-easy-to-use-high-performance-java.html

Rythm 简介:http: //software-lgl.blogspot.com.au/2012/03/rythm-easy-to-use-high-performance-java.html

Updates 20120701

更新 20120701

The latest version introduced a feature called "String Interpolation Mode", which enable you to do very lightweight string interpolation like follows:

最新版本引入了一个名为“字符串插值模式”的功能,它使您能够进行非常轻量级的字符串插值,如下所示:

String result = Rythm.render("hello @who!", "world");

A full feature demonstrationis hosted on GAE: http://play-rythm-demo.appspot.com/

GAE 上一个完整的功能演示http: //play-rythm-demo.appspot.com/

Updates 20130406

更新 20130406

A rythm fiddle web site is online now, and you can use it to learn Rythm syntax. Check it out at http://fiddle.rythmengine.org

节奏小提琴网站现已上线,您可以使用它来学习节奏语法。在http://fiddle.rythmengine.org查看

Updates 20130513

更新 20130513

  • package name changed from com.greenlaw110.rythmto org.rythmengine, the maven group id changed accordingly
  • Checkout the new project website: http://rythmengine.org
  • 包名由com.greenlaw110.rythmto 更改org.rythmengine,maven group id 相应更改
  • 查看新项目网站:http: //rythmengine.org

回答by Roland Tepp

As far as I know, there is none that looks and behaves quite like Razor in that Java world.

据我所知,在 Java 世界中,没有任何一款外观和行为与 Razor 非常相似。

From what I can understand, the templating engine in Razor does not only parse the "placeholders" in the static text, like #xxxin Velocity or ${xxx}in JSP.

据我所知,Razor 中的模板引擎不仅仅解析静态文本中的“占位符”,就像#xxx在 Velocity 或${xxx}JSP 中一样。

Instead the @symbol in Razor acts as a toggle for switching to the hosting language parser (VB and C# in case of Razor), recognizing full syntax of the element immediately following the @sign. This allowes Razor to recognize both names of objects passed to the engine as well as syntactic structures like for loops and conditionals.

相反@,Razor 中的符号充当切换到宿主语言解析器(在 Razor 的情况下为 VB 和 C#)的开关,识别紧跟@符号的元素的完整语法。这允许 Razor 识别传递给引擎的对象的名称以及诸如 for 循环和条件的句法结构。

This opens up the full power of the hosting language to te Razor templates, which can be a dangerous thing in wrong hands...

这为 te Razor 模板打开了托管语言的全部力量,这在坏人手中可能是一件危险的事情......

Most of the templating engines in the Java side of the world have conciously chosen to strictly separate business logic from templating concerns and thus have very limited or no support for dynamic features like looping or conditionals in their template "languages", opting for declarative style over dynamic.

世界上 Java 一侧的大多数模板引擎都自觉地选择将业务逻辑与模板关注点严格分开,因此在其模板“语言”中对循环或条件等动态特性的支持非常有限或不支持,选择声明式风格而不是动态的。

回答by atrain

Spring supports a number of templating languages:

Spring 支持多种模板语言:

For more reading on how to integrate with Spring, see this page.

有关如何与 Spring 集成的更多信息,请参阅此页面

Additionally, there's StringTemplate, which while not referenced in the official Spring documentation, can be usedas a Spring template engine.

此外,还有StringTemplate,虽然在官方 Spring 文档中没有引用,但可以用作Spring 模板引擎。

And if you reallywant to go wild, here's a pagewith about a bazillion other template engines.

如果你真的想疯狂,这里有一个页面,里面有很多其他模板引擎。

回答by deamon

Take a look at twirl, the Play framework template engine separated from the framework.

看看twirl,与框架分离的 Play 框架模板引擎。

Example:

例子:

@if(items.isEmpty) {
  <h1>Nothing to display</h1>
} else {
  <h1>@items.size items!</h1>
}

回答by vickirk

Try having a look at velocity (http://velocity.apache.org)

尝试查看速度 (http://velocity.apache.org)