I have extended VelocityViewServlet and in that class I have: VelocityEngine ve = getVelocityEngine(); Writer writer = getResponseWriter(response);
// stringWriter has my preprocessed template ve.evaluate(ctx,(Writer) writer, "footer", stringWriter.toString()); writer.flush(); Which works. Bill On 2/19/06, David Sperling <[EMAIL PROTECTED]> wrote: > > I got some hints from the Spring project source code, MessageTool > source code and this 3 year old message by Mr. Bubna > "Re: Using Velocity tools in an application?" and finally got > this working. > > I wanted to figure out how to access the MessageTool > from within a Velocity context and send the contents via mail. > Hopefully this will help out others having similar trouble. > > So to test this out I added entries to my config files: > > ******************************************************************** > This was in my main Strut-Config.xml file: > <message-resources parameter="resources.application"/> > ******************************************************************** > > ******************************************************************** > This was in a test-config.xml file > <message-resources key="nonStandardResources" > parameter="resources.nonStandardResources.application"/> > ******************************************************************** > > ******************************************************************** > next the template file: at mywebapp/mailTemplates/testTemplate.vm > > $title : $text.get("test.mail.name", ["jon","doe"]) > $title with no args: $text.get("test.mail.name") > $title with args and bundle: $text.get("test.mail.name", > "nagisateiHotelResources", ["jon", > "doe"] ) > ******************************************************************** > > ******************************************************************** > Struts Action mapping in the Struts module: > <action path="/test/testSendingVelocityMail" > type = "mydomain.myapp.action.TestSendingMailAction" > scope="request" > > <forward name ="success" path="/test/sendMail.vm" /> > </action> > ******************************************************************** > > ******************************************************************** > The web page: /test/sendMail.vm > > <html> > <head> > <title>Velocity Mail Test Page</title> > <meta http-equiv=Content-Type content="text/html; charset=utf-8"> > </head> > <body> > <p> > <a href="$link.setAction("/test/testSendingVelocityMail.do")" >Send > a test email</a> > </p> > </body> > </html> > ******************************************************************** > > ******************************************************************** > > The Action Class > > /* > * Created on Feb 17, 2006 > * > * > */ > > import java.io.StringWriter; > import java.io.UnsupportedEncodingException; > import java.util.Date; > import java.util.Properties; > > import javax.mail.Message; > import javax.mail.MessagingException; > import javax.mail.Session; > import javax.mail.Transport; > import javax.mail.internet.InternetAddress; > import javax.mail.internet.MimeMessage; > import javax.mail.internet.MimeUtility; > import javax.servlet.ServletContext; > import javax.servlet.http.HttpServletRequest; > import javax.servlet.http.HttpServletResponse; > > import org.apache.log4j.Logger; > import org.apache.struts.action.Action; > import org.apache.struts.action.ActionForm; > import org.apache.struts.action.ActionForward; > import org.apache.struts.action.ActionMapping; > import org.apache.velocity.Template; > import org.apache.velocity.VelocityContext; > import org.apache.velocity.app.VelocityEngine; > import org.apache.velocity.exception.MethodInvocationException; > import org.apache.velocity.exception.ParseErrorException; > import org.apache.velocity.exception.ResourceNotFoundException; > import org.apache.velocity.runtime.RuntimeConstants; > import org.apache.velocity.tools.struts.MessageTool; > import org.apache.velocity.tools.view.context.ChainedContext; > import org.apache.velocity.tools.view.servlet.WebappLoader; > > public class TestSendingMailAction extends Action > { > static Logger logger = Logger.getLogger(TestSendingMailAction.class); > public static final String TEST_TEMPLATE = > "mailTemplates/testTemplate.vm"; > > public ActionForward execute(ActionMapping mapping, ActionForm form, > HttpServletRequest request, HttpServletResponse response) > throws Exception > { > > MessageTool messageTool = new MessageTool(); > ServletContext servletContex = > this.getServlet().getServletContext(); > // Init the VelocityEngine Instance > VelocityEngine engine = new VelocityEngine(); > this.initializeEngine(engine); > > ChainedContext chainedContext = new ChainedContext(engine, > request, > response, servletContex); > messageTool.init(chainedContext); > > VelocityContext context = new VelocityContext(); > context.put("title", "MessageTool test mail"); > context.put("text", messageTool); > > String bodyText = createBodyText(engine, context); > > this.sendMail(bodyText); > > return (mapping.findForward("success")); > > } > > /** > * @param engine > * @param context > */ > private String createBodyText(VelocityEngine engine, VelocityContext > context) > { > StringWriter sw = new StringWriter(); > Template temp; > try > { > temp = engine.getTemplate(TEST_TEMPLATE); > temp.merge(context, sw); > String body = sw.toString(); > } > catch (ResourceNotFoundException e) > { > logger.error("resource no Found: " + e); > } > catch (ParseErrorException e) > { > logger.error("Parse Error Exception: " + e); > } > catch (MethodInvocationException e) > { > logger.error("MethodInvocationException, applying template: > " + e); > } > catch (Exception e) > { > logger.error("General Exception, applying template: " + e); > } > > return sw.toString(); > > } > > private void sendMail(String bodyText) > { > > String subject = "test mail"; > String fromName = "Jon Doe"; > String fromEmail = "[EMAIL PROTECTED]"; > String toRecipient = "Mr. Recipient"; > String toEmail = "[EMAIL PROTECTED]"; > Properties mailProps = new Properties(); > mailProps.put("mail.host", "localhost"); > mailProps.put("debug", "true"); > > Session session = Session.getDefaultInstance(mailProps, null); > MimeMessage msg = new MimeMessage(session); > > InternetAddress toAddress; > try > { > toAddress = new InternetAddress(toEmail, > MimeUtility.encodeText( > toRecipient, "UTF-8", null)); > if (fromName != null && fromEmail != null) > { > msg.setFrom(new InternetAddress(fromEmail, MimeUtility > .encodeText(fromName, "UTF-8", null))); > } > msg.setRecipient(Message.RecipientType.TO, toAddress); > msg.setSubject(MimeUtility.encodeText(subject, "UTF-8", > null)); > msg.setHeader("X-Mailer", "Mailer"); > msg.setSentDate(new Date()); > msg.setText(bodyText, "UTF-8"); > > Transport.send(msg); > > } > catch (UnsupportedEncodingException e) > { > logger.error("Problem encoding mail: " + e); > } > catch (MessagingException e) > { > logger.error("Problem sending mail: " + e); > } > > } > > /** > * @param engine > */ > private void initializeEngine(VelocityEngine engine) > { > > engine.setApplicationAttribute("javax.servlet.ServletContext", > this > .getServlet().getServletContext()); > > Properties p = new Properties(); > p.setProperty(RuntimeConstants.RESOURCE_LOADER, "webapp"); > p.setProperty("webapp.resource.loader.class", WebappLoader.class > .getName()); > > p.setProperty("resource.manager.logwhenfound", "true"); > p.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, > "org.apache.velocity.runtime.log.SimpleLog4JLogSystem"); > p.setProperty("runtime.log.logsystem.log4j.category", > "VelocityMailManager"); > try > { > engine.init(p); > } > catch (Exception e) > { > logger.error("failed to init the velocity engine: " + e); > } > > } > > } > > > Cheers, > > David > > ******************************************************************** > > > David Sperling wrote: > > >Hi- > > > >I'm trying to send mail from a Struts Action with velocity 1.5 and > >velocity-tools-1.2. > > > >The faq: > >http://wiki.apache.org/jakarta-velocity/VelocityFAQ > >Says: > > > > > >>>You can get this instance for other uses of velocity by calling > >>> > >>> > >getVelocityEngine > > > > > >>>from a subclass > >> > >> > > > >Another recommendation from this list was to: > > > > > >>>To get the VelocityEngine from getVelocityEngine(), > >>> > >>> > > > > > >So my question is how do you get the VelocityEngine from the > >getVelocityEngine()? > > > >I've spent hours on google trying to find some code that calls the > >getVelocityEngine() > >but the only thing I could come up with was a VelocityManager class from > >opensymphony. > >And the VelocityManager.java class from opensymphony is quite complex > >and has > >many dependencies. > > > > > >Cheers, > > > > > > > > > >--------------------------------------------------------------------- > >To unsubscribe, e-mail: [EMAIL PROTECTED] > >For additional commands, e-mail: [EMAIL PROTECTED] > > > > > > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > >