解决方法整合-----The following has evaluated to null or missing

文章目录

  • 问题描述
  • 方法整合
    • 1添加判断是否为空
    • 2命名规则要使用驼峰命名
    • 3未写get set方法

问题描述

servlet请求转发到freemarker中,访问时报错如下

Tip: If the failing expression is known to 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. (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: #list employee_list as emp  [in template "employee.ftl" at line 68, column 17]
	----

	Java stack trace (for programmers):
	----
	freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...]
		at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:134)
		at freemarker.core.Expression.assertNonNull(Expression.java:233)
		at freemarker.core.IteratorBlock.acceptWithResult(IteratorBlock.java:103)
		at freemarker.core.IteratorBlock.accept(IteratorBlock.java:93)
		at freemarker.core.Environment.visit(Environment.java:330)
		at freemarker.core.Environment.visit(Environment.java:336)

源代码:

<#list employee_list as emp>
                <#list employee_list as emp>
                    
                        ${emp_index+1}
                        ${emp.empno?string("0")}
                        ${emp.ename}
                        ${emp.department}
                        ${emp.job}
                        ¥${emp.salary?string("0.00")}
                    

主要意思是该list为null或者不存在,我通过查阅总结了以下几个方法可以逐个排错

方法整合

1添加判断是否为空

源代码:

<#list employee_list as emp>
                <#list employee_list as emp>
                    
                        ${emp_index+1}
                        ${emp.empno?string("0")}
                        ${emp.ename}
                        ${emp.department}
                        ${emp.job}
                        ¥${emp.salary?string("0.00")}
                    

解决办法是:因为ftl是不能传入空值的。如果有空值 就会报这个的错误 :只要做一下判断就行了

<#if employee_list??>


修改之后代码

<#if employee_list??>
<#list employee_list as emp>
                <#list employee_list as emp>
                    
                        ${emp_index+1}
                        ${emp.empno?string("0")}
                        ${emp.ename}
                        ${emp.department}
                        ${emp.job}
                        ¥${emp.salary?string("0.00")}
                    


2命名规则要使用驼峰命名

one example:


解决方法整合-----The following has evaluated to null or missing_第1张图片
two example:


解决方法整合-----The following has evaluated to null or missing_第2张图片

3未写get set方法

解决方法整合-----The following has evaluated to null or missing_第3张图片
你要访问的是servlet而不是直接访问ftl文件,你servlet不传入数据如何让ftl显示数据,我真的是太蠢了,脑袋短路了 fk。

你可能感兴趣的:(java学习,1024程序员节,freemarker,servlet)