简体   繁体   中英

ResourceNotFoundException in Velocity

it was 'true' when i check using File.exists, but when i read using Template t = ve.getTemplate(pathContent); i get ResourceNotFoundException error. why be like that,

My EmailSender class :

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)

<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

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)

Thank you flash

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM