【问题标题】:Creating your own custom helpers with Freemarker?使用 Freemarker 创建您自己的自定义助手?
【发布时间】:2011-03-09 05:41:47
【问题描述】:

从我的控制器中,我设置了我的模型和视图:

ModelAndView mav = new ModelAndView();

mav.setView("index");

mav.addObject("user", user);
mav.addObject("someCollection", someCollection);

return mav;

现在我想创建一个辅助类型对象,它将 someCollection 和用户对象作为参数。

我的辅助函数会输出一些 HTML 等,这可能吗?

【问题讨论】:

    标签: java spring spring-mvc freemarker


    【解决方案1】:

    您可以使用FTLJava 编写宏和指令,将它们公开给您的模板并以与通常使用内置宏/指令相同的方式调用它们。

    【讨论】:

      【解决方案2】:

      没有什么能阻止你放置任何 Java 对象,例如一个辅助实例,到模型,然后使用如下语法调用它的方法:${helper.myMethod(arg)}

      【讨论】:

        【解决方案3】:
        /**
         * Add logotype logotype1AsBase64.
         * @return
         * @throws IOException 
        @ModelAttribute("logotype1AsBase64")
        public String getLogotype() 
                throws IOException {
            return logotypeService.getLogotype();
        }
        */
        
        @ModelAttribute
        public void addAttributes(Model model) throws IOException {
            //...
            model.addAttribute("logotype1AsBase64", logotypeCacheServ.getImage("logotypeInEnglish1From20190101.png"));
            //...
        }
        

        然后使用它:

        <img src="<#if locale == 'specificLocale'>${logotype1AsBase64}></#if>" alt="description for picture">
        

        如果设置是这样的话,model 属性可以包含除 base64 之外的更多 html。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-10-20
          • 2016-06-09
          • 2011-12-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-04
          • 1970-01-01
          相关资源
          最近更新 更多