BAEL-1562 - Working with Fragments in Thymeleaf (#3868)
* BAEL-1562 - Thymeleaf sample working * BAEL-1562 Code added for Fragments sample * BAEL-1562 - Last correction for the test * BAEL-1562 - Thymeleaf sample working * BAEL-1562 Code added for Fragments sample * BAEL-1562 - Last correction for the test
This commit is contained in:
parent
b20a3a1097
commit
5edb9acb95
|
@ -27,3 +27,6 @@ http://localhost:8082/spring-thymeleaf/addStudent/
|
||||||
http://localhost:8082/spring-thymeleaf/listStudents/
|
http://localhost:8082/spring-thymeleaf/listStudents/
|
||||||
|
|
||||||
The first URL is the home page of the application. The home page has links to the other two pages.
|
The first URL is the home page of the application. The home page has links to the other two pages.
|
||||||
|
|
||||||
|
### Security
|
||||||
|
The user/password required is: user1/user1Pass
|
||||||
|
|
|
@ -1,210 +1,210 @@
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<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">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>spring-thymeleaf</artifactId>
|
<artifactId>spring-thymeleaf</artifactId>
|
||||||
<version>0.1-SNAPSHOT</version>
|
<version>0.1-SNAPSHOT</version>
|
||||||
<packaging>war</packaging>
|
<packaging>war</packaging>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-modules</artifactId>
|
<artifactId>parent-modules</artifactId>
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<java-version>1.8</java-version>
|
<java-version>1.8</java-version>
|
||||||
<!-- spring -->
|
<!-- spring -->
|
||||||
<org.springframework-version>4.3.4.RELEASE</org.springframework-version>
|
<org.springframework-version>4.3.4.RELEASE</org.springframework-version>
|
||||||
<springframework-security.version>4.2.0.RELEASE</springframework-security.version>
|
<springframework-security.version>4.2.0.RELEASE</springframework-security.version>
|
||||||
<javax.servlet-version>3.1.0</javax.servlet-version>
|
<javax.servlet-version>3.1.0</javax.servlet-version>
|
||||||
<!-- thymeleaf -->
|
<!-- thymeleaf -->
|
||||||
<org.thymeleaf-version>3.0.3.RELEASE</org.thymeleaf-version>
|
<org.thymeleaf-version>3.0.3.RELEASE</org.thymeleaf-version>
|
||||||
<org.thymeleaf.extras-version>3.0.0.RELEASE</org.thymeleaf.extras-version>
|
<org.thymeleaf.extras-version>3.0.0.RELEASE</org.thymeleaf.extras-version>
|
||||||
<thymeleaf-layout-dialect.version>2.1.2</thymeleaf-layout-dialect.version>
|
<thymeleaf-layout-dialect.version>2.1.2</thymeleaf-layout-dialect.version>
|
||||||
<!-- validation -->
|
<!-- validation -->
|
||||||
<javax.validation-version>1.1.0.Final</javax.validation-version>
|
<javax.validation-version>1.1.0.Final</javax.validation-version>
|
||||||
<hibernate-validator.version>5.3.3.Final</hibernate-validator.version>
|
<hibernate-validator.version>5.3.3.Final</hibernate-validator.version>
|
||||||
<org.hibernate-version>5.2.5.Final</org.hibernate-version>
|
<org.hibernate-version>5.2.5.Final</org.hibernate-version>
|
||||||
|
|
||||||
<!-- Maven plugins -->
|
<!-- Maven plugins -->
|
||||||
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
||||||
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
|
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
|
||||||
<tomcat7-maven-plugin.version>2.2</tomcat7-maven-plugin.version>
|
<tomcat7-maven-plugin.version>2.2</tomcat7-maven-plugin.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<!-- Spring -->
|
<!-- Spring -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-context</artifactId>
|
<artifactId>spring-context</artifactId>
|
||||||
<version>${org.springframework-version}</version>
|
<version>${org.springframework-version}</version>
|
||||||
<exclusions>
|
<exclusions>
|
||||||
<!-- Exclude Commons Logging in favor of SLF4j -->
|
<!-- Exclude Commons Logging in favor of SLF4j -->
|
||||||
<exclusion>
|
<exclusion>
|
||||||
<groupId>commons-logging</groupId>
|
<groupId>commons-logging</groupId>
|
||||||
<artifactId>commons-logging</artifactId>
|
<artifactId>commons-logging</artifactId>
|
||||||
</exclusion>
|
</exclusion>
|
||||||
</exclusions>
|
</exclusions>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-webmvc</artifactId>
|
<artifactId>spring-webmvc</artifactId>
|
||||||
<version>${org.springframework-version}</version>
|
<version>${org.springframework-version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- Spring Security -->
|
<!-- Spring Security -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.security</groupId>
|
<groupId>org.springframework.security</groupId>
|
||||||
<artifactId>spring-security-web</artifactId>
|
<artifactId>spring-security-web</artifactId>
|
||||||
<version>${springframework-security.version}</version>
|
<version>${springframework-security.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.security</groupId>
|
<groupId>org.springframework.security</groupId>
|
||||||
<artifactId>spring-security-config</artifactId>
|
<artifactId>spring-security-config</artifactId>
|
||||||
<version>${springframework-security.version}</version>
|
<version>${springframework-security.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- Thymeleaf -->
|
<!-- Thymeleaf -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.thymeleaf</groupId>
|
<groupId>org.thymeleaf</groupId>
|
||||||
<artifactId>thymeleaf</artifactId>
|
<artifactId>thymeleaf</artifactId>
|
||||||
<version>${org.thymeleaf-version}</version>
|
<version>${org.thymeleaf-version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.thymeleaf</groupId>
|
<groupId>org.thymeleaf</groupId>
|
||||||
<artifactId>thymeleaf-spring4</artifactId>
|
<artifactId>thymeleaf-spring4</artifactId>
|
||||||
<version>${org.thymeleaf-version}</version>
|
<version>${org.thymeleaf-version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- https://mvnrepository.com/artifact/nz.net.ultraq.thymeleaf/thymeleaf-layout-dialect -->
|
<!-- https://mvnrepository.com/artifact/nz.net.ultraq.thymeleaf/thymeleaf-layout-dialect -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>nz.net.ultraq.thymeleaf</groupId>
|
<groupId>nz.net.ultraq.thymeleaf</groupId>
|
||||||
<artifactId>thymeleaf-layout-dialect</artifactId>
|
<artifactId>thymeleaf-layout-dialect</artifactId>
|
||||||
<version>${thymeleaf-layout-dialect.version}</version>
|
<version>${thymeleaf-layout-dialect.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.thymeleaf.extras</groupId>
|
<groupId>org.thymeleaf.extras</groupId>
|
||||||
<artifactId>thymeleaf-extras-java8time</artifactId>
|
<artifactId>thymeleaf-extras-java8time</artifactId>
|
||||||
<version>${org.thymeleaf.extras-version}</version>
|
<version>${org.thymeleaf.extras-version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- Servlet -->
|
<!-- Servlet -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax.servlet</groupId>
|
<groupId>javax.servlet</groupId>
|
||||||
<artifactId>javax.servlet-api</artifactId>
|
<artifactId>javax.servlet-api</artifactId>
|
||||||
<version>${javax.servlet-version}</version>
|
<version>${javax.servlet-version}</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- Validation -->
|
<!-- Validation -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax.validation</groupId>
|
<groupId>javax.validation</groupId>
|
||||||
<artifactId>validation-api</artifactId>
|
<artifactId>validation-api</artifactId>
|
||||||
<version>${javax.validation-version}</version>
|
<version>${javax.validation-version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.hibernate</groupId>
|
<groupId>org.hibernate</groupId>
|
||||||
<artifactId>hibernate-validator</artifactId>
|
<artifactId>hibernate-validator</artifactId>
|
||||||
<version>${hibernate-validator.version}</version>
|
<version>${hibernate-validator.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- test scoped -->
|
<!-- test scoped -->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-test</artifactId>
|
<artifactId>spring-test</artifactId>
|
||||||
<version>${org.springframework-version}</version>
|
<version>${org.springframework-version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-test -->
|
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-test -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.security</groupId>
|
<groupId>org.springframework.security</groupId>
|
||||||
<artifactId>spring-security-test</artifactId>
|
<artifactId>spring-security-test</artifactId>
|
||||||
<version>${springframework-security.version}</version>
|
<version>${springframework-security.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-war-plugin</artifactId>
|
<artifactId>maven-war-plugin</artifactId>
|
||||||
<version>${maven-war-plugin.version}</version>
|
<version>${maven-war-plugin.version}</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.codehaus.cargo</groupId>
|
<groupId>org.codehaus.cargo</groupId>
|
||||||
<artifactId>cargo-maven2-plugin</artifactId>
|
<artifactId>cargo-maven2-plugin</artifactId>
|
||||||
<version>${cargo-maven2-plugin.version}</version>
|
<version>${cargo-maven2-plugin.version}</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<wait>true</wait>
|
<wait>true</wait>
|
||||||
<container>
|
<container>
|
||||||
<containerId>jetty8x</containerId>
|
<containerId>jetty8x</containerId>
|
||||||
<type>embedded</type>
|
<type>embedded</type>
|
||||||
<systemProperties>
|
<systemProperties>
|
||||||
</systemProperties>
|
</systemProperties>
|
||||||
</container>
|
</container>
|
||||||
<configuration>
|
<configuration>
|
||||||
<properties>
|
<properties>
|
||||||
<cargo.servlet.port>8082</cargo.servlet.port>
|
<cargo.servlet.port>8082</cargo.servlet.port>
|
||||||
</properties>
|
</properties>
|
||||||
</configuration>
|
</configuration>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.tomcat.maven</groupId>
|
<groupId>org.apache.tomcat.maven</groupId>
|
||||||
<artifactId>tomcat7-maven-plugin</artifactId>
|
<artifactId>tomcat7-maven-plugin</artifactId>
|
||||||
<version>${tomcat7-maven-plugin.version}</version>
|
<version>${tomcat7-maven-plugin.version}</version>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<id>tomcat-run</id>
|
<id>tomcat-run</id>
|
||||||
<goals>
|
<goals>
|
||||||
<goal>exec-war-only</goal>
|
<goal>exec-war-only</goal>
|
||||||
</goals>
|
</goals>
|
||||||
<phase>package</phase>
|
<phase>package</phase>
|
||||||
<configuration>
|
<configuration>
|
||||||
<path>/</path>
|
<path>/</path>
|
||||||
<enableNaming>false</enableNaming>
|
<enableNaming>false</enableNaming>
|
||||||
<finalName>webapp.jar</finalName>
|
<finalName>webapp.jar</finalName>
|
||||||
<charset>utf-8</charset>
|
<charset>utf-8</charset>
|
||||||
</configuration>
|
</configuration>
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<profiles>
|
<profiles>
|
||||||
<profile>
|
<profile>
|
||||||
<id>integration</id>
|
<id>integration</id>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<phase>integration-test</phase>
|
<phase>integration-test</phase>
|
||||||
<goals>
|
<goals>
|
||||||
<goal>test</goal>
|
<goal>test</goal>
|
||||||
</goals>
|
</goals>
|
||||||
<configuration>
|
<configuration>
|
||||||
<excludes>
|
<excludes>
|
||||||
<exclude>**/*LiveTest.java</exclude>
|
<exclude>**/*LiveTest.java</exclude>
|
||||||
</excludes>
|
</excludes>
|
||||||
<includes>
|
<includes>
|
||||||
<include>**/*IntegrationTest.java</include>
|
<include>**/*IntegrationTest.java</include>
|
||||||
</includes>
|
</includes>
|
||||||
</configuration>
|
</configuration>
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
<configuration>
|
<configuration>
|
||||||
<systemPropertyVariables>
|
<systemPropertyVariables>
|
||||||
<test.mime>json</test.mime>
|
<test.mime>json</test.mime>
|
||||||
</systemPropertyVariables>
|
</systemPropertyVariables>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
</profile>
|
</profile>
|
||||||
</profiles>
|
</profiles>
|
||||||
</project>
|
</project>
|
|
@ -77,14 +77,14 @@ public class WebMVCConfig extends WebMvcConfigurerAdapter implements Application
|
||||||
return resolver;
|
return resolver;
|
||||||
}
|
}
|
||||||
|
|
||||||
private TemplateEngine templateEngine(ITemplateResolver templateResolver) {
|
private TemplateEngine templateEngine(ITemplateResolver templateResolver) {
|
||||||
SpringTemplateEngine engine = new SpringTemplateEngine();
|
SpringTemplateEngine engine = new SpringTemplateEngine();
|
||||||
engine.addDialect(new LayoutDialect(new GroupingStrategy()));
|
engine.addDialect(new LayoutDialect(new GroupingStrategy()));
|
||||||
engine.addDialect(new Java8TimeDialect());
|
engine.addDialect(new Java8TimeDialect());
|
||||||
engine.setTemplateResolver(templateResolver);
|
engine.setTemplateResolver(templateResolver);
|
||||||
engine.setTemplateEngineMessageSource(messageSource());
|
engine.setTemplateEngineMessageSource(messageSource());
|
||||||
return engine;
|
return engine;
|
||||||
}
|
}
|
||||||
|
|
||||||
private ITemplateResolver htmlTemplateResolver() {
|
private ITemplateResolver htmlTemplateResolver() {
|
||||||
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
|
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
|
||||||
|
@ -142,7 +142,8 @@ public class WebMVCConfig extends WebMvcConfigurerAdapter implements Application
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||||
registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");
|
registry.addResourceHandler("/resources/**", "/css/**")
|
||||||
|
.addResourceLocations("/WEB-INF/resources/", "/WEB-INF/css/");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -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";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,9 +1,9 @@
|
||||||
msg.id=ID
|
msg.id=ID
|
||||||
msg.name=Name
|
msg.name=Name
|
||||||
msg.gender=Gender
|
msg.gender=Gender
|
||||||
msg.percent=Percentage
|
msg.percent=Percentage
|
||||||
welcome.message=Welcome Student !!!
|
welcome.message=Welcome Student !!!
|
||||||
msg.AddStudent=Add Student
|
msg.AddStudent=Add Student
|
||||||
msg.ListStudents=List Students
|
msg.ListStudents=List Students
|
||||||
msg.Home=Home
|
msg.Home=Home
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
body {
|
||||||
|
background-color: lightGray
|
||||||
|
}
|
||||||
|
|
||||||
|
.dark {
|
||||||
|
background-color: black;
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
.light {
|
||||||
|
background-color: #f1f1f1;
|
||||||
|
color: #ccc;
|
||||||
|
}
|
|
@ -1,43 +1,43 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml"
|
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||||
xmlns:th="http://www.thymeleaf.org">
|
xmlns:th="http://www.thymeleaf.org">
|
||||||
<head>
|
<head>
|
||||||
<title>Add Student</title>
|
<title>Add Student</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Add Student</h1>
|
<h1>Add Student</h1>
|
||||||
<form action="#" th:action="@{/saveStudent}" th:object="${student}"
|
<form action="#" th:action="@{/saveStudent}" th:object="${student}"
|
||||||
method="post">
|
method="post">
|
||||||
<ul>
|
<ul>
|
||||||
<li th:errors="*{id}" />
|
<li th:errors="*{id}" />
|
||||||
<li th:errors="*{name}" />
|
<li th:errors="*{name}" />
|
||||||
<li th:errors="*{gender}" />
|
<li th:errors="*{gender}" />
|
||||||
<li th:errors="*{percentage}" />
|
<li th:errors="*{percentage}" />
|
||||||
</ul>
|
</ul>
|
||||||
<table border="1">
|
<table border="1">
|
||||||
<tr>
|
<tr>
|
||||||
<td><label th:text="#{msg.id}" /></td>
|
<td><label th:text="#{msg.id}" /></td>
|
||||||
<td><input type="number" th:field="*{id}" /></td>
|
<td><input type="number" th:field="*{id}" /></td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><label th:text="#{msg.name}" /></td>
|
<td><label th:text="#{msg.name}" /></td>
|
||||||
<td><input type="text" th:field="*{name}" /></td>
|
<td><input type="text" th:field="*{name}" /></td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><label th:text="#{msg.gender}" /></td>
|
<td><label th:text="#{msg.gender}" /></td>
|
||||||
<td><select th:field="*{gender}">
|
<td><select th:field="*{gender}">
|
||||||
<option th:value="'M'" th:text="Male"></option>
|
<option th:value="'M'" th:text="Male"></option>
|
||||||
<option th:value="'F'" th:text="Female"></option>
|
<option th:value="'F'" th:text="Female"></option>
|
||||||
</select></td>
|
</select></td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><label th:text="#{msg.percent}" /></td>
|
<td><label th:text="#{msg.percent}" /></td>
|
||||||
<td><input type="text" th:field="*{percentage}" /></td>
|
<td><input type="text" th:field="*{percentage}" /></td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><input type="submit" value="Submit" /></td>
|
<td><input type="submit" value="Submit" /></td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
</form>
|
</form>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</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>
|
|
@ -1,45 +1,45 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml"
|
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||||
xmlns:th="http://www.thymeleaf.org">
|
xmlns:th="http://www.thymeleaf.org">
|
||||||
<head>
|
<head>
|
||||||
<title>Student List</title>
|
<title>Student List</title>
|
||||||
</head>
|
</head>
|
||||||
<script type="text/javascript"
|
<script type="text/javascript"
|
||||||
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
|
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
|
||||||
<script>
|
<script>
|
||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url : "/spring-thymeleaf/js",
|
url : "/spring-thymeleaf/js",
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
<body>
|
<body>
|
||||||
<h1>Student List</h1>
|
<h1>Student List</h1>
|
||||||
<table border="1">
|
<table border="1">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th th:text="#{msg.id}" />
|
<th th:text="#{msg.id}" />
|
||||||
<th th:text="#{msg.name}" />
|
<th th:text="#{msg.name}" />
|
||||||
<th th:text="#{msg.gender}" />
|
<th th:text="#{msg.gender}" />
|
||||||
<th th:text="#{msg.percent}" />
|
<th th:text="#{msg.percent}" />
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr th:each="student: ${students}">
|
<tr th:each="student: ${students}">
|
||||||
<td th:text="${student.id}" />
|
<td th:text="${student.id}" />
|
||||||
<td th:text="${{student.name}}" />
|
<td th:text="${{student.name}}" />
|
||||||
<td th:switch="${student.gender}"><span th:case="'M'"
|
<td th:switch="${student.gender}"><span th:case="'M'"
|
||||||
th:text="Male" /> <span th:case="'F'" th:text="Female" /></td>
|
th:text="Male" /> <span th:case="'F'" th:text="Female" /></td>
|
||||||
<td th:text="${#conversions.convert(student.percentage, 'Integer')}" />
|
<td th:text="${#conversions.convert(student.percentage, 'Integer')}" />
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
<div>
|
<div>
|
||||||
<p>
|
<p>
|
||||||
<a th:href="@{/}" th:text="#{msg.Home}" />
|
<a th:href="@{/}" th:text="#{msg.Home}" />
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</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,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,90 @@
|
||||||
|
package com.baeldung.thymeleaf.controller;
|
||||||
|
|
||||||
|
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.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;
|
||||||
|
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
import org.springframework.test.context.web.WebAppConfiguration;
|
||||||
|
|
||||||
|
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 static org.hamcrest.Matchers.containsString;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@WebAppConfiguration
|
||||||
|
@ContextConfiguration(classes = { WebApp.class, WebMVCConfig.class, WebMVCSecurity.class, InitSecurity.class })
|
||||||
|
public class FragmentsTest {
|
||||||
|
|
||||||
|
@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>")));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue