简体   繁体   中英

confirmation email - create templae and combine it with an object

I need to implement email confirmation in my java web application. I am stuck with the email I have to send to the user.

I need to combine a template (of an confirmation email) with the User object and this will be the html content of the confirmation email.
I thought about using xslt as the template engine but I don't have xml form of the User object and don't really know how to create a xml from User instance.
I thought about jsp, but how do I render jsp page with an object and get the html as a result?

Any idea what packages I can use in order to create templae and combine it with an object?

I have used the following before. I seem to recall it wasn't complicated

http://velocity.apache.org/

How complex is the user object? If it's just five string-valued fields (say) you could simply supply these as string parameters to the transformation, avoiding the need to build XML from your Java data.

Alternatively, Java XSLT processors typically provide some way to invoke methods on Java objects from within the XSLT code. So you could supply the Java object as a parameter to the stylesheet and invoke its methods using extension functions. The details are processor-specific.

Instead of learning a new code, debug other's complicate code I decided to write my own small and suitable util:

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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