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/
|
||||
|
||||
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"
|
||||
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>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<java-version>1.8</java-version>
|
||||
<!-- spring -->
|
||||
<org.springframework-version>4.3.4.RELEASE</org.springframework-version>
|
||||
<springframework-security.version>4.2.0.RELEASE</springframework-security.version>
|
||||
<javax.servlet-version>3.1.0</javax.servlet-version>
|
||||
<!-- thymeleaf -->
|
||||
<org.thymeleaf-version>3.0.3.RELEASE</org.thymeleaf-version>
|
||||
<org.thymeleaf.extras-version>3.0.0.RELEASE</org.thymeleaf.extras-version>
|
||||
<thymeleaf-layout-dialect.version>2.1.2</thymeleaf-layout-dialect.version>
|
||||
<!-- validation -->
|
||||
<javax.validation-version>1.1.0.Final</javax.validation-version>
|
||||
<hibernate-validator.version>5.3.3.Final</hibernate-validator.version>
|
||||
<org.hibernate-version>5.2.5.Final</org.hibernate-version>
|
||||
|
||||
<!-- Maven plugins -->
|
||||
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
||||
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
|
||||
<tomcat7-maven-plugin.version>2.2</tomcat7-maven-plugin.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- Spring -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${org.springframework-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>${org.springframework-version}</version>
|
||||
</dependency>
|
||||
<!-- Spring Security -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-web</artifactId>
|
||||
<version>${springframework-security.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-config</artifactId>
|
||||
<version>${springframework-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-spring4</artifactId>
|
||||
<version>${org.thymeleaf-version}</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/nz.net.ultraq.thymeleaf/thymeleaf-layout-dialect -->
|
||||
<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-version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- Validation -->
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
<version>${javax.validation-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>${hibernate-validator.version}</version>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${org.springframework-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-test -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-test</artifactId>
|
||||
<version>${springframework-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>jetty8x</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>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>integration</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>integration-test</phase>
|
||||
<goals>
|
||||
<goal>test</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
</excludes>
|
||||
<includes>
|
||||
<include>**/*IntegrationTest.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<test.mime>json</test.mime>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
<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>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<java-version>1.8</java-version>
|
||||
<!-- spring -->
|
||||
<org.springframework-version>4.3.4.RELEASE</org.springframework-version>
|
||||
<springframework-security.version>4.2.0.RELEASE</springframework-security.version>
|
||||
<javax.servlet-version>3.1.0</javax.servlet-version>
|
||||
<!-- thymeleaf -->
|
||||
<org.thymeleaf-version>3.0.3.RELEASE</org.thymeleaf-version>
|
||||
<org.thymeleaf.extras-version>3.0.0.RELEASE</org.thymeleaf.extras-version>
|
||||
<thymeleaf-layout-dialect.version>2.1.2</thymeleaf-layout-dialect.version>
|
||||
<!-- validation -->
|
||||
<javax.validation-version>1.1.0.Final</javax.validation-version>
|
||||
<hibernate-validator.version>5.3.3.Final</hibernate-validator.version>
|
||||
<org.hibernate-version>5.2.5.Final</org.hibernate-version>
|
||||
|
||||
<!-- Maven plugins -->
|
||||
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
||||
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
|
||||
<tomcat7-maven-plugin.version>2.2</tomcat7-maven-plugin.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- Spring -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${org.springframework-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>${org.springframework-version}</version>
|
||||
</dependency>
|
||||
<!-- Spring Security -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-web</artifactId>
|
||||
<version>${springframework-security.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-config</artifactId>
|
||||
<version>${springframework-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-spring4</artifactId>
|
||||
<version>${org.thymeleaf-version}</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/nz.net.ultraq.thymeleaf/thymeleaf-layout-dialect -->
|
||||
<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-version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- Validation -->
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
<version>${javax.validation-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>${hibernate-validator.version}</version>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${org.springframework-version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-test -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-test</artifactId>
|
||||
<version>${springframework-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>jetty8x</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>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>integration</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>integration-test</phase>
|
||||
<goals>
|
||||
<goal>test</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
</excludes>
|
||||
<includes>
|
||||
<include>**/*IntegrationTest.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<test.mime>json</test.mime>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
</project>
|
|
@ -77,14 +77,14 @@ public class WebMVCConfig extends WebMvcConfigurerAdapter implements Application
|
|||
return resolver;
|
||||
}
|
||||
|
||||
private TemplateEngine 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 TemplateEngine 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();
|
||||
|
@ -142,7 +142,8 @@ public class WebMVCConfig extends WebMvcConfigurerAdapter implements Application
|
|||
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/resources/");
|
||||
registry.addResourceHandler("/resources/**", "/css/**")
|
||||
.addResourceLocations("/WEB-INF/resources/", "/WEB-INF/css/");
|
||||
}
|
||||
|
||||
@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.name=Name
|
||||
msg.gender=Gender
|
||||
msg.percent=Percentage
|
||||
welcome.message=Welcome Student !!!
|
||||
msg.AddStudent=Add Student
|
||||
msg.ListStudents=List Students
|
||||
msg.Home=Home
|
||||
|
||||
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
|
||||
|
||||
|
|
|
@ -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>
|
||||
<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><input type="text" th:field="*{percentage}" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><input type="submit" value="Submit" /></td>
|
||||
</tr>
|
||||
</table>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
<!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><input type="text" th:field="*{percentage}" /></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><input type="submit" value="Submit" /></td>
|
||||
</tr>
|
||||
</table>
|
||||
</form>
|
||||
</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>
|
|
@ -1,45 +1,45 @@
|
|||
<!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}" />
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="student: ${students}">
|
||||
<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')}" />
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<div>
|
||||
<p>
|
||||
<a th:href="@{/}" th:text="#{msg.Home}" />
|
||||
</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
<!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}" />
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr th:each="student: ${students}">
|
||||
<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')}" />
|
||||
</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,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