gpt4 book ai didi

java - 资源管理器 : unable to find resource 'emailTemplate.vm' in any resource loader

转载 作者:搜寻专家 更新时间:2023-11-01 01:07:57 24 4
gpt4 key购买 nike

我正在用 maven 在 spring 中做一个应用程序。我在 app.properties 文件中写了所有属性

文件结构是这样的

                         src/main/resource

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

我在 app.property 中给出了路径(绝对)

应用程序属性文件

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>

我的类(class)

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

谁能帮帮我..

提前致谢

最佳答案

将 velocity 与 spring 一起使用时,您会陷入一个常见的陷阱:您将模板放在一个位置,然后使用资源加载器在另一个位置搜索它们。所以你有两种常见用法:

  • 将模板放在类路径中(就像您所做的那样)并使用 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/

    模板位置更自然,但您添加了对速度工具的依赖。

并让 spring 管理依赖关系,但不通过 new 实例化 ...

关于java - 资源管理器 : unable to find resource 'emailTemplate.vm' in any resource loader,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24935896/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com