Discussion:
[FreeMarker-user] Working in a Unit Test, not in Application
Peter Harrison
2015-09-01 02:28:27 UTC
Permalink
I've written a wrapper around Freemarker for use in my application. My
unit test runs the following template:

<#assign hello="Goodbye cruel world">
${hello}

And you get "Goodbye cruel world" as you would expect.

But in my application, which runs inside Tomcat, the same template
results in nothing. Nada. Exactly the same code, two different outcomes.
Obviously something environmental, but I have no idea what.

public class TemplatePlugin implements Plugin {

@Autowired
private ResourceController resourceController;

@Override
public Map<String,Object> process( Action action,
Map<String,Object> context ) throws Exception{
String resource =
getResourceController().getResource(action.getResource());

Configuration configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");

Template template = new Template(action.getResource(), new
StringReader(resource), configuration);
Writer output = new StringWriter();
template.process(context, output);
String result = output.toString();
context.put(action.getResponse(), result);
return context;
}

}

------------------------------------------------------------------------------
Evangelia Dendramis
2015-09-01 03:39:18 UTC
Permalink
Are there any errors in the Tomcat log?
Post by Peter Harrison
I've written a wrapper around Freemarker for use in my application. My
<#assign hello="Goodbye cruel world">
${hello}
And you get "Goodbye cruel world" as you would expect.
But in my application, which runs inside Tomcat, the same template
results in nothing. Nada. Exactly the same code, two different outcomes.
Obviously something environmental, but I have no idea what.
public class TemplatePlugin implements Plugin {
@Autowired
private ResourceController resourceController;
@Override
public Map<String,Object> process( Action action,
Map<String,Object> context ) throws Exception{
String resource =
getResourceController().getResource(action.getResource());
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
Template template = new Template(action.getResource(), new
StringReader(resource), configuration);
Writer output = new StringWriter();
template.process(context, output);
String result = output.toString();
context.put(action.getResponse(), result);
return context;
}
}
------------------------------------------------------------------------------
_______________________________________________
FreeMarker-user mailing list
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Daniel Dekany
2015-09-01 06:38:27 UTC
Permalink
Have you checked if the `result` variable is indeed empty (like with
logging it or something direct)?

Have you checked what's in the `template` variable, if you toString
it?

Otherwise, JavaCC has this terrible habit of silently ending parsing
on I/O errors (which is also a problem in Velocity for example, not
just in FreeMarker). In 2.3.24 I have worked that around. So as a last
resort, try if with
http://freemarker.org/builds/2.3.24-nightly/freemarker.jar you get an
exception.
Post by Peter Harrison
I've written a wrapper around Freemarker for use in my application. My
<#assign hello="Goodbye cruel world">
${hello}
And you get "Goodbye cruel world" as you would expect.
But in my application, which runs inside Tomcat, the same template
results in nothing. Nada. Exactly the same code, two different outcomes.
Obviously something environmental, but I have no idea what.
public class TemplatePlugin implements Plugin {
@Autowired
private ResourceController resourceController;
@Override
public Map<String,Object> process( Action action,
Map<String,Object> context ) throws Exception{
String resource =
getResourceController().getResource(action.getResource());
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
Template template = new Template(action.getResource(), new
StringReader(resource), configuration);
Writer output = new StringWriter();
template.process(context, output);
String result = output.toString();
context.put(action.getResponse(), result);
return context;
}
}
------------------------------------------------------------------------------
_______________________________________________
FreeMarker-user mailing list
https://lists.sourceforge.net/lists/listinfo/freemarker-user
--
Thanks,
Daniel Dekany


------------------------------------------------------------------------------
Continue reading on narkive:
Loading...