freemarker.core.InvalidReferenceException:以下内容已评估为null或缺失:

时间:2017-08-27 20:34:03

标签: java freemarker

我的java类中的

看起来像这样

 Map<String,Object> model = subscriptionReportingManager.orderCreateReport();
     //  model.put("month", LocalDate.now().getMonth().plus(1).name());
       mailService.sendMail(createOrderEmailDTO, model);

我故意注释了month属性,因为我想测试freemarker如何处理它,如果对于某些reasone我没有在模型对象中填充month属性。

我的邮件服务类看起来像这样

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import com.macys.selection.reporting.email.dto.EmailInfo;

import freemarker.template.Configuration;

@Service
public class MailServiceImpl implements MailService {

    @Autowired
    public Configuration freemarkerConfigurationBean;

    @Autowired
    public JavaMailSender javaMailSender;

    @Override
    public void sendMail(EmailInfo emailInfo, Map<String, Object> model) {

    MimeMessagePreparator mimeMessagePreparator = getMessagePreparator(emailInfo, model);
    try {
        javaMailSender.send(mimeMessagePreparator);
    } catch (Exception e) {
        e.printStackTrace();
    }

    }

    private MimeMessagePreparator getMessagePreparator(EmailInfo emailInfo, Map<String, Object> model) {

    MimeMessagePreparator messagePreparator = new MimeMessagePreparator() {

        @Override
        public void prepare(MimeMessage mimeMessage) throws Exception {

        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MMM-dd");

        helper.setSubject(emailInfo.getSubject() + " " + now.format(formatter));
        helper.setFrom(emailInfo.getFrom());
        helper.setTo(InternetAddress.parse(emailInfo.getTo()));

        String text = geFreeMarkerTemplateContent(model, emailInfo.getTemplateName());

        helper.setText(text, true);
        System.out.println("Template content : " + text);
        }
    };

    return messagePreparator;
    }

    public String geFreeMarkerTemplateContent(Map<String, Object> model, String templateName) {
    StringBuffer content = new StringBuffer();
    try {
        content.append(FreeMarkerTemplateUtils.processTemplateIntoString(freemarkerConfigurationBean.getTemplate(templateName), model));
        return content.toString();
    } catch (Exception e) {
        System.out.println("Exception occured while processing fmtemplate:" + e.getMessage());
    }
    return "";
    }

}

freemarker html看起来像这样

<tr>
                <td style="width: 8%;">${month}!'hello'</td>
   </tr>

发生了什么事情,因为我已经评论了月份属性,它没有被添加到模型对象中,当我查询月份属性的freemarker模板时,我收到错误

freemarker.core.InvalidReferenceException: The following has evaluated to null or missing:
==> month  [in template "createOrderTemplate.ftl" at line 11, column 96]

----
Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
----

----
FTL stack trace ("~" means nesting-related):
    - Failed at: ${month}  [in template "createOrderTemplate.ftl" at line 11, column 94]
----
    at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:131) ~[freemarker-2.3.23.jar:2.3.23]
    at freemarker.core.EvalUtil.coerceModelToString(EvalUtil.java:355) ~[freemarker-2.3.23.jar:2.3.23]
    at freemarker.core.Expression.evalAndCoerceToString(Expression.java:82) ~[freemarker-2.3.23.jar:2.3.23]
    at freemarker.core.DollarVariable.accept(DollarVariable.java:41) ~[freemarker-2.3.23.jar:2.3.23]
    at freemarker.core.Environment.visit(Environment.java:324) [freemarker-2.3.23.jar:2.3.23]
    at freemarker.core.MixedContent.accept(MixedContent.java:54) [freemarker-2.3.23.jar:2.3.23]
    at freemarker.core.Environment.visit(Environment.java:324) [freemarker-2.3.23.jar:2.3.23]
    at freemarker.core.Environment.process(Environment.java:302) [freemarker-2.3.23.jar:2.3.23]
    at freemarker.template.Template.process(Template.java:325) [freemarker-2.3.23.jar:2.3.23]
    at org.springframework.ui.freemarker.FreeMarkerTemplateUtils.processTemplateIntoString(FreeMarkerTemplateUtils.java:50) [spring-context-support-4.3.10.RELEASE.jar:4.3.10.RELEASE]

我已经尝试过stackoverflow中的其他链接,但问题仍然存在。任何想法?

我已经尝试过以下

<tr>
                <td style="width: 8%;">${month!'hello'}</td>
</tr>

我仍然得到同样的错误

0 个答案:

没有答案