[freemarker]使用freemarker生成父子关系树抛异常

需要生成一个菜单树,使用了freemarker,由于采用了tiles2,在和freemarker整合的时候出现了问题,所以直接使用freemarker 2.3.15将模板通过template.process方法产生html code显示到前端,主要代码如下:
[code="java"]
//产生freemarker config
package project.util.freemarker;

import freemarker.cache.ClassTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.TemplateExceptionHandler;

public class FreeMarkerService {
private static Configuration cfg = new Configuration();

static {
    //设置模版加载的路径,相对于FreemarkerService类路径下的templates路径
    cfg.setTemplateLoader(new ClassTemplateLoader(FreeMarkerService.class,
            "ftl"));
    //设置对象包装器
    cfg.setObjectWrapper(new DefaultObjectWrapper());
    //设置默认编码
    cfg.setDefaultEncoding("UTF-8");
    //设置异常处理器
    cfg.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);
}

public static Configuration getConfiguration() {
    return cfg;
}

}
[/code]

[code="java"]
//freemarker工具类,将数据和模板合成html
package project.util.freemarker

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class FreeMarkerUtil {
@SuppressWarnings("rawtypes")
public static String dataToString(Map data,String templatePath) throws IOException,TemplateException{
if(data == null){
return null;
}

    Writer out = null;

    Configuration cfg = FreeMarkerService.getConfiguration();
    Template template = cfg.getTemplate(templatePath);
    out = new StringWriter();
    template.process(data,out);
    return out.toString();
}

}
[/code]

freemarker 模板
[code="html"]
<#-- 定义宏buildNode -->
<#macro buildNode child parent>




style="text-decoration:line-through"
</#if>
>
${parent.menuName}


<#if child?? && child?size gt 0>

    <#list child as t>
    <#-- 递归调用宏buildNode -->
    <@buildNode child=t.children parent=t/>
    </#list>

</#if>

</#macro>
    <ul>
        <@buildNode child=rootMenu.children parent=rootMenu/>
    </ul>

[/code]

[code="java"]
//实体类
package project.bean;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

@Entity
@Table(name="RBAC_MENUS")
public class Menu {
private int menuId;
private String menuName;
private String description;
private Menu parent;
private List

children;
private List functions;
private String isDel;
public Menu(){}

@Id
@GeneratedValue(generator = "paymentableGenerator")     
@GenericGenerator(name = "paymentableGenerator", strategy = "sequence", parameters= {@Parameter(name="sequence",value="RBAC_MENU_SEQ")})
@Column(name="MENU_ID")
public int getMenuId() {
    return menuId;
}

public void setMenuId(int menuId) {
    this.menuId = menuId;
}

@Column(name="MENU_NAME")
public String getMenuName() {
    return menuName;
}

public void setMenuName(String menuName) {
    this.menuName = menuName;
}

@ManyToOne(cascade = {CascadeType.MERGE,CascadeType.REFRESH }, optional = true)  
@JoinColumn(name="PARENT_MENU_ID")
public Menu getParent() {
    return parent;
}

public void setParent(Menu parent) {
    this.parent = parent;
}

@OneToMany(cascade = { CascadeType.REFRESH, CascadeType.PERSIST,
        CascadeType.MERGE, CascadeType.REMOVE },mappedBy ="parent")
public List<Menu> getChildren() {
    return children;
}

public void setChildren(List<Menu> children) {
    this.children = children;
}

public void addChildren(Menu menu){
    if(this.children == null){
        this.children = new ArrayList<Menu>();
    }
    this.children.add(menu);
}

@OneToMany(cascade = { CascadeType.REFRESH, CascadeType.PERSIST,CascadeType.MERGE, CascadeType.REMOVE },mappedBy ="menu")
public List<Function> getFunctions() {
    return functions;
}

public void setFunctions(List<Function> functions) {
    this.functions = functions;
}

@Column(name="IS_DEL")
public String getIsDel() {
    return isDel;
}

public void setIsDel(String isDel) {
    this.isDel = isDel;
}

@Column(name="description")
public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

}
[/code]

[code="java"]
//Struts action
package project.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import project.bean.Menu;
import project.service.MenuService;
import project.util.freemarker.FreeMarkerUtil;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

public class MenuAction extends ActionSupport {
...
@Resource private MenuService menuService;

    private String menuTree;

    public String getMenuTree(){
    return menuTree;
}
   ...
@SuppressWarnings({ "rawtypes", "unchecked" })
public String initMenu(){

    Menu rootMenu = menuService.getRootMenu();

    try{
        Map freemarkerData = new HashMap();
        freemarkerData.put("rootMenu", rootMenu);
        freemarkerData.put("webRoot", ServletActionContext.getServletContext().getContextPath());
        menuTree = FreeMarkerUtil.dataToString(freemarkerData, "menumgmt.ftl");
    }
    catch(Exception e){
        e.printStackTrace();
    }

    return ActionSupport.SUCCESS;
}
    ...

[/code]

[code="java"]
//Menu Service实现类
public class MenuServiceImpl implements MenuService {
@Resource private SessionFactory sessionFactory;
...

//得到根菜单
@SuppressWarnings("unchecked")
@Override
public Menu getRootMenu() {
    List<Menu> menus = sessionFactory.getCurrentSession().createQuery("from Menu m where m.parent is null").list();

    if(menus != null && menus.size()>0)
        return menus.get(0);

    return null;
}

//插入菜单
@Override
public void insertMenu(Menu menu) {
    sessionFactory.getCurrentSession().persist(menu);
}
    ...

}
[/code]

jsp代码
[code="html"]
...


<!--添加层开始-->

菜单列表


${menuTree}



...
<br> $(document).ready(function(){<br> $(&quot;#treeContainer ul&quot;).Treeview();<br> $(&quot;#treeContainer&quot;).show(500);<br> });<br>
[/code]

异常信息
...
Hibernate:
select
menu0_.MENU_ID as MENU1_2_,
menu0_.description as descript2_2_,
menu0_.IS_DEL as IS3_2_,
menu0_.MENU_NAME as MENU4_2_,
menu0_.PARENT_MENU_ID as PARENT5_2_
from
RBAC_MENUS menu0_
where
menu0_.PARENT_MENU_ID is null
Hibernate:
select
children0_.PARENT_MENU_ID as PARENT5_1_,
children0_.MENU_ID as MENU1_1_,
children0_.MENU_ID as MENU1_2_0_,
children0_.description as descript2_2_0_,
children0_.IS_DEL as IS3_2_0_,
children0_.MENU_NAME as MENU4_2_0_,
children0_.PARENT_MENU_ID as PARENT5_2_0_
from
RBAC_MENUS children0_
where
children0_.PARENT_MENU_ID=?
[ERROR] [freemarker.log.Log4JLoggerFactory$Log4JLogger.error(Log4JLoggerFactory.java:96)] er.runtime -

Expression parent.parent is undefined on line 3, column 43 in menumgmt.ftl.

The problematic instruction:

==> ${parent.parent.menuId} [on line 3, column 41 in menumgmt.ftl]

in user-directive buildNode [on line 27, column 21 in menumgmt.ftl]

Java backtrace for programmers:

freemarker.core.InvalidReferenceException: Expression parent.parent is undefined on line 3, column 43 in menumgmt.ftl.
at freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:124)
at freemarker.core.TemplateObject.invalidTypeException(TemplateObject.java:134)
at freemarker.core.Dot._getAsTemplateModel(Dot.java:78)
at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
at freemarker.core.Expression.getStringValue(Expression.java:93)
at freemarker.core.DollarVariable.accept(DollarVariable.java:76)
at freemarker.core.Environment.visit(Environment.java:209)
at freemarker.core.MixedContent.accept(MixedContent.java:92)
at freemarker.core.Environment.visit(Environment.java:209)
at freemarker.core.Macro$Context.runMacro(Macro.java:168)
at freemarker.core.Environment.visit(Environment.java:602)
at freemarker.core.UnifiedCall.accept(UnifiedCall.java:106)
at freemarker.core.Environment.visit(Environment.java:209)
at freemarker.core.MixedContent.accept(MixedContent.java:92)
at freemarker.core.Environment.visit(Environment.java:209)
at freemarker.core.Environment.process(Environment.java:189)
at freemarker.template.Template.process(Template.java:237)
at project.rbac.util.freemarker.FreeMarkerUtil.dataToString(FreeMarkerUtil.java:24)
at project.rbac.web.MenuAction.initMenu(MenuAction.java:91)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:404)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:267)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:229)
at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:221)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:150)
at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:48)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:123)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:184)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:105)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:83)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:207)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:74)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:127)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at org.apache.struts2.interceptor.ProfilingActivationInterceptor.intercept(ProfilingActivationInterceptor.java:107)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:206)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:115)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:143)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:121)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:170)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:123)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224)
at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223)
at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221)
at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:504)
at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:422)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)
Hibernate:
select
children0_.PARENT_MENU_ID as PARENT5_1_,
children0_.MENU_ID as MENU1_1_,
children0_.MENU_ID as MENU1_2_0_,
children0_.description as descript2_2_0_,
children0_.IS_DEL as IS3_2_0_,
children0_.MENU_NAME as MENU4_2_0_,
children0_.PARENT_MENU_ID as PARENT5_2_0_
from
RBAC_MENUS children0_
where
children0_.PARENT_MENU_ID=?
Hibernate:
select
children0_.PARENT_MENU_ID as PARENT5_1_,
children0_.MENU_ID as MENU1_1_,
children0_.MENU_ID as MENU1_2_0_,
children0_.description as descript2_2_0_,
children0_.IS_DEL as IS3_2_0_,
children0_.MENU_NAME as MENU4_2_0_,
children0_.PARENT_MENU_ID as PARENT5_2_0_
from
RBAC_MENUS children0_
where
children0_.PARENT_MENU_ID=?
...

菜单能够正常展现,但是这个异常为什么发生呢,我还碰到一个问题就是当我选择一个菜单项,并且插入一个子菜单的时候,发生一个异常,并且页面也无法展现最新插入的那个菜单,需要刷新一下才能看到新插入的菜单

程序界面如下:

[img]http://hiphotos.baidu.com/lucky_sonic/pic/item/c81382cf5b0ce869f9dc612a.jpg[/img]

查看全部
Johnson_Cheng
Johnson_Cheng
2011/03/07 21:39
  • it技术
  • 互联网问答
  • IT行业问题
  • 编程语言问答
  • 计算机技术
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

0个回复