FAQ Database Discussion Community


How to split string in chars and get code of each char in freemarker?

casting,char,freemarker
I think that I can split using this: <#list str as c> ... </#list> But then I need to convert this char to byte and add it to another number. In most languages it will be like so: int num=53, res; char c='g'; res=num+c;//c cast to byte and then to...

Lifefay freemarker ADT: methods unavailable?

java,liferay,liferay-6,freemarker
I'm trying to work on some ADT for asset publisher (Documents and Media type), but I often have a message telling me that : Expression assetRenderer.getDataRepositoryId is undefined or Expression assetRenderer.getFileEntryId is undefined I found some threads where people said that we must add velocity.engine.restricted.classes= velocity.engine.restricted.variables= freemarker.engine.restricted.classes= freemarker.engine.restricted.variables= in portal-ext.properties...

Regex to match and replace **someVal** in to someVal

regex,freemarker
I have the following input: **1** Blah **3** Blah Desired Output: <b>1</b> Blah <b>3</b> Blah Basically I'm trying to use the following regex with free marker to replace leading ** with <b> and </b> /\*\*([^\*]+)\*\* https://regex101.com/r/aX0xL6/2 I'm a bit confused on how to replace the ** for an according <b>...

In FreeMarker, how do I automatically convert a custom object (e.g. java.awt.Color) to a particular String value (e.g. HTML hex color)?

java,freemarker
In my data model, I have a lot of methods which return a color encoded as an HTML color hex code (e.g. #8da6b1). These methods would be much simpler if they could simply return a java.awt.Color and let FreeMarker object wrappers take care of the conversion. What do I need...

Freemarker template error handling with Spring MVC

spring-mvc,freemarker
I am using Freemarker template with Spring MVC. Is there a way to return HTTP Status 500 if there is any error rendering the template? Currently I am using attempt block to handle error, but would like to throw Internal Server error and allow web server to redirect to a...

Output string as html in freemarker

java,templates,freemarker
So we are storing html in out data model. I need to output this into a freemarker template: example: [#assign value = model.value!] ${value} value = '<p>This is <a href='somelink'>Some link</a></p>' I have tried [#noescape] but it throws an error saying there is no escape block. see FREEMARKER: avoid escaping...

Freemarker template not able to parse the Java object

java,templates,freemarker
I have a POJO (the class has getters set for each field) which i am sending back to a variable in a different class where the template configuration is done. Somehow i am getting an error when the ftl tries to populate the view. I don't know how to present...

Unable to parse JSON in Freemarker 2.3.21

freemarker
I have a valid json. Which I want to assign to a variable to be used in the page. But FTL is throwing error suggesting "the string is evaluated to null or missing". I was able to do the same operation in 2.1.X versions smoothly. What I might be doing...

Checking Spring security roles and logged username in Freemarker template

java,spring,security,freemarker
Does anyone know the freemarker tags to check spring security roles and username in freemarker file? I found from couple of resources on the web that the following code would print the logged in username. But it is not printing the username, instead it is printing just "logged in as"...

How to use “or” in freemarker templates

freemarker
I am trying to change the layout of a data table based on the presence or lack of data. What I would LIKE to write but does not function is: <#if record.enduseraccount?has_content OR record.installedataccount?has_content> Table 1 <#else> table 2 </#if> I don't need both statements to be true, just one...

Remove XML Node when Freemarker Variable defined inside that node is not used or set

java,xml,xpath,freemarker
My XML has Freemarker Variables ${xyz} defined in it as show in the below example. After processing this XML against a Map I replaced Freemarker Variables with relevant values. I would like to delete those XML nodes whose freemarker variables are not used. Example Scenario: Below XML having Freemarker variables...

Freemarker template string util that can create n number of characters or from a string?

freemarker
is there a string util can can do something like this <#assign junk="repeatMe"/> ${string.utils.repeat(junk,2)} OUTPUT: repeatMerepeatMe...

pass more parameters to freemarker template

freemarker
How could I check if more parameters were passed to the template that the template needs? For example the template is <wcw>${ricflair}</wcw> And 2 parameters are passed to the template: ricflair=The president goldberg=The king I know it is possible to check the other way around (if less parameters are passed)...

FreeMarker and OSGI

java,jsp,java-ee,osgi,freemarker
We have done a lot of work with building our framework using javaee and osgi. As a template system JSP was selected as the fastest and very mature. However, we came across one very serious problem. As we use osgi, we have one web application bundle - wab. This bundle...

Freemarker displays int value as double value(e.g. prints 0 as 0.00)

freemarker
I am trying to write a freemarker template file having information about a table. I have written '${noRows}' in the template for table rows count. The noRows is a int value: int noOfRows = myTable.getNumRows(); data.put("noRows", noOfRows); The noOfRows is always an integer value but in the output file it...

Assign key for a hash inside a hash in freemarker

hash,freemarker
I want to make a new model of an existing model in freemarker to make it easy to use. My model is something like this: [ { "id": "1", "type": "TYPE_1", "code": "CODE_OF_TYPE_1" }, { "id": "2", "type": "TYPE_1", "code": "ANOTHER_CODE_OF_TYPE_1" }, { "id": "3", "type": "TYPE_2", "code": "CODE_OF_TYPE_2" },...

String.format in FreeMarker

freemarker
I need to output a few values in FreeMarker using printf-esque parameters equivalent to this: String.format("%04d", index); String.format("%s (%s)", lastName, firstName); I've tried doing the following, which fails with an error: ${String.format("%04d", index)} It fails to locate String. What's the recommended way of approaching these formatting issues?...

How to call public Java variable in FreeMarker

java,freemarker
I'm trying to call a Java method in my FreeMarker template that uses a public static Java variable as one of its parameters. For example, if the FreeMarker code in a test.ftl is this: ${javaClass.getSomething(javaClass.VARIABLE)} and if the class JavaClass looks like this: public class JavaClass { public static final...

How to implement bit-shift in freemarker?

freemarker,bit-shift
I'm working on captcha, but the server doesn't support php and i can't add java libraries to it. So I have to implement check on freemarker, but the function using binary shift <<, is it any way to implement it in freemarker?

how to use macros with FreeMarker in spring mvc

java,spring,spring-mvc,template-engine,freemarker
hi i'm woriking in a spring mvc project and i want to use FreeMarker as my template engine, i 'm having problems using macros i want to create a master page or a layout with (the footer, header and menu)that i could use in my other pages, so far i'm...

Freemarker - Evaluating an expression one after another

freemarker
the issue here is that i have both index and c as the evaluated expressions, after index[1] is evaluated, it will serve as a parameter to the userDetails. Hence, an e.g. like c.firstname. <#ftl encoding='UTF-8'> <#list param?chunk(3) as index> <#list userDetails as c> ${c.index[1]} </#list> </#list> However, i am assuming...

exporting constructors into the FreeMarker data model

java,freemarker
Is there any way to export a specific class constructor into the FreeMarker data model? ObjectConstructor provides the power to access any accessible constructor: Java: myDataModel.put("objectConstructor", new ObjectConstructor()); Template: <#assign aList = objectConstructor("java.util.ArrayList", 100)> But I don't want to do that; if I have a class Foo with two constructors...

How to remove file extension in a FreeMarker template

java,freemarker
My Freemarker template is as following: file : ${filename} In the java code filename is a String whose extension can vary: "the_file_name.ext" or "the_file_name.mpas" or even "the_file_name" The result I would like : file : the_file_name Does FreeMarker offer the functionnality I am looking for ? For example : file...

`name` as a variable name in freemarker breaks

java,maven,freemarker,spark-java
I have the following code in an ftl: <#macro field label name value="" type="text"> ${name} ${name!"print if null"} <div class="field"> <div class="clearfix" id="${name}_field"> <label for="${name}">${label}</label> <div class="input"> <input type="${type}" id="${name}" name="${name}" value="${value}"> <span class="help-inline"></span> <span class="help-block"></span> </div> </div> </div> </#macro> <@field label="label" name="test" /> And this is...

FreeMarker textarea display improper

java,spring,freemarker
I have a requirement where we have a review screen and we can see all values entered in previous screen. Textarea is giving problem here when we have multiple lines like eg test test This gets changed to "test test" in review screen. All the return characters are replaced by...

Freemarker special symbol usage

java,templates,freemarker
What does below structure means in ftl? How do I create my own? Where can I find more info about it? Google doesn't seem to follow this. @[someArray.someVariable]...

Inheritance / instanceof checks in Freemarker

java,templates,freemarker
my use case is to provide the user the possibility to create reports with the help of a template engine. Therefore I extracted the relevant part of my data model and integrated Freemarker as template engine. So far it worked great, but now my data model contains inheritance on some...

Freemarker iterating over hashmap in list of map

java,html,freemarker
I have seen the similar questions, but still could not found greatly solution. This program: List<HashMap<String, String>> list = ArrayList<HashMap<String, String>>(); String abc = "a,b,c"; for(String x : abc.split(",")){ // how to get this ? HashMap<String, String> map = new HashMap<String, String>(); map.put("one", x); map.put("two", x); map.put("three",x); list.add(map); } HashMap<String,...

Spring Boot with Freemarker - form tags

java,spring,spring-mvc,spring-boot,freemarker
I would like to use spring Boot MVC with Freemarker and display a form in a similar way to how it is done with JSP tags. E.g. this form: <form:form method="post" action="save" modelAttribute="form" class="form-horizontal"> <form:hidden path="id"/> <div class="form-group"> <label for="name" class="col-sm-2 control-label">Name</label> <div class="col-sm-10"> <form:input id="name" path="name" class="form-control" /> </div>...

freemarker and spring security taglib looking fo a list of functions

java,spring-mvc,spring-security,template-engine,freemarker
hi i'm working with a apring mvc project and i'm using freemarker as my template engine, i added spring secuirty to my pages like this <#assign security=JspTaglibs["http://www.springframework.org/security/tags"] /> and i put this tag in the content that i want to be visible to a specific user <@security.authorize ifAnyGranted="ROLE_ADMIN"> <div><h1>Hello ADMIN</h1></div>...

Getting big objects with FreeMarker

java,reflection,freemarker
I think it would be really great, if template designer would be able to access all the fields he wants from the given object. So in java instead of: Map<String, Object> fields = new HashMap<>(); fields.add("x", unit.getX()); fields.add("y", unit.getY()); fields.add("z", unit.getZ()); Map<String, Object> dataModel = new HashMap<>(); dataModel.put("unit", fields); I...

Spring Boot, Freemarker, MVC Unit Tests, Csrf

c#,unit-testing,spring-boot,csrf,freemarker
Im using Freemarker with Spring Boot and do mvc unit tests. In my freemarker template I have a hidden input field for the csrf token like this: <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/> Then I also have a mvc unit test : @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = MyApplication.class) @WebAppConfiguration @ActiveProfiles("test") public class MvcTests {...

Is it possible to create a Freemarker macro programmatically?

java,freemarker,ninjaframework
Freemarker is used as the default template engine in the ninja web framework. The framework assigns some default values to a template which are globaly available when using the ninja web framework. I have created an extension for the template which does enbales CSRF-Protection. The extension offers a function which...

Setting a property in model object which will be shared by all controller method of spring mvc

spring,spring-mvc,model,freemarker
I have few attributes with values I wanted to set in the application scope of Spring MVC. And I should be able to access this in the view layer (I am using freemarker). This value should be loaded only one time during the application startup. What is the best practice...

Freemarker removeIntrospectionInfo does not work with DCEVM after model hotswap

freemarker,hotswap,dcevm
I am using Freemarker and DCEVM+HotSwapManager agent. This basically allows me to hotswap classes even when adding/removing methods. Everything works like charm until Freemarker uses hotswapped class as model. It's throwing freemarker.ext.beans.InvalidPropertyException: No such bean property on me even though reflection shows that the method is there (checked during debug...

Creating a Velocity template for Dynamic Data List (Liferay 6.2)

templates,liferay,velocity,freemarker,datalist
I'm currently working with liferay 6.2 but i'm having problems to create a Display Template for a Dynamic data list. I'm trying to follow this guide: https://dev.liferay.com/discover/portal/-/knowledge_base/6-2/make-it-pretty-creating-custom-displays#display-templates But the FreeMarker example doesn't work. I'm trying to create a template in Velocity (since i have more knoledge than FreeMarker): My code...

How to create multiple files based on one Freemarker Template

java,code-generation,freemarker
I'm having a little bit trouble with freemarker right now. What I want to do basically in my template: iterate over a list of elements and create for each element a new file. <#assign x=3> <#list 1..x as i> ${i} ...create a new file with the output of this loop...

Post of properties in a nested list does not seem to work in spring mvc

java,spring,spring-mvc,freemarker
I have an Object with a main list in it and a sub list in that main list. Showing the properties in a form is no problem. But when I post the form and one or more of the properties of the sub list are inputs (hidden or not) I...

Freemarker - Is there a way to format an integer as a floating point number?

java,freemarker
That's pretty much the question. I just want to do something basic like 5 and format it as 5.0 ...

how to add parameterized message in freemarker / spring messages

spring,spring-mvc,freemarker
I'm using freemarker as view renderer in a Spring application. Is there any way to parameterise messages from the spring.ftl library? In java we'd use the message source getMessageSource("code.key", new String[]{"param1", "param2"}, null, null) and it interpolates the string. But there doesn't appear to be an option for this with...