我需要在我的java Web应用程序中实现电子邮件确认。我被困在我必须发送给用户的电子邮件中。
我需要将(确认电子邮件的)模板与User对象组合在一起,这将是确认电子邮件的html内容。
我想过使用xslt作为模板引擎,但我没有User对象的xml形式,也不知道如何从User实例创建一个xml。
我考虑过jsp,但是如何使用对象渲染jsp页面并获得html?
知道我可以使用哪些包来创建模板并将其与对象结合起来吗?
答案 0 :(得分:0)
之前我使用过以下内容。我似乎记得它并不复杂
答案 1 :(得分:0)
用户对象有多复杂?如果它只是五个字符串值字段(比如说),你可以简单地将它们作为字符串参数提供给转换,从而避免了从Java数据构建XML的需要。
或者,Java XSLT处理器通常提供一些方法来从XSLT代码中调用Java对象上的方法。因此,您可以将Java对象作为参数提供给样式表,并使用扩展函数调用其方法。细节是特定于处理器的。
答案 2 :(得分:0)
不是学习新代码,而是调试其他复杂的代码,我决定编写自己的小而且合适的函数:
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();
}
}