0

我已经开始查看以下线程 -

在 Spring 应用程序中从 FreeMarker 获取模板文本

我的弹簧配置 -

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

在控制器类中制作电子邮件正文 -

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();
    }

在这里,我收到一个编译器错误“方法 getTemplate(String) 未定义类型 FreeMarkerConfigurationFactoryBean”。然后我尝试使用 fmConfig 创建一个配置对象 -

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

但是开始出现运行时异常-

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

我可以有一个解决方案吗?谢谢。

4

1 回答 1

2

工厂 bean 应该返回配置类型的东西。所以 setter 应该接受这种类型。

private Configuration fmConfig_;

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

以前,我使用的是 FreeMarkerConfigurationFactoryBean 而不是 Configuration,这是错误的。

于 2010-02-18T11:51:34.343 回答