BAEL-3091: The Prototype Pattern in Java (changed code based on valid comments from a reader)

This commit is contained in:
Vivek Balasubramaniam
2019-10-29 22:27:15 +05:30
parent db85c8f275
commit d3d5b060e7
20517 changed files with 1642290 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
/target/
+43
View File
@@ -0,0 +1,43 @@
## Spring Thymeleaf
This module contains articles about Spring with Thymeleaf
### Relevant Articles:
- [Introduction to Using Thymeleaf in Spring](https://www.baeldung.com/thymeleaf-in-spring-mvc)
- [CSRF Protection with Spring MVC and Thymeleaf](https://www.baeldung.com/csrf-thymeleaf-with-spring-security)
- [Thymeleaf: Custom Layout Dialect](https://www.baeldung.com/thymeleaf-spring-layouts)
- [Spring and Thymeleaf 3: Expressions](https://www.baeldung.com/spring-thymeleaf-3-expressions)
- [Spring MVC + Thymeleaf 3.0: New Features](https://www.baeldung.com/spring-thymeleaf-3)
- [How to Work with Dates in Thymeleaf](https://www.baeldung.com/dates-in-thymeleaf)
- [How to Create an Executable JAR with Maven](https://www.baeldung.com/executable-jar-with-maven)
- [Working with Boolean in Thymeleaf](https://www.baeldung.com/thymeleaf-boolean)
- [Working with Fragments in Thymeleaf](https://www.baeldung.com/spring-thymeleaf-fragments)
- [Conditionals in Thymeleaf](https://www.baeldung.com/spring-thymeleaf-conditionals)
- [Iteration in Thymeleaf](https://www.baeldung.com/thymeleaf-iteration)
- [Working With Arrays in Thymeleaf](https://www.baeldung.com/thymeleaf-arrays)
- [Spring with Thymeleaf Pagination for a List](https://www.baeldung.com/spring-thymeleaf-pagination)
- [Working with Select and Option in Thymeleaf](https://www.baeldung.com/thymeleaf-select-option)
- [Working With Custom HTML Attributes in Thymeleaf](https://www.baeldung.com/thymeleaf-custom-html-attributes)
- [Spring Request Parameters with Thymeleaf](https://www.baeldung.com/spring-thymeleaf-request-parameters)
- [[next -->]](/spring-thymeleaf-2)
### Build the Project
mvn clean install
### Run the Project
mvn cargo:run
- **note**: starts on port '8082'
Access the pages using the URLs:
- http://localhost:8082/spring-thymeleaf/
- http://localhost:8082/spring-thymeleaf/addStudent/
- http://localhost:8082/spring-thymeleaf/listStudents/
- http://localhost:8082/spring-thymeleaf/booleans/
The first URL is the home page of the application. The home page has links to the second and third pages.
### Security
The user/password required is: user1/user1Pass
+180
View File
@@ -0,0 +1,180 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>spring-thymeleaf</artifactId>
<version>0.1-SNAPSHOT</version>
<name>spring-thymeleaf</name>
<packaging>war</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-spring-5</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-spring-5</relativePath>
</parent>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>${spring-data.version}</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>${javax.validation-version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate-validator.version}</version>
</dependency>
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring-security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring-security.version}</version>
</dependency>
<!-- Thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>${org.thymeleaf-version}</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>${org.thymeleaf-version}</version>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
<version>${thymeleaf-layout-dialect.version}</version>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
<version>${org.thymeleaf.extras-version}</version>
</dependency>
<!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<!-- test scoped -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<version>${spring-security.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven-war-plugin.version}</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>${cargo-maven2-plugin.version}</version>
<configuration>
<wait>true</wait>
<container>
<containerId>jetty9x</containerId>
<type>embedded</type>
<systemProperties>
</systemProperties>
</container>
<configuration>
<properties>
<cargo.servlet.port>8082</cargo.servlet.port>
</properties>
</configuration>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>${tomcat7-maven-plugin.version}</version>
<executions>
<execution>
<id>tomcat-run</id>
<goals>
<goal>exec-war-only</goal>
</goals>
<phase>package</phase>
<configuration>
<path>/</path>
<enableNaming>false</enableNaming>
<finalName>webapp.jar</finalName>
<charset>utf-8</charset>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<spring-data.version>2.0.9.RELEASE</spring-data.version>
<org.thymeleaf-version>3.0.9.RELEASE</org.thymeleaf-version>
<org.thymeleaf.extras-version>3.0.1.RELEASE</org.thymeleaf.extras-version>
<thymeleaf-layout-dialect.version>2.3.0</thymeleaf-layout-dialect.version>
<javax.validation-version>2.0.1.Final</javax.validation-version>
<hibernate-validator.version>6.0.11.Final</hibernate-validator.version>
<!-- Maven plugins -->
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
<tomcat7-maven-plugin.version>2.2</tomcat7-maven-plugin.version>
</properties>
</project>
@@ -0,0 +1,12 @@
// package com.baeldung.thymeleaf;
// import org.springframework.boot.SpringApplication;
// import org.springframework.boot.autoconfigure.SpringBootApplication;
// @SpringBootApplication
// public class WorkingWithArraysInThymeleafApplication {
// public static void main(String[] args) {
// SpringApplication.run(WorkingWithArraysInThymeleafApplication.class, args);
// }
// }
@@ -0,0 +1,11 @@
package com.baeldung.thymeleaf.config;
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
public class InitSecurity extends AbstractSecurityWebApplicationInitializer {
public InitSecurity() {
super(WebMVCSecurity.class);
}
}
@@ -0,0 +1,36 @@
package com.baeldung.thymeleaf.config;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
/**
* Java configuration file that is used for web application initialization
*/
public class WebApp extends AbstractAnnotationConfigDispatcherServletInitializer {
public WebApp() {
super();
}
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WebMVCConfig.class, WebMVCSecurity.class, InitSecurity.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
protected void customizeRegistration(final Dynamic registration) {
super.customizeRegistration(registration);
}
}
@@ -0,0 +1,155 @@
package com.baeldung.thymeleaf.config;
import java.util.Locale;
import nz.net.ultraq.thymeleaf.LayoutDialect;
import nz.net.ultraq.thymeleaf.decorators.strategies.GroupingStrategy;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Description;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
import org.thymeleaf.extras.java8time.dialect.Java8TimeDialect;
import org.thymeleaf.spring5.ISpringTemplateEngine;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ITemplateResolver;
import com.baeldung.thymeleaf.formatter.NameFormatter;
import com.baeldung.thymeleaf.utils.ArrayUtil;
@Configuration
@EnableWebMvc
@ComponentScan({ "com.baeldung.thymeleaf" })
/**
* Java configuration file that is used for Spring MVC and Thymeleaf
* configurations
*/
public class WebMVCConfig implements WebMvcConfigurer, ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Bean
public ViewResolver htmlViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine(htmlTemplateResolver()));
resolver.setContentType("text/html");
resolver.setCharacterEncoding("UTF-8");
resolver.setViewNames(ArrayUtil.array("*.html"));
return resolver;
}
@Bean
public ViewResolver javascriptViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine(javascriptTemplateResolver()));
resolver.setContentType("application/javascript");
resolver.setCharacterEncoding("UTF-8");
resolver.setViewNames(ArrayUtil.array("*.js"));
return resolver;
}
@Bean
public ViewResolver plainViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine(plainTemplateResolver()));
resolver.setContentType("text/plain");
resolver.setCharacterEncoding("UTF-8");
resolver.setViewNames(ArrayUtil.array("*.txt"));
return resolver;
}
private ISpringTemplateEngine templateEngine(ITemplateResolver templateResolver) {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.addDialect(new LayoutDialect(new GroupingStrategy()));
engine.addDialect(new Java8TimeDialect());
engine.setTemplateResolver(templateResolver);
engine.setTemplateEngineMessageSource(messageSource());
return engine;
}
private ITemplateResolver htmlTemplateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(applicationContext);
resolver.setPrefix("/WEB-INF/views/");
resolver.setCacheable(false);
resolver.setTemplateMode(TemplateMode.HTML);
return resolver;
}
private ITemplateResolver javascriptTemplateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(applicationContext);
resolver.setPrefix("/WEB-INF/js/");
resolver.setCacheable(false);
resolver.setTemplateMode(TemplateMode.JAVASCRIPT);
return resolver;
}
private ITemplateResolver plainTemplateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(applicationContext);
resolver.setPrefix("/WEB-INF/txt/");
resolver.setCacheable(false);
resolver.setTemplateMode(TemplateMode.TEXT);
return resolver;
}
@Bean
@Description("Spring Message Resolver")
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
return messageSource;
}
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
localeResolver.setDefaultLocale(new Locale("en"));
return localeResolver;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("lang");
return localeChangeInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**", "/css/**")
.addResourceLocations("/WEB-INF/resources/", "/WEB-INF/css/");
}
@Override
@Description("Custom Conversion Service")
public void addFormatters(FormatterRegistry registry) {
registry.addFormatter(new NameFormatter());
}
}
@@ -0,0 +1,43 @@
package com.baeldung.thymeleaf.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
public class WebMVCSecurity extends WebSecurityConfigurerAdapter {
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
public WebMVCSecurity() {
super();
}
@Override
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user1").password("{noop}user1Pass").authorities("ROLE_USER");
}
@Override
public void configure(final WebSecurity web) throws Exception {
web.ignoring().antMatchers("/resources/**");
}
@Override
protected void configure(final HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
}
@@ -0,0 +1,46 @@
package com.baeldung.thymeleaf.controller;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.baeldung.thymeleaf.model.Book;
import com.baeldung.thymeleaf.service.BookService;
@Controller
public class BookController {
@Autowired
private BookService bookService;
@RequestMapping(value = "/listBooks", method = RequestMethod.GET)
public String listBooks(Model model, @RequestParam("page") Optional<Integer> page, @RequestParam("size") Optional<Integer> size) {
final int currentPage = page.orElse(1);
final int pageSize = size.orElse(5);
Page<Book> bookPage = bookService.findPaginated(PageRequest.of(currentPage - 1, pageSize));
model.addAttribute("bookPage", bookPage);
int totalPages = bookPage.getTotalPages();
if (totalPages > 0) {
List<Integer> pageNumbers = IntStream.rangeClosed(1, totalPages)
.boxed()
.collect(Collectors.toList());
model.addAttribute("pageNumbers", pageNumbers);
}
return "listBooks.html";
}
}
@@ -0,0 +1,45 @@
package com.baeldung.thymeleaf.controller;
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 boolean expressions
*
*/
@Controller
public class BooleanExpressionsController {
@RequestMapping(value = "/booleans", method = RequestMethod.GET)
public String getDates(Model model) {
// "truthy" values
model.addAttribute("trueValue", true);
model.addAttribute("one", 1);
model.addAttribute("nonZeroCharacter", 'a');
model.addAttribute("emptyString", "");
model.addAttribute("foo", "foo");
model.addAttribute("object", new Object());
model.addAttribute("arrayOfZeros", new Integer[] { 0, 0 });
model.addAttribute("arrayOfZeroAndOne", new Integer[] { 0, 1 });
model.addAttribute("arrayOfOnes", new Integer[] { 1, 1 });
// "falsy" values
model.addAttribute("nullValue", null);
model.addAttribute("falseValue", false);
model.addAttribute("zero", 0);
model.addAttribute("zeroCharacter", '\0');
model.addAttribute("falseString", "false");
model.addAttribute("no", "no");
model.addAttribute("off", "off");
model.addAttribute("isRaining", true);
model.addAttribute("isSunny", true);
model.addAttribute("isCold", false);
model.addAttribute("isWarm", true);
return "booleans.html";
}
}
@@ -0,0 +1,30 @@
package com.baeldung.thymeleaf.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.baeldung.thymeleaf.model.Course;
/**
* Handles requests for the student model.
*
*/
@Controller
public class CourseRegistrationController {
@RequestMapping(value = "/registerCourse", method = RequestMethod.POST)
public String register(@ModelAttribute Course course, Model model) {
model.addAttribute("successMessage", "You have successfully registered for course: " + course.getName() + ".");
return "courseRegistration.html";
}
@RequestMapping(value = "/registerCourse", method = RequestMethod.GET)
public String register(Model model) {
model.addAttribute("course", new Course());
return "courseRegistration.html";
}
}
@@ -0,0 +1,25 @@
package com.baeldung.thymeleaf.controller;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
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
public class DatesController {
@RequestMapping(value = "/dates", method = RequestMethod.GET)
public String getInfo(Model model) {
model.addAttribute("standardDate", new Date());
model.addAttribute("localDateTime", LocalDateTime.now());
model.addAttribute("localDate", LocalDate.now());
model.addAttribute("timestamp", Instant.now());
return "dates.html";
}
}
@@ -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,33 @@
package com.baeldung.thymeleaf.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import com.baeldung.thymeleaf.utils.StudentUtils;
@Controller
public class FragmentsController {
@GetMapping("/fragments")
public String getHome() {
return "fragments.html";
}
@GetMapping("/markup")
public String markupPage() {
return "markup.html";
}
@GetMapping("/params")
public String paramsPage() {
return "params.html";
}
@GetMapping("/other")
public String otherPage(Model model) {
model.addAttribute("data", StudentUtils.buildStudents());
return "other.html";
}
}
@@ -0,0 +1,26 @@
package com.baeldung.thymeleaf.controller;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* Handles requests for the application home page.
*
*/
@Controller
public class HomeController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String getHome(Model model) {
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.getDefault());
model.addAttribute("serverTime", dateFormat.format(new Date()));
return "home.html";
}
}
@@ -0,0 +1,33 @@
package com.baeldung.thymeleaf.controller;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.baeldung.thymeleaf.utils.StudentUtils;
@Controller
public class InliningController {
@RequestMapping(value = "/html", method = RequestMethod.GET)
public String getExampleHTML(Model model) {
model.addAttribute("title", "Baeldung");
model.addAttribute("description", "<strong>Thymeleaf</strong> tutorial");
return "inliningExample.html";
}
@RequestMapping(value = "/js", method = RequestMethod.GET)
public String getExampleJS(Model model) {
model.addAttribute("students", StudentUtils.buildStudents());
return "studentCheck.js";
}
@RequestMapping(value = "/plain", method = RequestMethod.GET)
public String getExamplePlain(Model model) {
model.addAttribute("username", SecurityContextHolder.getContext().getAuthentication().getName());
model.addAttribute("students", StudentUtils.buildStudents());
return "studentsList.txt";
}
}
@@ -0,0 +1,16 @@
package com.baeldung.thymeleaf.controller;
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
public class LayoutDialectController {
@RequestMapping(value = "/layout", method = RequestMethod.GET)
public String getNewPage(Model model) {
return "content.html";
}
}
@@ -0,0 +1,50 @@
package com.baeldung.thymeleaf.controller;
import java.util.List;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.baeldung.thymeleaf.model.Student;
import com.baeldung.thymeleaf.utils.StudentUtils;
/**
* Handles requests for the student model.
*
*/
@Controller
public class StudentController {
@RequestMapping(value = "/saveStudent", method = RequestMethod.POST)
public String saveStudent(@Valid @ModelAttribute Student student, BindingResult errors, Model model) {
if (!errors.hasErrors()) {
// get mock objects
List<Student> students = StudentUtils.buildStudents();
// add current student
students.add(student);
model.addAttribute("students", students);
}
return ((errors.hasErrors()) ? "addStudent.html" : "listStudents.html");
}
@RequestMapping(value = "/addStudent", method = RequestMethod.GET)
public String addStudent(Model model) {
model.addAttribute("student", new Student());
return "addStudent.html";
}
@RequestMapping(value = "/listStudents", method = RequestMethod.GET)
public String listStudent(Model model) {
model.addAttribute("students", StudentUtils.buildStudents());
return "listStudents.html";
}
}
@@ -0,0 +1,19 @@
package com.baeldung.thymeleaf.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.baeldung.thymeleaf.utils.TeacherUtils;
@Controller
public class TeacherController {
@RequestMapping(value = "/listTeachers", method = RequestMethod.GET)
public String getInfo(Model model) {
model.addAttribute("teachers", TeacherUtils.buildTeachers());
return "listTeachers.html";
}
}
@@ -0,0 +1,17 @@
package com.baeldung.thymeleaf.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class ThymeleafArrayController {
@GetMapping("/arrays")
public String arrayController(Model model) {
String[] continents = {"Africa", "Antarctica", "Asia", "Australia", "Europe", "North America", "Sourth America"};
model.addAttribute("continents", continents);
return "continents";
}
}
@@ -0,0 +1,30 @@
package com.baeldung.thymeleaf.formatter;
import java.text.ParseException;
import java.util.Locale;
import org.springframework.format.Formatter;
import org.thymeleaf.util.StringUtils;
/**
*
* Name formatter class that implements the Spring Formatter interface.
* Formats a name(String) and return the value with spaces replaced by commas.
*
*/
public class NameFormatter implements Formatter<String> {
@Override
public String print(String input, Locale locale) {
return formatName(input, locale);
}
@Override
public String parse(String input, Locale locale) throws ParseException {
return formatName(input, locale);
}
private String formatName(String input, Locale locale) {
return StringUtils.replace(input, " ", ",");
}
}
@@ -0,0 +1,29 @@
package com.baeldung.thymeleaf.model;
public class Book {
private int id;
private String name;
public Book(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@@ -0,0 +1,52 @@
package com.baeldung.thymeleaf.model;
import java.util.Date;
public class Course {
private String name;
private String description;
private Date startDate;
private Date endDate;
private String teacher;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
public String getTeacher() {
return teacher;
}
public void setTeacher(String teacher) {
this.teacher = teacher;
}
}
@@ -0,0 +1,61 @@
package com.baeldung.thymeleaf.model;
import java.util.Collections;
import java.util.List;
public class Page<T> {
private List<T> list;
private int pageSize = 0;
private int currentPage = 0;
private int totalPages = 0;
public Page(List<T> list, int pageSize, int currentPage) {
if (list.isEmpty()) {
this.list = list;
}
if (pageSize <= 0 || pageSize > list.size() || currentPage <= 0) {
throw new IllegalArgumentException("invalid page size or current page!");
}
this.pageSize = pageSize;
this.currentPage = currentPage;
if (list.size() % pageSize == 0) {
this.totalPages = list.size() / pageSize;
} else {
this.totalPages = list.size() / pageSize + 1;
}
int startItem = (currentPage - 1) * pageSize;
if (list.size() < startItem) {
this.list = Collections.emptyList();
}
int toIndex = Math.min(startItem + pageSize, list.size());
this.list = list.subList(startItem, toIndex);
}
public List<T> getList() {
return list;
}
public int getPageSize() {
return pageSize;
}
public int getCurrentPage() {
return currentPage;
}
public int getTotalPages() {
return totalPages;
}
}
@@ -0,0 +1,60 @@
package com.baeldung.thymeleaf.model;
import java.io.Serializable;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
/**
*
* Simple student POJO with few fields
*
*/
public class Student implements Serializable {
private static final long serialVersionUID = -8582553475226281591L;
@NotNull(message = "Student ID is required.")
@Min(value = 1000, message = "Student ID must be at least 4 digits.")
private Integer id;
@NotNull(message = "Student name is required.")
private String name;
@NotNull(message = "Student gender is required.")
private Character gender;
private Float percentage;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Character getGender() {
return gender;
}
public void setGender(Character gender) {
this.gender = gender;
}
public Float getPercentage() {
return percentage;
}
public void setPercentage(Float percentage) {
this.percentage = percentage;
}
}
@@ -0,0 +1,78 @@
package com.baeldung.thymeleaf.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
public class Teacher implements Serializable {
private static final long serialVersionUID = 946941572942270450L;
@NotNull(message = "Teacher ID is required.")
@Min(value = 1000, message = "Teacher ID must be at least 4 digits.")
private Integer id;
@NotNull(message = "Teacher name is required.")
private String name;
@NotNull(message = "Teacher gender is required.")
private String gender;
private boolean isActive;
private List<String> courses = new ArrayList<String>();
private String additionalSkills;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public boolean isActive() {
return isActive;
}
public void setActive(boolean isActive) {
this.isActive = isActive;
}
public List<String> getCourses() {
return courses;
}
public void setCourses(List<String> courses) {
this.courses = courses;
}
public String getAdditionalSkills() {
return additionalSkills;
}
public void setAdditionalSkills(String additionalSkills) {
this.additionalSkills = additionalSkills;
}
}
@@ -0,0 +1,38 @@
package com.baeldung.thymeleaf.service;
import java.util.Collections;
import java.util.List;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import com.baeldung.thymeleaf.model.Book;
import com.baeldung.thymeleaf.utils.BookUtils;
@Service
public class BookService {
final private List<Book> books = BookUtils.buildBooks();
public Page<Book> findPaginated(Pageable pageable) {
int pageSize = pageable.getPageSize();
int currentPage = pageable.getPageNumber();
int startItem = currentPage * pageSize;
List<Book> list;
if (books.size() < startItem) {
list = Collections.emptyList();
} else {
int toIndex = Math.min(startItem + pageSize, books.size());
list = books.subList(startItem, toIndex);
}
Page<Book> bookPage = new PageImpl<Book>(list, PageRequest.of(currentPage, pageSize), books.size());
return bookPage;
}
}
@@ -0,0 +1,8 @@
package com.baeldung.thymeleaf.utils;
public class ArrayUtil {
public static String[] array(String... args) {
return args;
}
}
@@ -0,0 +1,28 @@
package com.baeldung.thymeleaf.utils;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
import com.baeldung.thymeleaf.model.Book;
public class BookUtils {
private static List<Book> books = new ArrayList<Book>();
private static final int NUM_BOOKS = 30;
private static final int MIN_BOOK_NUM = 1000;
public static List<Book> buildBooks() {
if (books.isEmpty()) {
IntStream.range(0, NUM_BOOKS).forEach(n -> {
books.add(new Book(MIN_BOOK_NUM + n + 1, "Spring in Action"));
});
}
return books;
}
}
@@ -0,0 +1,34 @@
package com.baeldung.thymeleaf.utils;
import java.util.ArrayList;
import java.util.List;
import com.baeldung.thymeleaf.model.Student;
public class StudentUtils {
private static List<Student> students = new ArrayList<Student>();
public static List<Student> buildStudents() {
if (students.isEmpty()) {
Student student1 = new Student();
student1.setId(1001);
student1.setName("John Smith");
student1.setGender('M');
student1.setPercentage(Float.valueOf("80.45"));
students.add(student1);
Student student2 = new Student();
student2.setId(1002);
student2.setName("Jane Williams");
student2.setGender('F');
student2.setPercentage(Float.valueOf("60.25"));
students.add(student2);
}
return students;
}
}
@@ -0,0 +1,47 @@
package com.baeldung.thymeleaf.utils;
import java.util.ArrayList;
import java.util.List;
import com.baeldung.thymeleaf.model.Teacher;
public class TeacherUtils {
private static List<Teacher> teachers = new ArrayList<Teacher>();
public static List<Teacher> buildTeachers() {
if (teachers.isEmpty()) {
Teacher teacher1 = new Teacher();
teacher1.setId(2001);
teacher1.setName("Jane Doe");
teacher1.setGender("F");
teacher1.setActive(true);
teacher1.getCourses().add("Mathematics");
teacher1.getCourses().add("Physics");
teachers.add(teacher1);
Teacher teacher2 = new Teacher();
teacher2.setId(2002);
teacher2.setName("Lazy Dude");
teacher2.setGender("M");
teacher2.setActive(false);
teacher2.setAdditionalSkills("emergency responder");
teachers.add(teacher2);
Teacher teacher3 = new Teacher();
teacher3.setId(2002);
teacher3.setName("Micheal Jordan");
teacher3.setGender("M");
teacher3.setActive(true);
teacher3.getCourses().add("Sports");
teachers.add(teacher3);
}
return teachers;
}
}
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="WARN" />
<logger name="org.springframework.transaction" level="WARN" />
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>
@@ -0,0 +1,13 @@
msg.id=ID
msg.name=Name
msg.gender=Gender
msg.percent=Percentage
welcome.message=Welcome Student !!!
msg.AddStudent=Add Student
msg.ListStudents=List Students
msg.Home=Home
msg.ListTeachers=List Teachers
msg.ListBooks=List Books with paging
msg.courses=Courses
msg.skills=Skills
msg.active=Active
@@ -0,0 +1,49 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Arrays in Thymeleaf</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h3>THE CONTINENTS</h3>
<p>
We have a total of <span th:text="${continents.length}"></span>
continents in this world. Following is a list showing their order
based on the number of population in each of them.
</p>
<ol>
<li th:text="${continents[2]}"></li>
<li th:text="${continents[0]}"></li>
<li th:text="${continents[4]}"></li>
<li th:text="${continents[5]}"></li>
<li th:text="${continents[6]}"></li>
<li th:text="${continents[3]}"></li>
<li th:text="${continents[1]}"></li>
</ol>
<ul th:each="continent : ${continents}">
<li th:text="${continent}"></li>
</ul>
<h4 th:each="continent : ${continents}" th:text="${continent}"></h4>
<p>
The greatest <span th:text="${#arrays.length(continents)}"></span>
continents.
</p>
<p>
Europe is a continent: <span
th:text="${#arrays.contains(continents, 'Europe')}"></span>.
</p>
<p>
Array of continents is empty <span
th:text="${#arrays.isEmpty(continents)}"></span>.
</p>
</body>
</html>
@@ -0,0 +1,13 @@
body {
background-color: lightGray
}
.dark {
background-color: black;
color: white;
}
.light {
background-color: #f1f1f1;
color: #ccc;
}
@@ -0,0 +1,2 @@
var count = [[${students.size()}]];
alert("Number of students in group: " + count);
@@ -0,0 +1,8 @@
Dear [(${username})],
This is the list of our students:
[# th:each="s : ${students}"]
- [(${s.name})]. ID: [(${s.id})]
[/]
Thanks,
The Baeldung University
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Add Student</title>
</head>
<body>
<h1>Add Student</h1>
<form action="#" th:action="@{/saveStudent}" th:object="${student}"
method="post">
<ul>
<li th:errors="*{id}" />
<li th:errors="*{name}" />
<li th:errors="*{gender}" />
<li th:errors="*{percentage}" />
</ul>
<table border="1">
<tr>
<td><label th:text="#{msg.id}" /></td>
<td><input type="number" th:field="*{id}" /></td>
</tr>
<tr>
<td><label th:text="#{msg.name}" /></td>
<td><input type="text" th:field="*{name}" /></td>
</tr>
<tr>
<td><label th:text="#{msg.gender}" /></td>
<td><select th:field="*{gender}">
<option th:value="'M'" th:text="Male"></option>
<option th:value="'F'" th:text="Female"></option>
</select></td>
</tr>
<tr>
<td><label th:text="#{msg.percent}" /></td>
<td><select id="percentage" name="percentage">
<option th:each="i : ${#numbers.sequence(0, 100)}" th:value="${i}" th:text="${i}" th:selected="${i==75}"></option>
</select></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
@@ -0,0 +1,179 @@
<!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>
<style>
table {
border-collapse: collapse;
}
td {
border: 1px solid black;
padding: .5em;
}
</style>
</head>
<body>
<h1>'Truthy' and 'falsy' expressions</h1>
<ul>
<li>'true' is evaluated to <strong th:text="${#bools.isTrue(trueValue)}"></strong></li>
<li>'1' is evaluated to <strong th:text="${#bools.isTrue(one)}"></strong></li>
<li>non zero character is evaluated to <strong th:text="${#bools.isTrue(nonZeroCharacter)}"></strong></li>
<li>empty string is evaluated to <strong th:text="${#bools.isTrue(emptyString)}"></strong></li>
<li>the string "foo" is evaluated to <strong th:text="${#bools.isTrue(foo)}"></strong></li>
<li>an object is evaluated to <strong th:text="${#bools.isTrue(object)}"></strong></li>
<li>the array [0, 0] is evaluated to <strong th:text="${#bools.isTrue(arrayOfZeros)}"></strong></li>
<li>the array [0, 1] is evaluated to <strong th:text="${#bools.isTrue(arrayOfZeroAndOne)}"></strong></li>
<li>the array [1, 1] is evaluated to <strong th:text="${#bools.isTrue(arrayOfOnes)}"></strong></li>
<li>null value is evaluated to <strong th:text="${#bools.isTrue(nullValue)}"></strong></li>
<li>'false' is evaluated to <strong th:text="${#bools.isTrue(falseValue)}"></strong></li>
<li>'0' is evaluated to <strong th:text="${#bools.isTrue(zero)}"></strong></li>
<li>zero character is evaluated to <strong th:text="${#bools.isTrue(zeroCharacter)}"></strong></li>
<li>the string "false" is evaluated to <strong th:text="${#bools.isTrue(falseString)}"></strong></li>
<li>the string "no" is evaluated to <strong th:text="${#bools.isTrue(no)}"></strong></li>
<li>the string "off" is evaluated to <strong th:text="${#bools.isTrue(off)}"></strong></li>
</ul>
<h1>Using booleans as rendering conditions</h1>
<table>
<tr>
<td></td>
<td>th:if</td>
<td>th:unless</td>
</tr>
<tr>
<td>true</td>
<td><span th:if="${true}">will be rendered</span></td>
<td><span th:unless="${true}">won't be rendered</span></td>
</tr>
<tr>
<td>false</td>
<td><span th:if="${false}">won't be rendered</span></td>
<td><span th:unless="${false}">will be rendered</span></td>
</tr>
</table>
<h1>Boolean and conditional operators</h1>
<table>
<tr>
<td>A</td>
<td>B</td>
<td>${A or B}</td>
<td>${A} or ${B}</td>
<td>${A and B}</td>
<td>${A} and ${B}</td>
<td>${!A}</td>
<td>!${A}</td>
<td>${not A}</td>
<td>not ${A}</td>
</tr>
<tr>
<td>true</td>
<td>true</td>
<td th:text="${true or true}"></td>
<td th:text="${true} or ${true}"></td>
<td th:text="${true and true}"></td>
<td th:text="${true} and ${true}"></td>
<td th:text="${!true}"></td>
<td th:text="!${true}"></td>
<td th:text="${not true}"></td>
<td th:text="not ${true}"></td>
</tr>
<tr>
<td>true</td>
<td>false</td>
<td th:text="${true or false}"></td>
<td th:text="${true} or ${false}"></td>
<td th:text="${true and false}"></td>
<td th:text="${true} and ${false}"></td>
<td th:text="${!true}"></td>
<td th:text="!${true}"></td>
<td th:text="${not true}"></td>
<td th:text="not ${true}"></td>
</tr>
<tr>
<td>false</td>
<td>true</td>
<td th:text="${false or true}"></td>
<td th:text="${false} or ${true}"></td>
<td th:text="${false and true}"></td>
<td th:text="${false} and ${true}"></td>
<td th:text="${!false}"></td>
<td th:text="!${false}"></td>
<td th:text="${not false}"></td>
<td th:text="not ${false}"></td>
</tr>
<tr>
<td>false</td>
<td>false</td>
<td th:text="${false or false}"></td>
<td th:text="${false} or ${false}"></td>
<td th:text="${false and false}"></td>
<td th:text="${false} and ${false}"></td>
<td th:text="${!false}"></td>
<td th:text="!${false}"></td>
<td th:text="${not false}"></td>
<td th:text="not ${false}"></td>
</tr>
</table>
<ul>
<li> the result of "true ? 'then'" is <strong th:text="true ? 'then'"></strong></li>
<li> the result of "false ? 'then'" is <strong th:text="false ? 'then'"></strong></li>
<li> the result of "true ? 'then' : 'else'" is <strong th:text="true ? 'then' : 'else'"></strong></li>
<li> the result of "false ? 'then' : 'else'" is <strong th:text="false ? 'then' : 'else'"></strong></li>
<li> the result of "'foo' ?: 'bar'" is <strong th:text="'foo' ?: 'bar'"></strong></li>
<li> the result of "null ?: 'bar'" is <strong th:text="null ?: 'bar'"></strong></li>
<li> the result of "0 ?: 'bar'" is <strong th:text="0 ?: 'bar'"></strong></li>
<li> the result of "1 ?: 'bar'" is <strong th:text="1 ?: 'bar'"></strong></li>
</ul>
<ul>
<li><span th:if="${isRaining or isCold}">The weather is bad</span></li>
<li><span th:if="${isRaining} or ${isCold}">The weather is bad</span></li>
<li><span th:if="${isSunny and isWarm}">The weather is good</span></li>
<li><span th:if="${isSunny} and ${isWarm}">The weather is good</span></li>
<li><span th:if="${not isCold}">It's warm</span></li>
<li><span th:if="${!isCold}">It's warm</span></li>
<li><span th:if="not ${isCold}">It's warm</span></li>
<li><span th:if="!${isCold}">It's warm</span></li>
<li>It's <span th:text="${isCold} ? 'cold' : 'warm'"></span></li>
<li><span th:text="${isRaining or isCold} ? 'The weather is bad'"></span></li>
</ul>
<h1>#bools utility object</h1>
<table>
<tr>
<td>Array</td>
<td>#bools.arrayIsTrue()</td>
<td>#bools.arrayIsFalse()</td>
<td>#bools.arrayAnd()</td>
<td>#bools.arrayOr()</td>
</tr>
<tr>
<td>[0, 0]</td>
<td th:text="${#strings.arrayJoin(#bools.arrayIsTrue(arrayOfZeros), ',')}"></td>
<td th:text="${#strings.arrayJoin(#bools.arrayIsFalse(arrayOfZeros), ',')}"></td>
<td th:text="${#bools.arrayAnd(arrayOfZeros)}"></td>
<td th:text="${#bools.arrayOr(arrayOfZeros)}"></td>
</tr>
<tr>
<td>[0, 1]</td>
<td th:text="${#strings.arrayJoin(#bools.arrayIsTrue(arrayOfZeroAndOne), ',')}"></td>
<td th:text="${#strings.arrayJoin(#bools.arrayIsFalse(arrayOfZeroAndOne), ',')}"></td>
<td th:text="${#bools.arrayAnd(arrayOfZeroAndOne)}"></td>
<td th:text="${#bools.arrayOr(arrayOfZeroAndOne)}"></td>
</tr>
<tr>
<td>[1, 1]</td>
<td th:text="${#strings.arrayJoin(#bools.arrayIsTrue(arrayOfOnes), ',')}"></td>
<td th:text="${#strings.arrayJoin(#bools.arrayIsFalse(arrayOfOnes), ',')}"></td>
<td th:text="${#bools.arrayAnd(arrayOfOnes)}"></td>
<td th:text="${#bools.arrayOr(arrayOfOnes)}"></td>
</tr>
</table>
</body>
</html>
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{template.html}">
<head>
<title>Layout Dialect Example</title>
</head>
<body>
<section layout:fragment="custom-content">
<p>This is a custom content that you can provide</p>
</section>
<footer>
<p layout:fragment="custom-footer">This is some footer content
that you can change</p>
</footer>
</body>
</html>
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Register for course</title>
<script>
function validateForm() {
var e = document.getElementById("course");
var value = e.options[e.selectedIndex].value;
if (value == '') {
var error = document.getElementById("errormesg");
error.textContent = e.getAttribute('data-validation-message');
return false;
}
return true;
}
</script>
</head>
<body>
<h3>Register Here</h3>
<form action="#" th:action="@{/registerCourse}" th:object="${course}"
method="post" onsubmit="return validateForm();">
<span id="errormesg" style="color: red"></span> <span
style="font-weight: bold" th:text="${successMessage}"></span>
<table>
<tr>
<td><label th:text="#{msg.courseName}+': '"></label></td>
<td><select id="course" th:field="*{name}"
th:data-validation-message="#{msg.courseName.mandatory}">
<option th:value="''" th:text="'Select'"></option>
<option th:value="'Mathematics'" th:text="'Mathematics'"></option>
<option th:value="'History'" th:text="'History'"></option>
</select></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
</html>
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<form action="http://localhost:8080/spring-thymeleaf/saveStudent" method="post">
<input type="hidden" name="payload" value="CSRF attack!"/>
<input type="submit" />
</form>
</body>
</html>
@@ -0,0 +1,35 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Baeldung - dates</title>
</head>
<body>
<h1>Format ISO</h1>
<p th:text="${#dates.formatISO(standardDate)}"></p>
<p th:text="${#temporals.formatISO(localDateTime)}"></p>
<p th:text="${#temporals.formatISO(localDate)}"></p>
<p th:text="${#temporals.formatISO(timestamp)}"></p>
<h1>Format manually</h1>
<p th:text="${#dates.format(standardDate, 'dd-MM-yyyy HH:mm')}"></p>
<p th:text="${#temporals.format(localDateTime, 'dd-MM-yyyy HH:mm')}"></p>
<p th:text="${#temporals.format(localDate, 'MM-yyyy')}"></p>
<h1>Show only which day of a week</h1>
<p th:text="${#dates.day(standardDate)}"></p>
<p th:text="${#temporals.day(localDateTime)}"></p>
<p th:text="${#temporals.day(localDate)}"></p>
<h1>Show the name of the week day</h1>
<p th:text="${#dates.dayOfWeekName(standardDate)}"></p>
<p th:text="${#temporals.dayOfWeekName(localDateTime)}"></p>
<p th:text="${#temporals.dayOfWeekName(localDate)}"></p>
<h1>Show the second of the day</h1>
<p th:text="${#dates.second(standardDate)}"></p>
<p th:text="${#temporals.second(localDateTime)}"></p>
</body>
</html>
@@ -0,0 +1,12 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Thymeleaf Fragments: home</title>
<!--/*/ <th:block th:include="fragments/general.html :: headerfiles"></th:block> /*/-->
</head>
<body>
<header th:insert="fragments/general.html :: header"> </header>
<p>Go to the next page to see fragments in action</p>
<div th:replace="fragments/general.html :: footer"></div>
</body>
</html>
@@ -0,0 +1,12 @@
<div th:fragment="formField (field, value, size)">
<div>
<label th:for="${#strings.toLowerCase(field)}"> <span
th:text="${field}">Field</span>
</label>
</div>
<div>
<input type="text" th:id="${#strings.toLowerCase(field)}"
th:name="${#strings.toLowerCase(field)}" th:value="${value}"
th:size="${size}">
</div>
</div>
@@ -0,0 +1,23 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head th:fragment="headerfiles">
<meta charset="UTF-8" />
<link th:href="@{/css/styles.css}" rel="stylesheet">
</head>
<body>
<div th:fragment="header">
<h1>Thymeleaf Fragments sample</h1>
</div>
<p>Go to the next page to see fragments in action</p>
<aside>
<div>This is a sidebar</div>
</aside>
<div class="another">This is another sidebar</div>
<footer th:fragment="footer">
<a th:href="@{/fragments}">Fragments Index</a> |
<a th:href="@{/markup}">Markup inclussion</a> |
<a th:href="@{/params}">Fragment params</a> |
<a th:href="@{/other}">Other</a>
</footer>
</body>
</html>
@@ -0,0 +1,2 @@
<div th:fragment="dataPresent">Data received</div>
<div th:fragment="noData">No data</div>
@@ -0,0 +1 @@
<div><p id="parag">This is a subtitle</p></div>
@@ -0,0 +1,14 @@
<table>
<thead th:fragment="fields(theadFields)">
<tr th:replace="${theadFields}">
</tr>
</thead>
<tbody th:fragment="tableBody(tableData)">
<tr th:each="row: ${tableData}">
<td th:text="${row.id}">0</td>
<td th:text="${row.name}">Name</td>
</tr>
</tbody>
<tfoot>
</tfoot>
</table>
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
th:with="lang=${#locale.language}" th:lang="${lang}">
<head>
<title>Home</title>
</head>
<body>
<h1>
<span th:text="#{welcome.message}" />
</h1>
<p>
Current time is <span th:text="${serverTime}" />
</p>
<table>
<tr>
<td><a th:href="@{/addStudent}" th:text="#{msg.AddStudent}" /></td>
</tr>
<tr>
<td><a th:href="@{/listStudents}" th:text="#{msg.ListStudents}" /></td>
</tr>
<tr>
<td><a th:href="@{/listTeachers}" th:text="#{msg.ListTeachers}" /></td>
</tr>
<tr>
<td><a th:href="@{/listBooks}" th:text="#{msg.ListBooks}" /></td>
</tr>
<tr>
<td><a th:href="@{/registerCourse}" th:text="#{msg.CourseRegistration}" /></td>
</tr>
</table>
</body>
</html>
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Inlining example</title>
</head>
<body>
<p>Title of tutorial: [[${title}]]</p>
<p>Description: [(${description})]</p>
</body>
</html>
@@ -0,0 +1,58 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<style>
.pagination {
display: inline-block;
}
.pagination a {
color: black;
float: left;
padding: 5px 5px;
text-decoration: none;
}
.pagination a.active {
background-color: gray;
color: white;
border-radius: 2px;
}
</style>
<head>
<title>Book List</title>
</head>
<body>
<h1>Book List</h1>
<table border="1">
<thead>
<tr>
<th th:text="#{msg.id}" />
<th th:text="#{msg.name}" />
</tr>
</thead>
<tbody>
<tr th:each="book, iStat : ${bookPage.content}"
th:style="${iStat.odd}? 'font-weight: bold;'"
th:alt-title="${iStat.even}? 'even' : 'odd'">
<td th:text="${book.id}" />
<td th:text="${book.name}" />
</tr>
</tbody>
</table>
<div th:if="${bookPage.totalPages > 0}" class="pagination"
th:each="pageNumber : ${pageNumbers}">
<a th:href="@{/listBooks(size=${bookPage.size}, page=${pageNumber})}"
th:text=${pageNumber}
th:class="${pageNumber==bookPage.number + 1} ? active"></a>
</div>
<div>
<p>
<a th:href="@{/}" th:text="#{msg.Home}"></a>
</p>
</div>
</body>
</html>
@@ -0,0 +1,53 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Student List</title>
</head>
<script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
<script>
$(document).ready(function() {
$.ajax({
url : "/spring-thymeleaf/js",
});
});
</script>
<body>
<h1>Student List</h1>
<table border="1">
<thead>
<tr>
<th th:text="#{msg.id}" />
<th th:text="#{msg.name}" />
<th th:text="#{msg.gender}" />
<th th:text="#{msg.percent}" />
<th th:text="index" />
<th th:text="count" />
<th th:text="first" />
<th th:text="last" />
</tr>
</thead>
<tbody>
<tr th:each="student, iStat : ${students}" th:style="${iStat.odd}? 'font-weight: bold;'" th:alt-title="${iStat.even}? 'even' : 'odd'">
<td th:text="${student.id}" />
<td th:text="${student.name}" />
<td th:switch="${student.gender}"><span th:case="'M'"
th:text="Male" /> <span th:case="'F'" th:text="Female" /></td>
<td th:text="${#conversions.convert(student.percentage, 'Integer')}" />
<td th:text="${iStat.index}" />
<td th:text="${iStat.count}" />
<td th:text="${iStat.first}" />
<td th:text="${iStat.last}" />
</tr>
</tbody>
</table>
<div>
<p>
<a th:href="@{/}" th:text="#{msg.Home}" />
</p>
</div>
</body>
</html>
@@ -0,0 +1,50 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Teacher List</title>
</head>
<body>
<h1>Teacher List</h1>
<table border="1">
<thead>
<tr>
<th th:text="#{msg.id}" />
<th th:text="#{msg.name}" />
<th th:text="#{msg.gender}" />
<th th:text="#{msg.active}" />
<th th:text="#{msg.courses}" />
<th th:text="#{msg.skills}" />
</tr>
</thead>
<tbody>
<tr th:each="teacher: ${teachers}">
<td th:text="${teacher.id}" />
<td th:text="${{teacher.name}}" />
<td><span th:if="${teacher.gender == 'F'}">Female</span> <span
th:unless="${teacher.gender == 'F'}">Male</span></td>
<td th:text="${teacher.active} ? 'ACTIVE' : 'RETIRED'" />
<td th:switch="${#lists.size(teacher.courses)}"><span
th:case="'0'">NO COURSES YET!</span> <span th:case="'1'"
th:text="${teacher.courses[0]}"></span>
<div th:case="*">
<div th:each="course: ${teacher.courses}" th:text="${course}"></div>
</div></td>
<td th:text="*{teacher.additionalSkills}?: 'UNKNOWN'" />
</tr>
</tbody>
</table>
<div>
<p>
<a th:href="@{/}" th:text="#{msg.Home}" />
</p>
</div>
</body>
</html>
@@ -0,0 +1,13 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Thymeleaf Fragments: markup</title>
</head>
<body>
<header th:insert="fragments/general.html :: header"> </header>
<div th:replace="fragments/general.html :: aside"></div>
<div th:replace="fragments/general.html :: div.another"></div>
<div th:replace="fragments/general.html :: footer"></div>
</body>
</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,27 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Thymeleaf Fragments: other</title>
</head>
<body>
<header th:replace="fragments/general.html :: header"> </header>
<div
th:replace="${#lists.size(data) > 0} ?
~{fragments/menus.html :: dataPresent} :
~{fragments/menus.html :: noData}">
</div>
<table>
<thead
th:replace="fragments/tables.html :: fields(~{ :: .myFields})">
<tr class="myFields">
<th>Id</th>
<th>Name</th>
</tr>
</thead>
<div
th:replace="fragments/tables.html :: tableBody(tableData=${data})"></div>
</table>
<div th:replace="fragments/general.html :: footer"></div>
</body>
</html>
@@ -0,0 +1,14 @@
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Thymeleaf Fragments: params</title>
</head>
<body>
<header th:insert="fragments/general.html :: header"> </header>
<div
th:replace="fragments/forms.html :: formField(field='Name', value='John Doe',size='40')">
</div>
<div th:replace="fragments/general.html :: footer"></div>
</body>
</html>
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<title layout:title-pattern="$LAYOUT_TITLE - $CONTENT_TITLE">Baeldung</title>
<script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
</head>
<body>
<header>
<h1>New dialect example</h1>
</header>
<section layout:fragment="custom-content">
<p>Your page content goes here</p>
</section>
<footer>
<p>My custom footer</p>
<p layout:fragment="custom-footer">Your custom footer here</p>
</footer>
</body>
</html>
@@ -0,0 +1,68 @@
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 ExpressionUtilityObjectsControllerIntegrationTest {
@Autowired
WebApplicationContext wac;
@Autowired
MockHttpSession session;
private MockMvc mockMvc;
@Autowired
private Filter springSecurityFilterChain;
private RequestPostProcessor testUser() {
return user("user1").password("user1Pass").roles("USER");
}
@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).addFilters(springSecurityFilterChain).build();
}
@Test
public void testGetObjects() throws Exception {
mockMvc.perform(get("/objects").with(testUser()).with(csrf())).andExpect(status().isOk()).andExpect(view().name("objects.html"));
}
@Test
public void testDates() throws Exception {
mockMvc.perform(get("/dates").with(testUser()).with(csrf())).andExpect(status().isOk()).andExpect(view().name("dates.html"));
}
@Test
public void testTeachers() throws Exception {
mockMvc.perform(get("/listTeachers").with(testUser()).with(csrf())).andExpect(status().isOk()).andExpect(view().name("listTeachers.html"));
}
}
@@ -0,0 +1,87 @@
package com.baeldung.thymeleaf.controller;
import static org.hamcrest.Matchers.containsString;
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.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
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 FragmentsIntegrationTest {
@Autowired
WebApplicationContext wac;
@Autowired
MockHttpSession session;
private MockMvc mockMvc;
@Autowired
private Filter springSecurityFilterChain;
private RequestPostProcessor testUser() {
return user("user1").password("user1Pass").roles("USER");
}
@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).addFilters(springSecurityFilterChain).build();
}
@Test
public void whenAccessingFragmentsRoute_thenViewHasExpectedContent() throws Exception {
this.mockMvc
.perform(get("/fragments").with(testUser()))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("<title>Thymeleaf Fragments: home</title>")));
}
@Test
public void whenAccessingParamsRoute_thenViewHasExpectedContent() throws Exception {
this.mockMvc
.perform(get("/params").with(testUser()))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("<span>Name</span>")));
}
@Test
public void whenAccessingMarkupRoute_thenViewHasExpectedContent() throws Exception {
this.mockMvc
.perform(get("/markup").with(testUser()))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("<div class=\"another\">This is another sidebar</div>")));
}
@Test
public void whenAccessingOtherRoute_thenViewHasExpectedContent() throws Exception {
this.mockMvc
.perform(get("/other").with(testUser()))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().string(containsString("<td>John Smith</td>")));
}
}
@@ -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 LayoutDialectControllerIntegrationTest {
@Autowired
WebApplicationContext wac;
@Autowired
MockHttpSession session;
private MockMvc mockMvc;
@Autowired
private Filter springSecurityFilterChain;
private 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("/layout").with(testUser()).with(csrf())).andExpect(status().isOk()).andExpect(view().name("content.html"));
}
}
@@ -0,0 +1,22 @@
package org.baeldung;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
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 SpringContextIntegrationTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}
@@ -0,0 +1,22 @@
package org.baeldung;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
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 SpringContextTest {
@Test
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
}
}
@@ -0,0 +1,78 @@
package org.baeldung.security.csrf;
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.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
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.http.MediaType;
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 CsrfEnabledIntegrationTest {
@Autowired
WebApplicationContext wac;
@Autowired
MockHttpSession session;
private MockMvc mockMvc;
@Autowired
private Filter springSecurityFilterChain;
private RequestPostProcessor testUser() {
return user("user1").password("user1Pass").roles("USER");
}
@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).addFilters(springSecurityFilterChain).build();
}
@Test
public void addStudentWithoutCSRF() throws Exception {
mockMvc.perform(post("/saveStudent").contentType(MediaType.APPLICATION_JSON).param("id", "1234567").param("name", "Joe").param("gender", "M").with(testUser())).andExpect(status().isForbidden());
}
@Test
public void addStudentWithCSRF() throws Exception {
mockMvc.perform(post("/saveStudent").contentType(MediaType.APPLICATION_JSON).param("id", "1234567").param("name", "Joe").param("gender", "M").with(testUser()).with(csrf())).andExpect(status().isOk());
}
@Test
public void htmlInliningTest() throws Exception {
mockMvc.perform(get("/html").with(testUser()).with(csrf())).andExpect(status().isOk()).andExpect(view().name("inliningExample.html"));
}
@Test
public void jsInliningTest() throws Exception {
mockMvc.perform(get("/js").with(testUser()).with(csrf())).andExpect(status().isOk()).andExpect(view().name("studentCheck.js"));
}
@Test
public void plainInliningTest() throws Exception {
mockMvc.perform(get("/plain").with(testUser()).with(csrf())).andExpect(status().isOk()).andExpect(view().name("studentsList.txt"));
}
}