[英]confirmation email - create templae and combine it with an object
I need to implement email confirmation in my java web application. 我需要在Java Web应用程序中实现电子邮件确认。 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. 我需要将模板(确认电子邮件)与User对象结合在一起,这将是确认电子邮件的html内容。
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. 我考虑过使用xslt作为模板引擎,但是我没有User对象的xml形式,并且我真的不知道如何从User实例创建xml。
I thought about jsp, but how do I render jsp page with an object and get the html as a result? 我考虑过jsp,但是如何使用对象渲染jsp页面并得到html?
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 我似乎记得这并不复杂
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. 如果仅是五个字符串值的字段(例如),则只需将它们作为字符串参数提供给转换,就无需从Java数据构建XML。
Alternatively, Java XSLT processors typically provide some way to invoke methods on Java objects from within the XSLT code. 另外,Java XSLT处理器通常提供某种方式从XSLT代码中调用Java对象上的方法。 So you could supply the Java object as a parameter to the stylesheet and invoke its methods using extension functions. 因此,您可以将Java对象作为样式表的参数提供,并使用扩展功能调用其方法。 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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.