簡體   English   中英

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

[英]ResourceManager : unable to find resource 'emailTemplate.vm' in any resource loader

我正在用maven在彈簧上做一個應用程序。 我在app.properties文件中寫了所有屬性

文件結構是這樣的

                         src/main/resource

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

我在app.property中給出了路徑(absloute)

app.properties文件

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

公用事業,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'。

誰能幫我..

提前致謝

使用帶彈簧的速度時,您會遇到一個常見的陷阱:將模板放在一個位置,並使用資源加載器在另一個位置搜索它們。 所以你有兩個常見的用法:

  • 將模板放在類路徑中(就像你一樣)並使用ClasspathResourceLoader

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

    它很簡單,依賴性很小,但它會強制你將模板放在類路徑中......

  • 將模板放在WEB-INF (就像對JSP一樣)並使用來自velocity工具的WebappResourceLoader

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

    對於模板位置來說更自然,但是您需要添加對速度工具的依賴性。

讓spring管理依賴關系,而不是通過new實例來實現...

我最近遇到了與karaf OSGi框架相同的問題。 在CXF資源類(在此上下文中登錄)中,您必須像這樣初始化Velocity Engine:

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()調用之后立即加載模板並不成功。

您的配置似乎正確。 如果使用“new”實例化VelocityEngine實例,請嘗試以下操作:

@Autowired
VelocityEngine velocityEngine;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM