Expression Utility Objects article (#724)
* Expression-Based Access Control PermitAll, hasRole, hasAnyRole etc. I modified classes regards to Security * Added test cases for Spring Security Expressions * Handler Interceptor - logging example * Test for logger interceptor * Removed conflicted part * UserInterceptor (adding user information to model) * Spring Handler Interceptor - session timers * Spring Security CSRF attack protection with Thymeleaf * Fix and(); * Logger update * Changed config for Thymeleaf * Thymeleaf Natural Processing and Inlining * Expression Utility Objects, Thymeleaf
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
3eca6e588e
commit
454f0e50c5
+33
@@ -0,0 +1,33 @@
|
||||
package com.baeldung.thymeleaf.controller;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
/**
|
||||
* Controller to test expression utility objects: dates,
|
||||
*
|
||||
*/
|
||||
@Controller
|
||||
public class ExpressionUtilityObjectsController {
|
||||
|
||||
@RequestMapping(value = "/objects", method = RequestMethod.GET)
|
||||
public String getDates(Model model) {
|
||||
model.addAttribute("date", new Date());
|
||||
model.addAttribute("calendar", Calendar.getInstance());
|
||||
model.addAttribute("num", Math.random() * 10);
|
||||
model.addAttribute("string", "new text");
|
||||
model.addAttribute("emptyString", "");
|
||||
model.addAttribute("nullString", null);
|
||||
model.addAttribute("array", new int[] { 1, 3, 4, 5 });
|
||||
model.addAttribute("set", new HashSet<Integer>(Arrays.asList(1, 3, 8)));
|
||||
return "objects.html";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||
xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Expression utility objects</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Date expression utility</h1>
|
||||
<p th:text="${#dates.formatISO(date)}"></p>
|
||||
<p th:text="${#dates.format(date, 'dd-MM-yyyy HH:mm')}"></p>
|
||||
<p th:text="${#dates.dayOfWeekName(date)}"></p>
|
||||
<p th:text="${#dates.createNow()}"></p>
|
||||
<p th:text="${#dates.createToday()}"></p>
|
||||
|
||||
<h1>Calendar expression utility</h1>
|
||||
<p th:text="${#calendars.formatISO(calendar)}"></p>
|
||||
<p th:text="${#calendars.format(calendar, 'dd-MM-yyyy HH:mm')}"></p>
|
||||
<p th:text="${#calendars.dayOfWeekName(calendar)}"></p>
|
||||
<p th:text="${#calendars.createNow().getTime()}"></p>
|
||||
<p th:text="${#calendars.createToday().getFirstDayOfWeek()}"></p>
|
||||
|
||||
<h1>Numbers expression utility</h1>
|
||||
<p th:text="${#numbers.formatDecimal(num,2,3)}"></p>
|
||||
<p th:text="${#numbers.formatDecimal(num,2,3,'COMMA')}"></p>
|
||||
<p th:each="number: ${#numbers.sequence(0,2)}">
|
||||
<span th:text="${number}"></span>
|
||||
</p>
|
||||
<p th:each="number: ${#numbers.sequence(0,4,2)}">
|
||||
<span th:text="${number}"></span>
|
||||
</p>
|
||||
|
||||
<h1>Calendar expression utility</h1>
|
||||
<p th:text="${#strings.isEmpty(string)}"></p>
|
||||
<p th:text="${#strings.isEmpty(nullString)}"></p>
|
||||
<p th:text="${#strings.defaultString(emptyString,'Empty String')}"></p>
|
||||
<p th:text="${#strings.containsIgnoreCase(string,'new')}"></p>
|
||||
<p th:text="${#strings.abbreviate(string,5)} "></p>
|
||||
<p th:text="${#strings.capitalizeWords(string)}"></p>
|
||||
|
||||
<h1>Aggregates expression utility</h1>
|
||||
<p th:text="${#aggregates.sum(array)}"></p>
|
||||
<p th:text="${#aggregates.avg(array)}"></p>
|
||||
<p th:text="${#aggregates.sum(set)}"></p>
|
||||
<p th:text="${#aggregates.avg(set)}"></p>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user