Results 1 to 9 of 9

Thread: Velocity Template Location

  1. #1
    Join Date
    Feb 2005
    Posts
    8

    Default Velocity Template Location

    Hello,

    I am getting a NullPointerException with VelocityEngineUtils.
    org.springframework.ui.velocity.VelocityEngineUtil s.mergeTemplate(VelocityEngineUtils.java:55)
    I am guessing it can't find the template.

    I am trying to merge a template so I can email the result.

    Here is my applicationContext.xml snippet:

    Code:
      <!-- Mail Confiruguration -->
      <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
        <property name="resourceLoaderPath"><value>/WEB-INF/templates</value></property>
      </bean>
      
      <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host"><value>mail.xxx.org</value></property>
        <property name="username"><value>xxx</value></property>
        <property name="password"><value>xxx</value></property>
      </bean>
      
      <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage"/>
      
      <bean id="managerTarget" class="com.xxx.MailSender">
        <property name="mailSender"><ref bean="mailSender"/></property>
        <property name="message"><ref bean="mailMessage"/></property>
        <property name="velocityEngine"><ref bean="velocityEngine"/></property>    
      </bean>
    And the relevant Java Code:
    Code:
            SimpleMailMessage msg = new SimpleMailMessage&#40;&#41;;
            msg.setFrom&#40;"lostpassword@xxx.org"&#41;;
            msg.setSubject&#40;"Lost Password Request"&#41;;
            msg.setTo&#40;reg.getEmail&#40;&#41;&#41;;
            
            Map<String, Registrant> model = new HashMap<String, Registrant>&#40;&#41;;
            model.put&#40;"registrant", reg&#41;;
    
            String result = null;
            
            try
            &#123;
                result = VelocityEngineUtils.mergeTemplateIntoString&#40;velocityEngine,
                        "lostPassword.vm", model&#41;;
            &#125; 
            catch&#40;VelocityException e&#41;
            &#123;
                log.error&#40;e&#41;;
            &#125;
            
            msg.setText&#40;result&#41;;
    
            try
            &#123;
                mailSender.send&#40;msg&#41;;
            &#125; 
            catch &#40;MailException ex&#41;
            &#123;
                log.error&#40;ex&#41;;
            &#125;

  2. #2
    Join Date
    Feb 2005
    Posts
    8

    Default

    Is there a way to figure out where Velocity is looking for the template?

    Thanks

  3. #3
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    Try turning the logging on Velocity package (see this thread from the mailing list as a starting point: http://mail-archives.eu.apache.org/mod_mbox/jakarta-velocity-user/200501.mbox/%3c000001c4f71c$0421de90$310d3b80@mrl.cs.columbia. edu%3e)
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  4. #4
    Join Date
    Feb 2005
    Posts
    8

    Default

    Hello,

    Still can't figure this out, Logging shows the velocity engine is started properly, do i need any other configuration?

    Thanks

  5. #5
    Join Date
    Dec 2006
    Posts
    16

    Default

    Did you find out a solution? I run into the same problem.
    Thanks.
    Denis

  6. #6

    Default

    I encountered the same problem, too. Is there anyone can figure it out?
    I'll be waiting.....

    sai

  7. #7
    Join Date
    Dec 2006
    Posts
    16

    Default My solution

    1. applicationContext:
    <bean id="velocityEngine"
    class="org.springframework.ui.velocity.VelocityEng ineFactoryBean">
    <property name="velocityProperties">
    <props>
    <prop key="resource.loader">class</prop>
    <prop key="class.resource.loader.class">
    org.apache.velocity.runtime.resource.loader.Classp athResourceLoader
    </prop>
    </props>
    </property>
    </bean>
    2. Code:
    text = VelocityEngineUtils.mergeTemplateIntoString(veloci tyEngine,
    "velocity/templates/xyz.vm", model);
    3. deployment directories under zyx.jar:
    /applicationContext.xml
    /velocity/templates/xyz.vm

  8. #8

    Default template location in web-inf

    <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEng ineFactoryBean">
    <property name="resourceLoaderPath"><value>/WEB-INF/velocity</value></property>
    </bean>


    and in controller

    String text = VelocityEngineUtils.mergeTemplateIntoString(veloci tyEngine, "shareImageTemplate.vm", model);

    note your templates (.vm) under folder (/WEB-INF/velocity) as you are wrote in servlet.xml (<property name="resourceLoaderPath"><value>/WEB-INF/velocity</value>)

  9. #9

    Default template location in any location

    1. In appContext.xml define the location from where you want to pick up Velocity template.
    <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEng ineFactoryBean">
    <property name="velocityProperties">
    <props>
    <prop key="resource.loader">file</prop>
    <prop key="file.resource.loader.class">org.apache.veloci ty.runtime.resource.loader.FileResourceLoader
    </prop>
    <prop key="file.resource.loader.path">C:/Email/Velocity</prop>
    <prop key="file.resource.loader.cache">true</prop>
    <prop key="file.resource.loader.modificationCheckInterva l">0</prop>

    </props>
    </property>
    </bean>

    2. In AppProps.prop add Velocity template Name ( this will picked from folder given above).
    htmlTemplate = emailBody.vm
    3. Load AppProps.prop in classPath and create a Bean with getters and setters for this prop.
    <contextroperty-placeholder file:C:/Email/AppProps.properties" />
    Bean def for applicationProperties
    <bean id="applicationProperties" class="com.*.*.ApplicationProperties">
    <property name="htmlTemplate" value="${htmlTemplate}"></property>
    .........................
    </bean>

    4. In Java code
    @Autowired
    private ApplicationProperties applicationProperties = null;

    // get the html template and merge it with the merge objects
    StringWriter htmlBody = new StringWriter();
    VelocityEngineUtils.mergeTemplate(velocityEngine,
    applicationProperties.getHtmlTemplate(),templateVa riablesMap, htmlBody);

Similar Threads

  1. Replies: 9
    Last Post: Sep 5th, 2006, 06:50 AM
  2. Replies: 3
    Last Post: Feb 9th, 2006, 12:32 AM
  3. FreeMarker vs Velocity
    By Martin Kersten in forum Architecture
    Replies: 8
    Last Post: May 30th, 2005, 09:21 AM
  4. Velocity OutOfMemoryError during UnitTests
    By camach in forum Container
    Replies: 2
    Last Post: May 26th, 2005, 05:28 PM
  5. Velocity templates need help
    By kelvint in forum Web
    Replies: 1
    Last Post: Nov 24th, 2004, 03:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •