簡體   English   中英

Velocity中的ResourceNotFoundException

[英]ResourceNotFoundException in Velocity

當我使用File.exists檢查時為'true',但是當我使用Template t = ve.getTemplate(pathContent)進行讀取時; 我收到ResourceNotFoundException錯誤。 為什么要這樣

我的EmailSender類:

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;
        }
    }
}

我嘗試在我的服務班級中傳遞真實的道路

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;
        }
    }
}

謝謝

我通過在我的spring xml(applicationContext.xml)中注入class velocityEngine解決了這個問題

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

在我的EmailSender類中

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());

將我的emailFormat放在資源文件夾中(WEB-INF / classes)

謝謝閃光燈

暫無
暫無

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

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