2014-07-24 34 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()后没有立即加载模板。