繁体   English   中英

确认电子邮件-创建模板并将其与对象组合

[英]confirmation email - create templae and combine it with an object

我需要在Java Web应用程序中实现电子邮件确认。 我被我必须发送给用户的电子邮件所困扰。

我需要将模板(确认电子邮件)与User对象结合在一起,这将是确认电子邮件的html内容。
我考虑过使用xslt作为模板引擎,但是我没有User对象的xml形式,并且我真的不知道如何从User实例创建xml。
我考虑过jsp,但是如何使用对象渲染jsp页面并得到html?

知道我可以使用哪些软件包来创建模板并将其与对象组合吗?

我以前使用过以下内容。 我似乎记得这并不复杂

http://velocity.apache.org/

用户对象有多复杂? 如果仅是五个字符串值的字段(例如),则只需将它们作为字符串参数提供给转换,就无需从Java数据构建XML。

另外,Java XSLT处理器通常提供某种方式从XSLT代码中调用Java对象上的方法。 因此,您可以将Java对象作为样式表的参数提供,并使用扩展功能调用其方法。 详细信息是特定于处理器的。

我没有学习新的代码,而是调试其他人的复杂代码,而是决定编写自己的小型实用程序:

public class StringTemplate {
    private String filePath;
    private String charsetName;
    private Collection<AbstractMap.SimpleEntry<String, String>> args;

    public StringTemplate(String filePath, String charsetName,
                          Collection<AbstractMap.SimpleEntry<String, String>> args) {
        this.filePath = filePath;
        this.charsetName=charsetName;
        this.args = args;
    }

    public String generate() throws FileNotFoundException, IOException {
        StringBuilder builder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                getClass().getResourceAsStream(filePath),charsetName));
        try {
            String line = null;

            while ((line = reader.readLine()) != null) {
                builder.append(line);
                builder.append(System.getProperty("line.separator"));
            }
        } finally {
            reader.close();
        }
        for (AbstractMap.SimpleEntry<String, String> arg : this.args) {
            int index = builder.indexOf(arg.getKey());
            while (index != -1) {
                builder.replace(index, index + arg.getKey().length(), arg.getValue());
                index += arg.getValue().length();
                index = builder.indexOf(arg.getKey(), index);
            }
        }
        return builder.toString();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM