Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring 使用弹簧和速度选择模板时发生异常。它正在抛出java.lang.NullPointerException_Spring_Spring Mvc_Spring Security_Velocity - Fatal编程技术网

Spring 使用弹簧和速度选择模板时发生异常。它正在抛出java.lang.NullPointerException

Spring 使用弹簧和速度选择模板时发生异常。它正在抛出java.lang.NullPointerException,spring,spring-mvc,spring-security,velocity,Spring,Spring Mvc,Spring Security,Velocity,我正在尝试使用Spring和Velocity发送预定义的HTML电子邮件模板。它正在引发此异常: java.lang.NullPointerException: null at org.springframework.ui.velocity.VelocityEngineUtils.mergeTemplate(VelocityEngineUtils.java:53)~[spring-context-support-4.2.4.RELEASE.jar:4.2.4.RELEASE]

我正在尝试使用SpringVelocity发送预定义的HTML电子邮件模板。它正在引发此异常:

java.lang.NullPointerException: 
    null at org.springframework.ui.velocity.VelocityEngineUtils.mergeTemplate(VelocityEngineUtils.java:53)~[spring-context-support-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at org.springframework.ui.velocity.VelocityEngineUtils.mergeTemplateIntoString(VelocityEngineUtils.java:92) ~[spring-context-support-4.2.4.RELEASE.jar:4.2.4.RELEASE] 
    at com.incyyte.app.service.MailService.sendReactivationEmail(MailService.java:159) ~[classes/:na]
我的功能实现如下图所示:

 public void sendReactivationEmail(Optional<User> user) {
    Locale locale = Locale.forLanguageTag(user.get().getLangKey());
    String websiteURL = messageSource.getMessage("website.url", null, locale);
    String reactivationURL = websiteURL + messageSource.getMessage("activation.url", null, locale);
    String emailSubject = messageSource.getMessage("reactivation.subject", null, locale);
    ClassLoader classLoader = getClass().getClassLoader();
    File file = new File(classLoader.getResource("vmTemplate/Reactive_email.vm").getFile());
    Map<String, Object> emailModel = new HashMap<String, Object>();
    emailModel.put("username", user.get().getLogin());
    emailModel.put("activationCode", reactivationURL + user.get().getActivationKey());
    emailModel.put("webURL", websiteURL);
    String content = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, file.getAbsolutePath(), emailModel);
    sendEmail(user.get().getEmail(), emailSubject, content, false, true);
}
public void send邮件(可选用户){
Locale=Locale.forLanguageTag(user.get().getLangKey());
字符串websiteURL=messageSource.getMessage(“website.url”,null,locale);
String ReactionURL=websiteURL+messageSource.getMessage(“activation.url”,null,locale);
字符串emailSubject=messageSource.getMessage(“reactivation.subject”,null,区域设置);
ClassLoader ClassLoader=getClass().getClassLoader();
File File=新文件(classLoader.getResource(“vmTemplate/Reactive_email.vm”).getFile();
Map emailModel=newhashmap();
emailModel.put(“用户名”,user.get().getLogin());
emailModel.put(“activationCode”,reactivationURL+user.get().getActivationKey());
emailModel.put(“webURL”,websiteURL);
String content=VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,file.getAbsolutePath(),emailModel);
sendmail(user.get().getEmail(),emailSubject,content,false,true);
}
我看到了文件的绝对路径,它显示为

D:\v2dev\incyyte_version2\appCode\target\classes\vmTemplate\Reactive\email.vm


这里Reactive_email.vm是我的HTML格式的电子邮件模板。我认为这个异常是因为Velocity无法读取文件。请帮助我跟踪此异常以及如何修复它

之前不必将模板加载到
文件中。您可以直接使用方法
mergeTemplateIntoString
和模板的路径

像这样:

String content = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "vmTemplate/Reactive_email.vm", emailModel);
确保模板位于
src/main/resources
目录中

希望对你有帮助