[英]Is there a way I can embed Java code inside a YAML file to be processed at runtime.
I know ruby has this facility using ERB.我知道 ruby 有这个使用 ERB 的设施。 But does Java have this capability?
但是Java有这种能力吗? I'm trying to do something like this:
我正在尝试做这样的事情:
MY.yaml CURRENT_DATE: <%=LocalDateTime.now()%> MY.yaml CURRENT_DATE:<%=LocalDateTime.now()%>
Ya, the classic way is using Java server pages https://en.wikipedia.org/wiki/JavaServer_Pages Something like是的,经典的方法是使用 Java 服务器页面https://en.wikipedia.org/wiki/JavaServer_Pages类似
Date : <% Date() %>
myArray:
<% for (int i=1; i<4; i++) { %>
- This is <% i %> th element.
<% } %>
For modern curly bracket based templating use www.thymeleaf.org, or better JTwig with double curly braces and other cool features对于基于现代花括号的模板,请使用 www.thymeleaf.org,或者更好的带有双花括号和其他酷功能的 JTwig
CurrentDate: {{ "now"|date("m/d/Y") }}
https://twig.symfony.com/doc/2.x/filters/date.html https://twig.symfony.com/doc/2.x/filters/date.html
I had a similar requirement where a YAML had an expression which needed to be parsed in runtime in Java.我有一个类似的要求,其中 YAML 有一个需要在 Java 运行时解析的表达式。
We ended up using MVEL for this.我们最终为此使用了MVEL 。 We read the contents of the yaml and passed the expression to MVEL.
我们读取 yaml 的内容并将表达式传递给 MVEL。 Its syntax is quite similar to that of Java.
它的语法与Java 的语法非常相似。
You could also try other template library like Velocity Template .您也可以尝试其他模板库,如Velocity Template 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.