Announcement Announcement Module
Collapse
No announcement yet.
Velocity Template Location Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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;"[email protected]"&#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
    Is there a way to figure out where Velocity is looking for the template?

    Thanks

    Comment


    • #3
      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$[email protected] edu%3e)

      Comment


      • #4
        Hello,

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

        Thanks

        Comment


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

          Comment


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

            sai

            Comment


            • #7
              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

              Comment


              • #8
                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>)

                Comment


                • #9
                  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);

                  Comment

                  Working...
                  X