标签: freemarker

在java中加载freemarker模板时出现FileNotFoundException

即使模板实际存在于路径中,我在加载freemarker模板时也会收到文件未找到异常.

更新:这是作为Web服务运行的.它将根据搜索查询将xml返回给客户端.当我从另一个java程序(来自静态main)调用它时,模板加载成功.但是当客户端请求xml时,会发生FileNotFoundException.

操作系统:Windows 7文件的绝对路径:C:/ Users/Jay/workspace/WebService/templates /

这是我的代码:

private String templatizeQuestion(QuestionResponse qr) throws Exception
{
    SimpleHash context = new SimpleHash();
    Configuration config = new Configuration();

    StringWriter out = new StringWriter();

    Template _template = null;

    if(condition1)
    {           
        _template = config.getTemplate("/templates/fibplain.xml");
    } 
    else if(condition2)
    {
        _template = config.getTemplate("/templates/mcq.xml");
    }
    context.put("questionResponse", qr);
    _template.process(context, out);

    return out.toString();
 }
Run Code Online (Sandbox Code Playgroud)

完整错误堆栈:

 java.io.FileNotFoundException: Template /templates/fibplain.xml not found.
at freemarker.template.Configuration.getTemplate(Configuration.java:495)
at freemarker.template.Configuration.getTemplate(Configuration.java:458)
at com.hm.newAge.services.Curriculum.templatizeQuestion(Curriculum.java:251)
at com.hm.newAge.services.Curriculum.processQuestion(Curriculum.java:228)
at com.hm.newAge.services.Curriculum.processQuestionList(Curriculum.java:210)
at com.hm.newAge.services.Curriculum.getTest(Curriculum.java:122)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) …
Run Code Online (Sandbox Code Playgroud)

java eclipse freemarker filenotfoundexception

10
推荐指数
2
解决办法
2万
查看次数

FreeMarker?替换多个值

是否可以在FreeMarker模板中替换多个值?例如,如果我想用"c"替换"a"和"b",我必须这样做:

${event.EventTitle?replace('a','c')?replace('b','c')}
Run Code Online (Sandbox Code Playgroud)

但我宁愿这样做:

${event.EventTitle?replace("'a','b'",'c')}
Run Code Online (Sandbox Code Playgroud)

FreeMarker有这种能力吗?

我最终试图用破折号替换所有特殊字符($,.,@,&等),所以请随意提出一种更简单的方法.

replace freemarker

10
推荐指数
1
解决办法
2万
查看次数

弹簧mvc中的多视图解析器

我想在我的网络应用程序中使用多个视图解析器基于spring mvc

任何人都可以告诉我如何实现这一目标.

我想在我的应用程序中使用JSP和freemarker.请提出一些方法或链接或示例..

所有帮助表示赞赏.

Adhir

spring freemarker spring-mvc

9
推荐指数
1
解决办法
1万
查看次数

使用JAX-RS/Jersey和Freemarker模板

网上有一些例子展示了如何使用JAX-RS实现Jersey以及FreeMarker等自定义模板引擎.但这些例子看起来有点即兴过时.还有一个例子仅依赖于JAX-RS而不是Jersey特定类.ViewProcessorFreeMarker 是否有成熟的实现,还是我必须自己编写?

java templates freemarker jax-rs jersey

9
推荐指数
1
解决办法
2867
查看次数

Freemarker在模板中打印日期

我试图在模板激活时打印当前日期.我已经读过,我必须将新的Date()Java对象传递给模板,但我不知道如何做到这一点或将其放在代码中.

在这种情况下,有人知道如何将Java对象传递给模板吗?

谢谢 !!

datetime templates freemarker pretty-print pojo

9
推荐指数
2
解决办法
1万
查看次数

在Freemarker中添加Spring库以使用JSP Taglibs以确保安全性

我使用带有freemarker的spring作为模板引擎.Freemarker允许使用Jsp Taglibs,例如,通过添加安全性

    <#assign security=JspTaglibs["http://www.springframework.org/security/tags"] />
Run Code Online (Sandbox Code Playgroud)

到模板,允许我使用的例子

    <@security.authorize ifNotGranted="ROLE_ADMIN">
        whatever
    </@security.authorize>
Run Code Online (Sandbox Code Playgroud)

但是,Spring/Freemarker找不到taglib,除非它们被添加到类路径中,所以我补充说

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-taglibs</artifactId>
        <version>${spring.version}</version>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

到我项目中的pom.xml.

但无论如何,无法找到标签!我必须将spring-security-taglibs.jar添加到WEB-INF/lib文件夹中,以便找到标签.

有人知道为什么必须将jar显式添加到lib文件夹中吗?在我看来,tomcat为什么不找到它们?

编辑@ddekany

谢谢.如果未将spring-security-taglibs.jar复制到WEB-INF/lib目录中,则堆栈跟踪如下所示

    No mapping defined for http://www.springframework.org/security/tags 
    The problematic instruction: ---------- ==> assignment: 
            security=JspTaglibs["http://www.springframework.org/security/tags"] 
            [on line 12, column 1 in home.ftl] in user-directive content.main 
            [on line 8, column 9 in home.ftl] in user-directive layout.global 
            [on line 2, column 1 in home.ftl] 
    ---------- Java backtrace for programmers: ----------      
    freemarker.template.TemplateModelException: 
            No mapping defined for http://www.springframework.org/security/tags at         
    freemarker.ext.jsp.TaglibFactory.get(TaglibFactory.java:180) at 
    ...
Run Code Online (Sandbox Code Playgroud)

security spring jsp freemarker taglib

9
推荐指数
2
解决办法
1万
查看次数

Google App Engine上的ServletContext,类路径中的"上下文"在哪里?

我想在我的Google App Engine java应用程序中部署一些Freemarker模板,以用作电子邮件正文模板.我正在使用freemarker-gae-2.3.23.jar.

我的问题是在war文件中我应该放置我的模板文件,以便Freemarker Configuration类可以找到它们吗?我认为WEB-INF/classes/templates可以工作,但是当我在GAE实例上运行它时,我收到以下错误.getRealPath()也没有提供任何见解.返回空字符串.任何想法或建议非常感谢.

SEVERE: Template ./templates/invitation.ftl not found.
java.lang.RuntimeException: Error in loading ftl template: Template ./templates/invitation.ftl not found.
Run Code Online (Sandbox Code Playgroud)

我的基本配置如下:

public class FreeMarkerConfig {

private static FreeMarkerConfig freeMarkerConfig = null;
private static Configuration cfg = null;
private static final Logger logger =   Logger.getLogger(FreeMarkerConfig.class.getName());

private FreeMarkerConfig(ServletContext context){
    cfg = new Configuration();
    cfg.setServletContextForTemplateLoading(context, "/templates");
}

public static FreeMarkerConfig getInstance(ServletContext context){
    if(freeMarkerConfig == null){

        freeMarkerConfig = new FreeMarkerConfig(context);
        return freeMarkerConfig;
    }
    return freeMarkerConfig;
}

public static Template getTemplateByName(String fileName){
    try { …
Run Code Online (Sandbox Code Playgroud)

java google-app-engine servlets freemarker

9
推荐指数
1
解决办法
468
查看次数

TinyMCE逃脱了FreeMarker标签

我使用TinyMCE 4作为HTML页面的WYSIWYG编辑器.但是当我在TinyMCE中使用FreeMarker标签惯用语时<#if condition>,<#else>当我从"代码视图"(简单的textarea)转到"设计视图"(WYSIWYG)时,它们会被破坏.

例如,如果我在一个简单的textarea中编写HTML(代码视图)

<#if x == 1>
  x is 1
<#else>
  x is not 1
</#if>
Run Code Online (Sandbox Code Playgroud)

然后切换到编辑器并返回到代码视图,它变为:

&lt;#if x == 1&gt;
    x is 1
&lt;#else/&gt;
    x is not 1
&lt;/#if&gt;
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,FreeMarker标签在切换时会被转义.

Q1:有没有办法配置TinyMCE来处理这些标签,这样当我在编辑器和代码视图之间来回切换时,标记保持一致?
Q2:或者(以不同的方式说同样的事情)TinyMCE是否支持FreeMarker?


到目前为止我尝试了什么?

html wysiwyg freemarker tinymce

9
推荐指数
1
解决办法
473
查看次数

检查地图是否包含Freemarker中的特定密钥

在Java中:

Map<String, Object> model = new HashMap<>();
Map<String, String> items = new HashMap<>();
items.put("color", "red");
model.put("items", items);
Run Code Online (Sandbox Code Playgroud)

我现在想要在items包含密钥的渲染模板中包含一个片段color.

<#if ???? >
   the map contains a key called color
</#if>
Run Code Online (Sandbox Code Playgroud)

我该怎么替换???? 用?

freemarker

9
推荐指数
1
解决办法
5095
查看次数

如何获取freemarker中列表的大小?

我创建了一个内容模板,用户可以在其中创建多个卡片/盒子。它运行良好,但如果用户只创建一个盒子,我想更改 css。我尝试使用 getList()?size 但它不起作用。知道如何证明大小是否为 1 吗?

<div id="my-cards">
    <#list boxen.getSiblings() as box>
     <#assign i = box.getList()?size>   
       <#if i==1>
       <p>There is one box</p>
       </#if>
        <!-- Card-->
        <div class="card-box mb-3">
            <!-- Image-->
            <div class="card-pic">
                    <#if box.image.getData() != "">
                    <img src="${box.image.getData()}" />
                </#if>
            </div>
            <!--  Content-->
            <div class="card-box-content">


                <#if box.headline.getData() != "">
                <h2 class="title">${box.headline.getData()}</h2>
                 </#if>

                 <#if box.content.getData() != "">            
                <p class="description">${box.content.getData()}</p>
                </#if>
                <div class="card-box-footer">
                    <br />

            </div>
        </div>

    </#list>   
</div>
Run Code Online (Sandbox Code Playgroud)

java freemarker liferay

9
推荐指数
1
解决办法
2万
查看次数