Freemarker无法获取对象属性,报错The following has evaluated to null or missing

最近写freemarker项目的时候遇到一个错The following has evaluated to null or missing,改了一上午才改好,特意写下,以此为鉴!
歌曲实体类(问题就出在这!!!!!!!!)

import java.io.Serializable;
public class Music implements Serializable{
	private String M_Name;	 //歌名
	private String M_Singer; //歌手
	private String M_Time;   //时长
	private String M_Publish_Time;	 //发布时间
	private String M_Album;   //专辑

	public Music() {
		super();
	}
	public Music(String mName, String mSinger, String mTime,
			String mPublishTime, String mAlbum) {
		super();
		M_Name = mName;
		M_Singer = mSinger;
		M_Time = mTime;
		M_Publish_Time = mPublishTime;
		M_Album = mAlbum;
	}
	public String getM_Name() {
		return M_Name;
	}
	public void setM_Name(String mName) {
		M_Name = mName;
	}
	public String getM_Singer() {
		return M_Singer;
	}
	public void setM_Singer(String mSinger) {
		M_Singer = mSinger;
	}
	public String getM_Time() {
		return M_Time;
	}
	public void setM_Time(String mTime) {
		M_Time = mTime;
	}
	public String getM_Publish_Time() {
		return M_Publish_Time;
	}
	public void setM_Publish_Time(String mPublishTime) {
		M_Publish_Time = mPublishTime;
	}
	public String getM_Album() {
		return M_Album;
	}
	public void setM_Album(String mAlbum) {
		M_Album = mAlbum;
	}
}

ftl模板

${music.M_Name}
${music.M_Singer}
${music.M_Time}
${music.M_Publish_Time}
${music.M_Album}

测试类:

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;


public class FreeMarkerTest {
	   public static void main(String[] args) throws Exception { 
	        String dir="C:\\Users\\xyk\\Workspaces\\MyEclipse 8.6\\StaticHtml\\WebRoot\\WEB-INF\\templates";
			Configuration conf = new Configuration();
			//加载模板文件(模板的路径)
			conf.setDirectoryForTemplateLoading(new File(dir));
			// 加载模板
			Template template = conf.getTemplate("/test.ftl");
			// 定义数据	
			//多个个数据
			/*Map root = new HashMap();
			root.put("a", 123);
			root.put("b", 456);	*/
			
			//单个对象
			Map <String,Object> root  =  new HashMap<String,Object>();;
			Music music=new Music("阴天","莫文蔚","3:54","2013.10.23","莫后年代 莫文蔚20周年世纪典藏 (The Age of Moknificence)");
	       	       root.put("music", music);
	       	    
			// 定义输出
			Writer out = new FileWriter(dir + "/test.html");
			template.process(root, out);
			System.out.println("转换成功");
			out.flush();
			out.close();
	    } 
}

报错:

严重: Error executing FreeMarker template
FreeMarker template error:
The following has evaluated to null or missing:
==> music.M_Name  [in template "test.ftl" at line 1, column 3]

----
Tip: It's the step after the last dot that caused this error, not those before it.
----
Tip: If the failing expression is known to be legally refer to something that's 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: ${music.M_Name}  [in template "test.ftl" at line 1, column 1]
----

Java stack trace (for programmers):
----
freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...]
	at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:108)
	at freemarker.core.EvalUtil.coerceModelToString(EvalUtil.java:346)
	at freemarker.core.Expression.evalAndCoerceToString(Expression.java:80)
	at freemarker.core.DollarVariable.accept(DollarVariable.java:40)
	at freemarker.core.Environment.visit(Environment.java:257)
	at freemarker.core.MixedContent.accept(MixedContent.java:57)
	at freemarker.core.Environment.visit(Environment.java:257)
	at freemarker.core.Environment.process(Environment.java:235)
	at freemarker.template.Template.process(Template.java:262)
	at Test.FreeMarkerTest.main(FreeMarkerTest.java:40)
Exception in thread "main" FreeMarker template error:
The following has evaluated to null or missing:
==> music.M_Name  [in template "test.ftl" at line 1, column 3]

----
Tip: It's the step after the last dot that caused this error, not those before it.
----
Tip: If the failing expression is known to be legally refer to something that's 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: ${music.M_Name}  [in template "test.ftl" at line 1, column 1]
----

Java stack trace (for programmers):
----
freemarker.core.InvalidReferenceException: [... Exception message was already printed; see it above ...]
	at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:108)
	at freemarker.core.EvalUtil.coerceModelToString(EvalUtil.java:346)
	at freemarker.core.Expression.evalAndCoerceToString(Expression.java:80)
	at freemarker.core.DollarVariable.accept(DollarVariable.java:40)
	at freemarker.core.Environment.visit(Environment.java:257)
	at freemarker.core.MixedContent.accept(MixedContent.java:57)
	at freemarker.core.Environment.visit(Environment.java:257)
	at freemarker.core.Environment.process(Environment.java:235)
	at freemarker.template.Template.process(Template.java:262)
	at Test.FreeMarkerTest.main(FreeMarkerTest.java:40)

注意:其实就是实体类的属性命名不规范
private String M_Name; //歌名
private String M_Singer; //歌手
private String M_Time; //时长
private String M_Publish_Time; //发布时间
private String M_Album; //专辑
属性名字不符合javabean的规范!就改实体类和flt模板把首字母改为小写!M–>m
改完后代码:

import java.io.Serializable;
public class Music implements Serializable{
	private String m_Name;	 //歌名
	private String m_Singer; //歌手
	private String m_Time;   //时长
	private String m_Publish_Time;	 //发布时间
	private String m_Album;   //专辑
	
	public Music() {
		super();
	}

	public Music(String mName, String mSinger, String mTime,
			String mPublishTime, String mAlbum) {
		super();
		m_Name = mName;
		m_Singer = mSinger;
		m_Time = mTime;
		m_Publish_Time = mPublishTime;
		m_Album = mAlbum;
	}

	public String getM_Name() {
		return m_Name;
	}

	public void setM_Name(String mName) {
		m_Name = mName;
	}

	public String getM_Singer() {
		return m_Singer;
	}

	public void setM_Singer(String mSinger) {
		m_Singer = mSinger;
	}

	public String getM_Time() {
		return m_Time;
	}

	public void setM_Time(String mTime) {
		m_Time = mTime;
	}

	public String getM_Publish_Time() {
		return m_Publish_Time;
	}

	public void setM_Publish_Time(String mPublishTime) {
		m_Publish_Time = mPublishTime;
	}

	public String getM_Album() {
		return m_Album;
	}

	public void setM_Album(String mAlbum) {
		m_Album = mAlbum;
	}
	
}

ftl改后:

${music.m_Name}
${music.m_Singer}
${music.m_Time}
${music.m_Publish_Time}
${music.m_Album}

生成的html文件:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/nuoyan2018/article/details/85274873