2012-04-30 14 views
0

File.existsを使用してチェックしたときに 'true'でしたが、テンプレートt = ve.getTemplate(pathContent)を使用して読み取ったときはtrueでした。私はResourceNotFoundExceptionエラーを取得します。私はクラスvelocityEngineを注入することによって、それを解決するVelocityでResourceNotFoundExceptionが発生しました

public class UserServiceImpl { 
    public ResultDto sendNotifEmail(Users user) { 
     try{ 
      String emailFormatPath = context.getRealPath("emailFormat"); 
      if(!EmailSender.send(user.getEmail(), user.getUsername(), password, emailFormatPath+"\\emailFormat.vm")){ 

      }   

     } catch (Exception e) { 
      e.printStackTrace(); 
      return false; 
     } 
    } 
} 

はあなたに

+0

を点滅あなたは[ベロシティリソースローダー](HTTPを見てきました://velocity.apache.org/engine/releases/velocity-1.5/developer-guide.html#configuring_resource_loaders)? – flash

+0

pathContentとは何ですか?絶対パス?ベロシティエンジンを正しく設定しましたか?詳細を記入してください。 –

+0

私は絶対パスを渡す、私は私の質問を編集しました。この場合、私はちょうどそのコンテンツを書くことを試みる –

答えて

0

をありがとう、私は私のサービス・クラスで実際のパスを渡すためにしようと

public class EmailSender { 
    public static boolean send(String to, String newUsername, String newPassword, String contentPath) { 
     try{ 
      ....................... 
      VelocityEngine ve = new VelocityEngine(); 
      ve.init(); 
      VelocityContext context = new VelocityContext(); 
      context.put("username", newUsername); 
      context.put("password", newPassword); 

      Template t = ve.getTemplate(contentPath); 
      StringWriter writer = new StringWriter(); 
      t.merge(context, writer); 
      System.out.println(writer.toString()); 
      return true; 

     } catch (Exception e) { 
      e.printStackTrace(); 
      return false; 
     } 
    } 
} 

:なぜ

私の電子メールの送信のクラス、そのようなこと my spring xml(applicationContext.xml)

<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean"> 
    <property name="velocityProperties"> 
     <props> 
      <prop key="resource.loader">class</prop> 
      <prop key="class.resource.loader.class"> 
       org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader 
      </prop> 
     </props> 
    </property> 
</bean> 

<bean id="emailSender" class="com.satunol.pms.helper.EmailSender"> 
    <property name="velocityEngine"><ref bean="velocityEngine"/></property> 
</bean> 
リソースフォルダに入れ私のemailFormatで 私の電子メールの送信のクラスで

private VelocityEngine velocityEngine; 
public void setVelocityEngine(VelocityEngine velocityEngine) { 
    this.velocityEngine = velocityEngine; 
} 
...................... 
Template t = velocityEngine.getTemplate("emailFormat.vm"); 
StringWriter writer = new StringWriter(); 
System.out.println(writer.toString()); 

に(WEB-INF /クラス)

ありがとう

関連する問題