views:

372

answers:

1

I have started by looking in following thread -

http://stackoverflow.com/questions/230763/getting-template-text-from-freemarker-in-spring-app

My spring configuration -

<bean id="fmConfig" class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean"> 
    <property name="templateLoaderPath" value="/WEB-INF/templates"></property> 
</bean>

<bean name="/email.do" class="com.email.web.controller.EmailController">
  <property name="formView" value="email"/>
  <property name="successView" value="email_thanks"/>
  <property name="commandName" value="emailForm"/>
  <property name="commandClass" value="com.email.bean.EmailForm"/>
  <property name="bindOnNewForm" value="true"/>
  <property name="fmConfig" ref="fmConfig"/>
</bean>

Making email body in controller class as -

private String makeBody(EmailForm form) {
      StringBuffer content = new StringBuffer(); 

      try { 
          content.append(FreeMarkerTemplateUtils.processTemplateIntoString( 
              fmConfig_.getTemplate("email_default_TM.txt"),form)); 
      } catch (IOException e) {           
      } catch (TemplateException e) { 
      } 
      return content.toString();
    }

Here, I was getting a compiler error "The method getTemplate(String) is undefined for the type FreeMarkerConfigurationFactoryBean". Then I have tried to create a Configuration object using fmConfig as -

try { 
       content.append(FreeMarkerTemplateUtils.processTemplateIntoString( 
       fmConfig_.createConfiguration().getTemplate("email_default_TM.txt"),form)); 
    } catch (IOException e) {           
    } catch (TemplateException e) { 

But started getting a run time exception -

org.springframework.beans.factory.BeanCreationException: Error creating bean with name '/email-a-friend.do' defined in ServletContext resource [/WEB-INF/springapps-servlet.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [freemarker.template.Configuration] to required type [org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean] for property 'fmConfig'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [freemarker.template.Configuration] to required type [org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean] for property 'fmConfig': no matching editors or conversion strategy found

Can I have a solution? Thanks.

A: 

The factory bean is supposed to return something of type Configuration. So the setter should accept that type.

private Configuration fmConfig_;

public void setFmConfig(Configuration fmConfig) {
        fmConfig_ = fmConfig;
    }

Previously, I was using FreeMarkerConfigurationFactoryBean instead of Configuration which was wrong.

Saurabh
So have you answered your own question?
skaffman