sdywcd
翼海同行
2012-01-03 20:40
浏览 296
已采纳

struts2返回freemarker显示FreeMarker template error!

情况:action代码如下

Map<String,Object>map=new HashMap<String,Object>();
                map.put("deliveraddress", null);
                //获取导航数据
                map.put("siteNavigationList", this.getCreateHeaderTemplate().findIndexSiteNavigation());
                //获取商城基本数据
                map.put("jshopbasicinfo", this.getCreateHeaderTemplate().findJshopbasicInfo());
                
                //获取页脚分类数据
                map.put("footcategory", this.getCreatefootertemplate().findFooterCateogyrT());
                //获取页脚文章数据
                map.put("footerarticle", this.getCreatefootertemplate().findFooterArticle());
                //this.getCreateHeaderTemplate().getBasePath(map);
                fc.init("WEB-INF/theme/default/shop/deliveraddress.ftl", map);
                
                return Action.SUCCESS;

生成方法如下

public void init(String ftl,Map<String,Object>data) throws TemplateException, IOException{
        Configuration configuration=new Configuration();
        configuration.setServletContextForTemplateLoading(ServletActionContext.getServletContext(), "/");
        configuration.setEncoding(Locale.CHINA, "UTF-8");
        
        Template template=configuration.getTemplate(ftl);   
        template.setEncoding("UTF-8");
        ActionContext ctx = ActionContext.getContext();       
        HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);  
        
        response.setContentType("text/html;charset="+template.getEncoding());
        Writer out=response.getWriter();
        template.process(data, out);
        
    }

struts.xml如下

<action name="GetUserDeliverAddressForUserCenter" class="FrontDeliverAddressAction"
            method="GetUserDeliverAddressForUserCenter">
            <!-- <result name="success">/usercenter/businessmag/mydeliveraddress.jsp
            </result> -->
            <result name="success" type="freemarker">WEB-INF/theme/default/shop/deliveraddress.ftl
            </result>
            <result name="input">/usercenter/login.jsp</result>
            <interceptor-ref name="defaultStack"></interceptor-ref>
        </action>

freemarker错误如下

Expression jshopbasicinfo is undefined on line 8, column 15 in WEB-INF/theme/default/shop/deliveraddress.ftl.
The problematic instruction:
----------
==> ${(jshopbasicinfo.jshopname)} [on line 8, column 12 in WEB-INF/theme/default/shop/deliveraddress.ftl]
----------

Java backtrace for programmers:
----------
freemarker.core.InvalidReferenceException: Expression jshopbasicinfo is undefined on line 8, column 15 in WEB-INF/theme/default/shop/deliveraddress.ftl.
    at freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:125)
    at freemarker.core.TemplateObject.invalidTypeException(TemplateObject.java:135)
    at freemarker.core.Dot._getAsTemplateModel(Dot.java:78)
    at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
    at freemarker.core.ParentheticalExpression._getAsTemplateModel(ParentheticalExpression.java:75)
    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:221)
    at freemarker.core.MixedContent.accept(MixedContent.java:92)
    at freemarker.core.Environment.visit(Environment.java:221)
    at freemarker.core.Environment.process(Environment.java:199)
    at freemarker.template.Template.process(Template.java:237)
    at org.apache.struts2.views.freemarker.FreemarkerResult.doExecute(FreemarkerResult.java:187)
    at org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186)
    at com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:362)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:266)
    at org.apache.struts2.json.JSONInterceptor.intercept(JSONInterceptor.java:179)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
    at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.jshop.interceptor.impl.AuthorityLogininterceptor.doIntercept(AuthorityLogininterceptor.java:41)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
    at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
    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.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    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:128)
    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:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:619)

意思是说jshopbasicinfo没有定义,但是页面上已经显示出该有的数据了。但还是爆这个模板错误。我ftl中没有使用strtus2的标签,网上说如果使用了addactionerror这个方法可能报错,但是我没有使用啊。不知道怎么弄的。请教各位了~~~~

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • renpeng301
    renpeng301 2012-01-05 16:58
    已采纳

    [code="java"]你把要显示的字段 做以下空的处理 就是你的那个jshopbasicinfo 字段[/code]

    点赞 评论
  • iblike
    iblike 2012-01-04 09:40

    很可能传入的jshopbasicinfo 是null的,检查下

    点赞 评论
  • renpeng301
    renpeng301 2012-01-05 09:12

    [code="java"]
    deliveraddress.ftl中
    jshopbasicinfo 没定义

    也面上jshopbasicinfo 至显示一条数据吗?
    还是jshopbasicinfo 是存放了很多数据
    其中包含空的 你检查下这个···
    [/code]

    点赞 评论
  • renpeng301
    renpeng301 2012-01-05 09:15

    [code="java"]一般ftl 有null值就出现这个错误啊[/code]

    在ftl文件中判断为空,去掉这个错误。
    [#if ctt.contentType??][${ctt.contentType.name} - ${ctt.ctgName}] [#else][${ctt.ctgName}][/#if]
    “??”判断是否为空。

    贴下你的ftl

    点赞 评论

相关推荐