简体   繁体   中英

How can I easily use a JSP page as an email template using Spring?

My emails will need to be HTML anyway and I don't want to introduce another HTML generation mechanism (eg Velocity) into my project if I don't have to. It would be nice if image references in the generated HTML could be converted into attachments automatically.

You should fire a HTTP request on it.

InputStream response = new URL("http://localhost/context/page.jsp").openStream();

If you want to let the JSP access the same session as the current request, do so:

String url = "http://localhost/context/page.jsp";
String jsessionid = "jsessionid=" + request.getSession().getId();
InputStream response = new URL(url + ";" + jsessionid).openStream();

Needless to say that JSP is not entirely the right tool for the job. JSP is a webbased view technology, not a standalone template technology. Look, you need to fire a whole HTTP request just to get its output. While not necessarily expensive, this is hacky.

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