Java ResourceNotFound速度异常
当我使用File.exists检查时为“true”,但当我使用模板t=ve.getTemplate(pathContent)读取时为“true”;我得到ResourceNotFoundException错误。为什么会这样, 我的EmailSender类:Java ResourceNotFound速度异常,java,velocity,Java,Velocity,当我使用File.exists检查时为“true”,但当我使用模板t=ve.getTemplate(pathContent)读取时为“true”;我得到ResourceNotFoundException错误。为什么会这样, 我的EmailSender类: public class EmailSender { public static boolean send(String to, String newUsername, String newPassword, String conten
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;
}
}
}
谢谢我通过注入类velocityEngine解决了这个问题 在我的SpringXML(applicationContext.xml)中 将我的电子邮件格式放入资源文件夹(WEB-INF/classes)
谢谢flash您看过什么?什么是pathContent?绝对路径?您是否正确配置了velocity引擎?放更多的细节。我通过了一个绝对路径,我已经编辑了我的问题。在这种情况下,我尝试只写内容
<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>
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());