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:
parent
3eca6e588e
commit
454f0e50c5
|
@ -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>
|
|
@ -0,0 +1,58 @@
|
|||
package com.baeldung.thymeleaf.controller;
|
||||
|
||||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
|
||||
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.mock.web.MockHttpSession;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.RequestPostProcessor;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import com.baeldung.thymeleaf.config.InitSecurity;
|
||||
import com.baeldung.thymeleaf.config.WebApp;
|
||||
import com.baeldung.thymeleaf.config.WebMVCConfig;
|
||||
import com.baeldung.thymeleaf.config.WebMVCSecurity;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = { WebApp.class, WebMVCConfig.class, WebMVCSecurity.class, InitSecurity.class })
|
||||
public class ExpressionUtilityObjectsControllerTest {
|
||||
|
||||
@Autowired
|
||||
WebApplicationContext wac;
|
||||
@Autowired
|
||||
MockHttpSession session;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
private Filter springSecurityFilterChain;
|
||||
|
||||
protected RequestPostProcessor testUser() {
|
||||
return user("user1").password("user1Pass").roles("USER");
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mockMvc = MockMvcBuilders.webAppContextSetup(wac).addFilters(springSecurityFilterChain).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetDates() throws Exception{
|
||||
mockMvc.perform(get("/objects").with(testUser()).with(csrf())).andExpect(status().isOk()).andExpect(view().name("objects.html"));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue