4 Replies Latest reply on Mar 24, 2004 8:15 AM by Erez Efrati

    Velocity integration

    Wayne L. Andersen Newbie

      Is it possible to integrate Jboss and velocity, I can't find anyone that has done this successfully?

      I have a handfull of reports that my users require and it would be much easier to support them if I could use velocity templates to create the pages?

        • 1. Re: Velocity integration
          Joachim Van der Auwera Expert

          Of course you can. Just write a servlet which extends VelocityServlet (see the Velocity manual).

          Of course you will have to include the Velocity jar file in your war archive.


          • 2. Re: Velocity integration
            Wayne L. Andersen Newbie

            Have you actually done this, I have tried. I created a servlet like you said and assoicated .vm files with it.

            JBOSS would not deploy any of the .vm files in the war.

            I have not been able to find anyone who is actually doing this.

            • 3. Re: Velocity integration
              Joachim Van der Auwera Expert

              There is a magic trick of course. You have to make sure that the .vm files are loaded through the classloader. For this, you have to provide a velocity.properties files (in the WEB-INF directory).

              THe velocity.properties files looks something like this :


              class.resource.loader.description=Velocity Classpath Resource Loader

              Hope this helps you,

              • 4. Re: Velocity integration
                Erez Efrati Newbie

                Hi there, I trying to work with the Velocity and keep complaining the following error message:

                " Unable to find resource 'resources.UserLoginInfo.vm' "

                My application is contains the Logic (and EJB module) and Web (web module). the UserLoginInfo.vm is located under the resources directory.

                Here is my Emailer.java:

                 * A utility class that provides API for sending
                 * templated emails per application use case.
                 * <p>The <code>Emailer</code> uses Velocity SDK to generate
                 * the templated emails.
                 * @author Erez Efrati
                 * @version $Revision:$ $Date:$
                public class Emailer {
                 * JNDI context for JavaMail sessions.
                 private static final String JNDI_JAVA_MAIL = "java:/Mail";
                 * The content type of the HTML email messages.
                 private static final String HTML_CONTENT_TYPE = "text/html;charset=UTF-8";
                 * The user-login-info email template resource name.
                 private static final String VM_USER_LOGIN_INFO_REMINDER
                 = "resources.UserLoginInfo.vm";
                 // Velocity variable names keys
                 private static final String VMVAR_USER = "user";
                 private static final String VMVAR_SUBJECT = "subject";
                 * the engine instance
                 VelocityEngine m_engine = new VelocityEngine ();
                 // ------------------------------------------------------ Private Methods
                 * Configures the engine to use classpath to find templates
                 * @param engine the engine to configure
                 * @throws Exception thrown if an errors occures
                 private void configure (VelocityEngine engine) throws Exception
                 Properties props = new Properties ();
                 props.setProperty (VelocityEngine.RESOURCE_LOADER, "classpath");
                 props.setProperty ("classpath." + VelocityEngine.RESOURCE_LOADER + ".class",
                 m_engine.init (props);
                 * Creates a Velocity context
                 private VelocityContext createContext()
                 VelocityContext ctx = new VelocityContext ();
                 return ctx;
                 * Retreives a new JavaMail Mime message
                 * @return
                 * @throws NamingException
                 private MimeMessage createMimeMessage() throws NamingException
                 Context initCtx = new InitialContext ();
                 Session session = (Session)initCtx.lookup (JNDI_JAVA_MAIL);
                 MimeMessage message = new MimeMessage (session);
                 return message;
                 // ------------------------------------------------------ Public Methods
                 * Default constructor, configures the velocity engine.
                 * @throws Exception, if an error occures
                 public Emailer () throws Exception
                 configure (m_engine);
                 * Sends an email to the specified <code>user</code> containing
                 * his login information which includes his username and password.
                 * @param user
                 * @throws Exception
                 public void sendUserLoginInfo (User user) throws Exception
                 // retrieve the email template
                 Template template = m_engine.getTemplate (VM_USER_LOGIN_INFO_REMINDER);
                 // prepare the context and store data objects in it
                 VelocityContext ctx = createContext ();
                 ctx.put (VMVAR_USER, user);
                 // merge the template
                 StringWriter writer = new StringWriter ();
                 template.merge (ctx, writer);
                 writer.close ();
                 // retrieve the message subject and text. The subject
                 // is formulated as part of the email velocity template.
                 StringBuffer text = writer.getBuffer();
                 String subject = (String)ctx.get (VMVAR_SUBJECT);
                 // create a new message to work with
                 MimeMessage message = createMimeMessage ();
                 // retrieve the user's email address
                 String to = user.getEmail ();
                 String from = App.getSupportEmailAddress ();
                 message.setFrom (new InternetAddress (from));
                 message.addRecipient (Message.RecipientType.TO,
                 new InternetAddress (to));
                 // set the subject and content
                 message.setSubject (subject);
                 message.setContent (text, HTML_CONTENT_TYPE);
                 // Send message
                 Transport.send (message);

                Why doesn't it work? What am I missing?

                One more thing I maybe should point out as well - The emailer
                is called from my 'UserServiceBean' which is a stateless Session bean.
                I hope this is valid, is it?

                I would really appreciate any help here :)