diff --git a/spring-freemarker/src/main/java/com/baeldung/freemarker/controller/SpringController.java b/spring-freemarker/src/main/java/com/baeldung/freemarker/controller/SpringController.java index 18b42f06ea..6f6b5994f3 100644 --- a/spring-freemarker/src/main/java/com/baeldung/freemarker/controller/SpringController.java +++ b/spring-freemarker/src/main/java/com/baeldung/freemarker/controller/SpringController.java @@ -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"; + } +} \ No newline at end of file diff --git a/spring-freemarker/src/main/java/com/baeldung/freemarker/method/LastCharMethod.java b/spring-freemarker/src/main/java/com/baeldung/freemarker/method/LastCharMethod.java new file mode 100644 index 0000000000..956a512f56 --- /dev/null +++ b/spring-freemarker/src/main/java/com/baeldung/freemarker/method/LastCharMethod.java @@ -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); + } +} diff --git a/spring-freemarker/src/main/webapp/WEB-INF/views/ftl/commons.ftl b/spring-freemarker/src/main/webapp/WEB-INF/views/ftl/commons.ftl new file mode 100644 index 0000000000..eea1e6f683 --- /dev/null +++ b/spring-freemarker/src/main/webapp/WEB-INF/views/ftl/commons.ftl @@ -0,0 +1,28 @@ +

Testing is a property exists: ${student???c}

+<#if status??> +

Property value: ${status.reason}

+<#else> +

Missing property

+ + +

Iterating a sequence:

+<#list statuses> + + <#else> +

No statuses available

+ + +

Using static methods

+<#assign MathUtils=statics['java.lang.Math']> +

PI value: ${MathUtils.PI}

+

2*10 is: ${MathUtils.pow(2, 10)}

+ +

Using bean method

+

Random value: ${random.nextInt()}

+ +

Use custom method

+

Last char example: ${lastChar('mystring')}

\ No newline at end of file