如何解析模板化句子“#{name}邀请你”使用表达语言
我是 Java 的新手。
我的意图是在Java程序中使用类似句子的模板(没有JSP或任何网络相关页面)
示例:
String name = "Jon";
"#{ name } invited you";
or
String user.name = "Jon";
"#{ user.name } invited you";
如果我将此字符串传递给某个方法,我应该得到
"Jon invited you"
我已经通过了一些表达式语言MVEL、OGNL、JSTL EL
在 MVEL 和 OGNL 中,我必须编写一组代码来实现此目的,但以其他方式实现。
我只能在 JSP 文件中使用 JSTL EL 来实现此目的,而不是在 java 程序中。
有什么办法可以实现这一点吗?
提前致谢。
乔恩
I am a new bee to Java.
My intension is to use the template like sentences in Java program (no JSP or any web related pages)
Example:
String name = "Jon";
"#{ name } invited you";
or
String user.name = "Jon";
"#{ user.name } invited you";
If I pass this string to some method, I should get
"Jon invited you"
I've gone through some expression languages MVEL, OGNL, JSTL EL
In MVEL and OGNL, I have to write some set of code to achieve this but in some other way.
I can achieve this using JSTL EL only in JSP files not in java program.
Is there any way to achieve this?
Thanks in advance.
Jon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不是 100% 确定我明白你的意思,但这里有一些提示...
看看
MessageFormat
API 中的类。您可能还对Formatter
类和/或
String.format
方法。如果您有一些
Properties
并且想要搜索和替换形状#{ property.key }
的子字符串,您也可以这样做:如果您有实际的 POJO 和不是 Properties 对象,您可以通过反射,如下所示:
...但它变得丑陋,我建议您考虑深入挖掘一些第三方库来解决这个问题。
I'm not 100% sure I understand what you're after, but here are some pointers...
Have a look at the
MessageFormat
class in the API. You may also be interested in theFormatter
class and/or theString.format
method.If you have some
Properties
and you want to search and replace substrings of the shape#{ property.key }
you could also just do like this:If you have actual POJOs and not a Properties object, you could go through reflection, like this:
... but it's getting ugly, and I suggest you consider digging deeper into some of the 3rd party libraries for solving this.