如何解析模板化句子“#{name}邀请你”使用表达语言

发布于 2024-12-01 03:37:00 字数 538 浏览 0 评论 0原文

我是 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 技术交流群。

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

发布评论

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

评论(1

记忆で 2024-12-08 03:37:00

有什么办法可以实现这一点吗?

我不是 100% 确定我明白你的意思,但这里有一些提示...

看看MessageFormat API 中的类。您可能还对 Formatter类和/或 String.format 方法。

如果您有一些 Properties 并且想要搜索和替换形状 #{ property.key } 的子字符串,您也可以这样做:

import java.util.Properties;
import java.util.regex.*;

class Test {

    public static String process(String template, Properties props) {
        Matcher m = Pattern.compile("#\\{(.*?)\\}").matcher(template);

        StringBuffer sb = new StringBuffer();
        while (m.find())
            m.appendReplacement(sb, props.getProperty(m.group(1).trim()));
        m.appendTail(sb);

        return sb.toString();
    }


    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("user.name", "Jon");
        props.put("user.email", "[email protected]");

        String template = "Name: #{ user.name }, email: #{ user.email }";

        // Prints "Name: Jon, email: [email protected]"
        System.out.println(process(template, props));
    }
}

如果您有实际的 POJO 和不是 Properties 对象,您可以通过反射,如下所示:

import java.util.regex.*;

class User {
    String name;
    String email;
}


class Test {

    public static String process(String template, User user) throws Exception {
        Matcher m = Pattern.compile("#\\{(.*?)\\}").matcher(template);

        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            String fieldId = m.group(1).trim();
            Object val = User.class.getDeclaredField(fieldId).get(user);
            m.appendReplacement(sb, String.valueOf(val));
        }
        m.appendTail(sb);
        return sb.toString();
    }


    public static void main(String[] args) throws Exception {
        User user = new User();
        user.name = "Jon";
        user.email = "[email protected]";
        String template = "Name: #{ name }, email: #{ email }";

        System.out.println(process(template, user));
    }
}

...但它变得丑陋,我建议您考虑深入挖掘一些第三方库来解决这个问题。

Is there any way to achieve this?

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 the Formatter class and/or the String.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:

import java.util.Properties;
import java.util.regex.*;

class Test {

    public static String process(String template, Properties props) {
        Matcher m = Pattern.compile("#\\{(.*?)\\}").matcher(template);

        StringBuffer sb = new StringBuffer();
        while (m.find())
            m.appendReplacement(sb, props.getProperty(m.group(1).trim()));
        m.appendTail(sb);

        return sb.toString();
    }


    public static void main(String[] args) {
        Properties props = new Properties();
        props.put("user.name", "Jon");
        props.put("user.email", "[email protected]");

        String template = "Name: #{ user.name }, email: #{ user.email }";

        // Prints "Name: Jon, email: [email protected]"
        System.out.println(process(template, props));
    }
}

If you have actual POJOs and not a Properties object, you could go through reflection, like this:

import java.util.regex.*;

class User {
    String name;
    String email;
}


class Test {

    public static String process(String template, User user) throws Exception {
        Matcher m = Pattern.compile("#\\{(.*?)\\}").matcher(template);

        StringBuffer sb = new StringBuffer();
        while (m.find()) {
            String fieldId = m.group(1).trim();
            Object val = User.class.getDeclaredField(fieldId).get(user);
            m.appendReplacement(sb, String.valueOf(val));
        }
        m.appendTail(sb);
        return sb.toString();
    }


    public static void main(String[] args) throws Exception {
        User user = new User();
        user.name = "Jon";
        user.email = "[email protected]";
        String template = "Name: #{ name }, email: #{ email }";

        System.out.println(process(template, user));
    }
}

... but it's getting ugly, and I suggest you consider digging deeper into some of the 3rd party libraries for solving this.

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