2014-07-24 32 views
2

我正在使用maven在spring中執行應用程序。我寫了app.properties所有屬性文件ResourceManager:無法在任何資源加載器中找到資源'emailTemplate.vm'

文件結構是這樣的

      src/main/resource 

          |_ 
          | templates 
          |  |_mytempaltefile.vm  
          |_ app.properties   

我給在app.property路徑(absloute)

app.properties文件

template.base.path=D\:/SVN/trunk/tfbdirect/src/main/resources/templates 

utilities-spring.xml

<bean id="velocityEngine" 
    class="org.springframework.ui.velocity.VelocityEngineFactoryBean"> 
    <property name="velocityProperties"> 
     <props> 
      <prop key="resource.loader">file</prop> 
      <prop key="file.resource.loader.class"> 
       org.apache.velocity.runtime.resource.loader.FileResourceLoader 
      </prop> 
      <prop key="file.resource.loader.path">${template.base.path}</prop> 
      <prop key="file.resource.loader.cache">false</prop> 
     </props> 
     </property> 
</bean> 

我班

import java.util.HashMap; 
import java.util.Map; 
import org.apache.velocity.app.VelocityEngine; 
import org.springframework.ui.velocity.VelocityEngineUtils; 
import com.providerpay.tfbdirect.service.mail.MailSenderService; 

public class LoginServiceImpl implements ILoginService{ 

/** 
* Injected through Spring IOC 
*/ 
ILoginDAO loginDAO; 
ClaimRuleProcessServiceImpl claimRuleProcessServiceImpl; 
PlatformTransactionManager txmanager; 

//IForgotPasswordDAO forgotPasswordDAO; 

private VelocityEngine velocityEngine; 

private String appURL; 
private MailSenderService mailSenderService; 




TFBLogger log = TFBLoggerFactory.getLogger(RuleServer.class); 



public String getAppURL() { 
    return appURL; 
} 

public void setAppURL(String appURL) { 
    this.appURL = appURL; 
} 

public MailSenderService getMailSenderService() { 
    return mailSenderService; 
} 

public VelocityEngine getVelocityEngine() { 
    return velocityEngine; 
} 


public void setVelocityEngine(VelocityEngine velocityEngine) { 
    this.velocityEngine = velocityEngine; 
} 

public void setMailSenderService(MailSenderService mailSenderService) { 
    this.mailSenderService = mailSenderService; 
} 

public ILoginDAO getLoginDAO() { 
    return loginDAO; 
} 
public void setLoginDAO(ILoginDAO loginDAO) { 
    this.loginDAO = loginDAO; 
} 
public ClaimRuleProcessServiceImpl getClaimRuleProcessServiceImpl() { 
    return claimRuleProcessServiceImpl; 
} 
public void setClaimRuleProcessServiceImpl(
     ClaimRuleProcessServiceImpl claimRuleProcessServiceImpl) { 
    this.claimRuleProcessServiceImpl = claimRuleProcessServiceImpl; 
} 
public void setTxmanager(PlatformTransactionManager txmanager) { 
    this.txmanager = txmanager; 
} 



/** 
* Validates Login 
* @param loginView 
* @return 
*/ 
public boolean isValidLogin(LoginView loginView) { 

    /* create tx definition object */ 
    DefaultTransactionDefinition paramTransactionDefinition = new  DefaultTransactionDefinition(); 
    TransactionStatus status = txmanager.getTransaction(paramTransactionDefinition); 
    boolean result = false; 

    try{ 
     LoginEntity loginEntity = BeanMapper.INSTANCE.viewToEntityMapper(loginView); 
     Feedback feedback = claimRuleProcessServiceImpl.validateClaimEligibility(loginEntity); 
     log.info("Rule executed was " +feedback.getAll()); 

     for (FeedbackMessage feedbackmessaage :feedback.getAll()) 
     { 
      log.info("\n--------------"); 
      log.info(feedbackmessaage.getRuleCd()); 
      log.info(feedbackmessaage.getMessage()); 
      log.info(feedbackmessaage.getSeverity().getName()); 
      log.info("\n--------------"); 
     } 

     result = loginDAO.isValidLogin(loginEntity); 
     log.debug("result = {}", result); 

     txmanager.commit(status); 

    }catch(Exception e){ 
     txmanager.rollback(status); 
     throw new TfbException("Error occured while validating login credentials"); 
    } 

    return result; 
} 




@Autowired 
VelocityEngine velocityengine; 
public boolean mailResetLink(LoginView loginView) { 

    String toEmailAddress; 
    LoginEntity loginEntity = BeanMapper.INSTANCE.viewToEntityMapper(loginView); 

    /* getting user Email from DAO*/ 
    toEmailAddress = loginDAO.getEmailByUsername(loginEntity); 

    if(toEmailAddress != null && toEmailAddress.trim().length() > 0) 
    { 
     Map<String, Object> model = new HashMap<String, Object>(); 
     model.put("user", loginEntity); 
     model.put("appURL", appURL); 
     String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "emailTemplate.vm","UTF-8", model); 

     mailSenderService.sendMail("from mail", toEmailAddress, "Password Reset Link",body); 


    } 
    else 
    { 
     return false; 
    } 
    return true; 
} 

public boolean resetPassword(LoginView loginView) 
{ 

    LoginEntity loginEntity = BeanMapper.INSTANCE.viewToEntityMapper(loginView); 

    return loginDAO.resetPassword(loginEntity); 
} 
} 

每一件事很好,但我需要的絕對路徑更改爲相對路徑..我嘗試過很多辦法。

我試着像下面

template.base.path=/templates/ 

,但仍然得到下面的錯誤。

ResourceManager:無法在任何資源加載器中找到資源'emailTemplate.vm'。

任何一個可以幫助我..

在此先感謝

+0

你如何實例化VelocityEngine實例? –

回答

8

你落入一個常見的錯誤使用速度與春天的時候:你把你的模板在一個位置,並使用搜索他們在一個資源加載器另一個地方。所以,你有2個常見的用法:在類路徑

  • 放模板(像你一樣),並使用ClasspathResourceLoader

    resource.loader = class 
    class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader 
    

    它較少依賴關係簡單,但它迫使你把模板類路徑。 ..

  • WEB-INF劃歸模板(如你對JSP做的),並使用WebappResourceLoader從速度的工具

    resource.loader=webapp 
    webapp.resource.loader.class=org.apache.velocity.tools.view.WebappResourceLoader 
    webapp.resource.loader.path=/WEB-INF/velocity/ 
    

    模板位置更自然,但您添加了對速度工具的依賴關係。

而讓春季管理的依賴關係,而是通過new不instanciating ...

+0

謝謝你Serge Ballesta ....現在工作.. – user3691501

+0

在Spring Boot中使用任何一個選項時,你會把這些配置放到application.properties中嗎?我無法讓任何類加載器工作,似乎無關緊要地將我的模板放在哪個文件夾中。 –

0

你的配置似乎是正確的。如果您在使用「新」實例化VelocityEngine例如,嘗試以下操作:

@Autowired 
VelocityEngine velocityEngine; 
+0

如果使用spring,**避免**應該在其他bean中注入的新bean的實例化,或者在春季IOC中應該接收其他bean的實例,因爲它不會工作... –

+0

嗨serge我添加了代碼,該方法,但我只得到相同的錯誤@Autowired VelocityEngine velocityEngine; – user3691501

1

我karaf OSGi框架最近有同樣的問題。 在CXF資源類(登錄在這種情況下),你必須初始化速度引擎這樣的:

public Login() { 
    /* first, get and initialize an engine */ 
    ve = new VelocityEngine();   
    ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath"); 
    ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName()); 
    ve.init(); 
} 

然後,在Web方法,實例化模板:

/* create a context and add data */ 
    synchronized (initLock) { 
     if (loginTemplate == null) { 
      loginTemplate = ve.getTemplate("templates/login.vm"); 
     } 
    } 
    VelocityContext context = new VelocityContext(); 

不幸的是,在調用構造函數中的ve.init()後沒有立即加載模板。