简体   繁体   English

Velocity中的ResourceNotFoundException

[英]ResourceNotFoundException in Velocity

it was 'true' when i check using File.exists, but when i read using Template t = ve.getTemplate(pathContent); 当我使用File.exists检查时为'true',但是当我使用Template t = ve.getTemplate(pathContent)进行读取时; i get ResourceNotFoundException error. 我收到ResourceNotFoundException错误。 why be like that, 为什么要这样

My EmailSender class : 我的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;
        }
    }
}

i try to pass real path in my service class 我尝试在我的服务班级中传递真实的道路

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

Thank you 谢谢

i solve it by injecting class velocityEngine in my spring xml (applicationContext.xml) 我通过在我的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>

in my EmailSender class 在我的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());

in put my emailFormat in resources folder (WEB-INF/classes) 将我的emailFormat放在资源文件夹中(WEB-INF / classes)

Thank you flash 谢谢闪光灯

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 IntelliJ IDEA中的Velocity ResourceNotFoundException - Velocity ResourceNotFoundException in IntelliJ IDEA ResourceNotFoundException:用于spark-java和velocity - ResourceNotFoundException: for spark-java and velocity 快速加载模板时出现ResourceNotFoundException - ResourceNotFoundException while loading a template in velocity 速度问题-与Spring MVC一起使用时出现ResourceNotFoundException - Problem with Velocity - ResourceNotFoundException when using with Spring MVC Spark + Velocity:无法加载资源(ResourceNotFoundException) - Spark + Velocity: Unable to load resources (ResourceNotFoundException) org.apache.velocity.exception.ResourceNotFoundException - org.apache.velocity.exception.ResourceNotFoundException Apache velocity:org.apache.velocity.exception.ResourceNotFoundException - Apache velocity: org.apache.velocity.exception.ResourceNotFoundException Velocity模板-线程“ main”中的异常org.apache.velocity.exception.ResourceNotFoundException:无法找到资源 - Velocity Template - Exception in thread “main” org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource org.apache.velocity.exception.ResourceNotFoundException:无法找到资源“模板/电子邮件/test.vm” - org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource 'templates/email/test.vm' 如何在模式生成期间修复“org.apache.velocity.exception.ResourceNotFoundException” - How to fix “org.apache.velocity.exception.ResourceNotFoundException” during schema generating
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM