BAEL-2528 FreeMarker common operations (#7586)
* BAEL-2528 FreeMarker common operations * BAEL-2528 FreeMarker common operations * BAEL-2528 FreeMarker common operations
This commit is contained in:
parent
d05905664f
commit
2ae596d54d
|
@ -1,9 +1,10 @@
|
|||
package com.baeldung.freemarker.controller;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.*;
|
||||
|
||||
import com.baeldung.freemarker.method.LastCharMethod;
|
||||
import freemarker.template.DefaultObjectWrapperBuilder;
|
||||
import freemarker.template.Version;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.ui.ModelMap;
|
||||
|
@ -43,4 +44,12 @@ public class SpringController {
|
|||
return "redirect:/cars";
|
||||
}
|
||||
|
||||
}
|
||||
@RequestMapping(value = "/commons", method = RequestMethod.GET)
|
||||
public String showCommonsPage(Model model) {
|
||||
model.addAttribute("statuses", Arrays.asList("200 OK", "404 Not Found", "500 Internal Server Error"));
|
||||
model.addAttribute("lastChar", new LastCharMethod());
|
||||
model.addAttribute("random", new Random());
|
||||
model.addAttribute("statics", new DefaultObjectWrapperBuilder(new Version("2.3.28")).build().getStaticModels());
|
||||
return "commons";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.freemarker.method;
|
||||
|
||||
import freemarker.template.TemplateMethodModelEx;
|
||||
import freemarker.template.TemplateModelException;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class LastCharMethod implements TemplateMethodModelEx {
|
||||
public Object exec(List arguments) throws TemplateModelException {
|
||||
if (arguments.size() != 1 || StringUtils.isEmpty(arguments.get(0)))
|
||||
throw new TemplateModelException("Wrong arguments!");
|
||||
String argument = arguments.get(0).toString();
|
||||
return argument.charAt(argument.length() - 1);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
<p>Testing is a property exists: ${student???c}</p>
|
||||
<#if status??>
|
||||
<p>Property value: ${status.reason}</p>
|
||||
<#else>
|
||||
<p>Missing property</p>
|
||||
</#if>
|
||||
|
||||
<p>Iterating a sequence:</p>
|
||||
<#list statuses>
|
||||
<ul>
|
||||
<#items as status>
|
||||
<li>${status}</li>
|
||||
</#items>
|
||||
</ul>
|
||||
<#else>
|
||||
<p>No statuses available</p>
|
||||
</#list>
|
||||
|
||||
<p>Using static methods</p>
|
||||
<#assign MathUtils=statics['java.lang.Math']>
|
||||
<p>PI value: ${MathUtils.PI}</p>
|
||||
<p>2*10 is: ${MathUtils.pow(2, 10)}</p>
|
||||
|
||||
<p>Using bean method</p>
|
||||
<p>Random value: ${random.nextInt()}</p>
|
||||
|
||||
<p>Use custom method</p>
|
||||
<p>Last char example: ${lastChar('mystring')}</p>
|
Loading…
Reference in New Issue