Welcome to Spring MVC integration with Apache Tiles
+
\ No newline at end of file
diff --git a/spring-mvc-tiles/src/main/webapp/WEB-INF/views/tiles/templates/defaultMenu.jsp b/spring-mvc-tiles/src/main/webapp/WEB-INF/views/tiles/templates/defaultMenu.jsp
new file mode 100644
index 0000000000..2c91eace85
--- /dev/null
+++ b/spring-mvc-tiles/src/main/webapp/WEB-INF/views/tiles/templates/defaultMenu.jsp
@@ -0,0 +1,8 @@
+
diff --git a/spring-mvc-tiles/src/main/webapp/WEB-INF/views/tiles/tiles.xml b/spring-mvc-tiles/src/main/webapp/WEB-INF/views/tiles/tiles.xml
new file mode 100644
index 0000000000..789fbd809a
--- /dev/null
+++ b/spring-mvc-tiles/src/main/webapp/WEB-INF/views/tiles/tiles.xml
@@ -0,0 +1,34 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/spring-mvc-tiles/src/main/webapp/static/css/app.css b/spring-mvc-tiles/src/main/webapp/static/css/app.css
new file mode 100644
index 0000000000..9976e5406e
--- /dev/null
+++ b/spring-mvc-tiles/src/main/webapp/static/css/app.css
@@ -0,0 +1,36 @@
+.flex-container {
+ display: -webkit-flex;
+ display: flex;
+ -webkit-flex-flow: row wrap;
+ flex-flow: row wrap;
+ text-align: center;
+}
+
+.flex-container > * {
+ padding: 15px;
+ -webkit-flex: 1 100%;
+ flex: 1 100%;
+}
+
+.article {
+ text-align: left;
+}
+
+header {background: black;color:white;}
+footer {background: #aaa;color:white;}
+.nav {background:#eee;}
+
+.nav ul {
+ list-style-type: none;
+ padding: 0;
+}
+
+.nav ul a {
+ text-decoration: none;
+}
+
+@media all and (min-width: 768px) {
+ .nav {text-align:left;-webkit-flex: 1 auto;flex:1 auto;-webkit-order:1;order:1;}
+ .article {-webkit-flex:5 0px;flex:5 0px;-webkit-order:2;order:2;}
+ footer {-webkit-order:3;order:3;}
+}
\ No newline at end of file
diff --git a/spring-rest/pom.xml b/spring-rest/pom.xml
index 0c0d6219dd..18cb1dc72a 100644
--- a/spring-rest/pom.xml
+++ b/spring-rest/pom.xml
@@ -182,7 +182,6 @@
org.apache.maven.pluginsmaven-compiler-plugin
- ${maven-compiler-plugin.version}1.81.8
@@ -192,16 +191,14 @@
org.apache.maven.pluginsmaven-war-plugin
- ${maven-war-plugin.version}org.apache.maven.pluginsmaven-surefire-plugin
- ${maven-surefire-plugin.version}
-
+ **/*LiveTest.java
@@ -216,7 +213,7 @@
true
- jetty8x
+ tomcat8xembedded
@@ -234,6 +231,61 @@
+
+
+ live
+
+
+
+ org.codehaus.cargo
+ cargo-maven2-plugin
+
+
+ start-server
+ pre-integration-test
+
+ start
+
+
+
+ stop-server
+ post-integration-test
+
+ stop
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+
+
+ integration-test
+
+ test
+
+
+
+ none
+
+
+ **/*LiveTest.java
+
+
+ cargo
+
+
+
+
+
+
+
+
+
+
+
@@ -266,7 +318,7 @@
3.5.12.62.19.1
- 1.4.18
+ 1.6.0
diff --git a/spring-rest/src/main/java/org/baeldung/web/controller/status/ForbiddenException.java b/spring-rest/src/main/java/org/baeldung/web/controller/status/ForbiddenException.java
index 1d4aff2ebf..348ee6d596 100644
--- a/spring-rest/src/main/java/org/baeldung/web/controller/status/ForbiddenException.java
+++ b/spring-rest/src/main/java/org/baeldung/web/controller/status/ForbiddenException.java
@@ -5,5 +5,6 @@ import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(value = HttpStatus.FORBIDDEN, reason="To show an example of a custom message")
public class ForbiddenException extends RuntimeException {
+ private static final long serialVersionUID = 6826605655586311552L;
}
diff --git a/spring-rest/src/test/java/org/baeldung/web/test/RequestMappingLiveTest.java b/spring-rest/src/test/java/org/baeldung/web/test/RequestMappingLiveTest.java
index fd349f1e44..3155b5cda9 100644
--- a/spring-rest/src/test/java/org/baeldung/web/test/RequestMappingLiveTest.java
+++ b/spring-rest/src/test/java/org/baeldung/web/test/RequestMappingLiveTest.java
@@ -7,7 +7,7 @@ import org.junit.Test;
import com.jayway.restassured.RestAssured;
public class RequestMappingLiveTest {
- private static String BASE_URI = "http://localhost:8080/spring-rest/ex/";
+ private static String BASE_URI = "http://localhost:8082/spring-rest/ex/";
@Test
public void givenSimplePath_whenGetFoos_thenOk() {
diff --git a/spring-rest/src/test/java/org/baeldung/web/test/SpringHttpMessageConvertersIntegrationTestsCase.java b/spring-rest/src/test/java/org/baeldung/web/test/SpringHttpMessageConvertersLiveTest.java
similarity index 97%
rename from spring-rest/src/test/java/org/baeldung/web/test/SpringHttpMessageConvertersIntegrationTestsCase.java
rename to spring-rest/src/test/java/org/baeldung/web/test/SpringHttpMessageConvertersLiveTest.java
index 1dfe509c09..7f250653ab 100644
--- a/spring-rest/src/test/java/org/baeldung/web/test/SpringHttpMessageConvertersIntegrationTestsCase.java
+++ b/spring-rest/src/test/java/org/baeldung/web/test/SpringHttpMessageConvertersLiveTest.java
@@ -21,9 +21,9 @@ import org.springframework.web.client.RestTemplate;
/**
* Integration Test class. Tests methods hits the server's rest services.
*/
-public class SpringHttpMessageConvertersIntegrationTestsCase {
+public class SpringHttpMessageConvertersLiveTest {
- private static String BASE_URI = "http://localhost:8080/spring-rest/";
+ private static String BASE_URI = "http://localhost:8082/spring-rest/";
/**
* Without specifying Accept Header, uses the default response from the
diff --git a/spring-security-mvc-persisted-remember-me/src/main/java/org/baeldung/service/MyUserDetailsService.java b/spring-security-mvc-persisted-remember-me/src/main/java/org/baeldung/service/MyUserDetailsService.java
index 2baf88a62d..c662c32738 100644
--- a/spring-security-mvc-persisted-remember-me/src/main/java/org/baeldung/service/MyUserDetailsService.java
+++ b/spring-security-mvc-persisted-remember-me/src/main/java/org/baeldung/service/MyUserDetailsService.java
@@ -1,11 +1,5 @@
package org.baeldung.service;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.baeldung.security.SecurityRole;
@@ -17,9 +11,9 @@ import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
-/**
- * User Details Service - hard coded to two users for the example.
- */
+import java.util.*;
+import java.util.stream.Collectors;
+
@Service
public class MyUserDetailsService implements UserDetailsService {
@@ -28,24 +22,19 @@ public class MyUserDetailsService implements UserDetailsService {
private final Map availableUsers = new HashMap();
public MyUserDetailsService() {
-
populateDemoUsers();
-
}
- //
-
@Override
public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException {
logger.info("Load user by username " + username);
final UserDetails user = availableUsers.get(username);
if (user == null) {
- throw new UsernameNotFoundException("Username not found");
- } else {
- return availableUsers.get(username);
+ throw new UsernameNotFoundException(username);
}
+ return user;
}
/**
@@ -59,24 +48,13 @@ public class MyUserDetailsService implements UserDetailsService {
availableUsers.put("admin", createUser("admin", "password", Arrays.asList(SecurityRole.ROLE_ADMIN)));
}
- /**
- * Create a demo User.
- *
- * @param username
- * Username
- * @param password
- * Password
- * @param roles
- * Role names user is assigned to
- * @return User
- */
private User createUser(final String username, final String password, final List roles) {
logger.info("Create user " + username);
- final List authorities = new ArrayList();
- for (final SecurityRole role : roles) {
- authorities.add(new SimpleGrantedAuthority(role.toString()));
- }
+ final List authorities = roles.stream()
+ .map(role -> new SimpleGrantedAuthority(role.toString()))
+ .collect(Collectors.toList());
+
return new User(username, password, true, true, true, true, authorities);
}
}
diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/interceptor/SessionTimerInterceptor.java b/spring-security-rest-full/src/main/java/org/baeldung/web/interceptor/SessionTimerInterceptor.java
index 8d967ed1ef..f5c1626989 100644
--- a/spring-security-rest-full/src/main/java/org/baeldung/web/interceptor/SessionTimerInterceptor.java
+++ b/spring-security-rest-full/src/main/java/org/baeldung/web/interceptor/SessionTimerInterceptor.java
@@ -31,9 +31,8 @@ public class SessionTimerInterceptor extends HandlerInterceptorAdapter {
request.setAttribute("executionTime", startTime);
if (UserInterceptor.isUserLogged()) {
session = request.getSession();
- log.info("Who is logged in: " + SecurityContextHolder.getContext().getAuthentication().getName());
- log.info("Time since last request in this session: "
- + (System.currentTimeMillis() - request.getSession().getLastAccessedTime()) + " ms");
+ log.info("Time since last request in this session: {} ms",
+ System.currentTimeMillis() - request.getSession().getLastAccessedTime());
if (System.currentTimeMillis() - session.getLastAccessedTime() > MAX_INACTIVE_SESSION_TIME) {
log.warn("Logging out, due to inactive session");
SecurityContextHolder.clearContext();
@@ -52,6 +51,6 @@ public class SessionTimerInterceptor extends HandlerInterceptorAdapter {
final ModelAndView model) throws Exception {
log.info("Post handle method - check execution time of handling");
long startTime = (Long) request.getAttribute("executionTime");
- log.info("Execution time for handling the request was: " + (System.currentTimeMillis() - startTime) + " ms");
+ log.info("Execution time for handling the request was: {} ms", System.currentTimeMillis() - startTime);
}
}
diff --git a/spring-security-rest-full/src/test/java/org/baeldung/security/csrf/CsrfAbstractIntegrationTest.java b/spring-security-rest-full/src/test/java/org/baeldung/security/csrf/CsrfAbstractIntegrationTest.java
index 1b5f7cd894..13cb92a745 100644
--- a/spring-security-rest-full/src/test/java/org/baeldung/security/csrf/CsrfAbstractIntegrationTest.java
+++ b/spring-security-rest-full/src/test/java/org/baeldung/security/csrf/CsrfAbstractIntegrationTest.java
@@ -14,6 +14,7 @@ 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.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -21,6 +22,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
+@Transactional
public class CsrfAbstractIntegrationTest {
@Autowired
diff --git a/spring-thymeleaf/pom.xml b/spring-thymeleaf/pom.xml
index 51e26fdfdd..a13f1de4c7 100644
--- a/spring-thymeleaf/pom.xml
+++ b/spring-thymeleaf/pom.xml
@@ -5,59 +5,69 @@
spring-thymeleaf0.1-SNAPSHOTwar
-
- 1.7
-
- 4.1.8.RELEASE
- 3.0.1
-
+
+ 1.8
+
+ 4.3.3.RELEASE
+ 3.0.1
+
1.7.121.1.3
-
- 2.1.4.RELEASE
-
- 1.1.0.Final
- 5.1.2.Final
-
-
- 3.5.1
- 2.6
- 2.19.1
- 1.4.18
-
-
-
-
-
- org.springframework
- spring-context
- ${org.springframework-version}
-
-
-
- commons-logging
- commons-logging
-
-
-
-
- org.springframework
- spring-webmvc
- ${org.springframework-version}
-
-
-
- org.thymeleaf
- thymeleaf
- ${org.thymeleaf-version}
-
-
- org.thymeleaf
- thymeleaf-spring4
- ${org.thymeleaf-version}
-
-
-
+
+ 3.0.1.RELEASE
+
+ 1.1.0.Final
+ 5.1.2.Final
+
+
+ 3.5.1
+ 2.6
+ 2.19.1
+ 1.4.18
+
+
+
+
+
+ org.springframework
+ spring-context
+ ${org.springframework-version}
+
+
+
+ commons-logging
+ commons-logging
+
+
+
+
+ org.springframework
+ spring-webmvc
+ ${org.springframework-version}
+
+
+
+ org.springframework.security
+ spring-security-web
+ 4.1.3.RELEASE
+
+
+ org.springframework.security
+ spring-security-config
+ 4.1.3.RELEASE
+
+
+
+ org.thymeleaf
+ thymeleaf
+ ${org.thymeleaf-version}
+
+
+ org.thymeleaf
+ thymeleaf-spring4
+ ${org.thymeleaf-version}
+
+
org.slf4jslf4j-api
@@ -80,55 +90,81 @@
log4j-over-slf4j${org.slf4j.version}
-
-
- javax.servlet
- javax.servlet-api
- ${javax.servlet-version}
- provided
-
-
-
- javax.validation
- validation-api
- ${javax.validation-version}
-
-
- org.hibernate
- hibernate-validator
- ${org.hibernate-version}
-
-
-
-
-
- org.apache.maven.plugins
- maven-compiler-plugin
- ${maven-compiler-plugin.version}
-
- ${java-version}
- ${java-version}
-
-
-
- org.apache.maven.plugins
- maven-war-plugin
- ${maven-war-plugin.version}
-
- false
-
-
-
- org.apache.maven.plugins
- maven-surefire-plugin
- ${maven-surefire-plugin.version}
-
-
-
-
-
-
-
+
+
+ javax.servlet
+ javax.servlet-api
+ ${javax.servlet-version}
+ provided
+
+
+
+ javax.validation
+ validation-api
+ ${javax.validation-version}
+
+
+ org.hibernate
+ hibernate-validator
+ ${org.hibernate-version}
+
+
+
+
+ org.springframework
+ spring-test
+ 4.1.3.RELEASE
+ test
+
+
+
+
+ org.springframework.security
+ spring-security-test
+ 4.1.3.RELEASE
+ test
+
+
+
+
+ junit
+ junit
+ 4.12
+ test
+
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ ${maven-compiler-plugin.version}
+
+ ${java-version}
+ ${java-version}
+
+
+
+ org.apache.maven.plugins
+ maven-war-plugin
+ ${maven-war-plugin.version}
+
+ false
+
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+ ${maven-surefire-plugin.version}
+
+
+
+
+
+
+ org.codehaus.cargocargo-maven2-plugin
@@ -148,6 +184,7 @@
-
-
+
+
+
diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/InitSecurity.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/InitSecurity.java
new file mode 100644
index 0000000000..956db4a0e5
--- /dev/null
+++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/InitSecurity.java
@@ -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);
+
+ }
+}
diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebApp.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebApp.java
index 89ad7e601e..3104f45ab5 100644
--- a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebApp.java
+++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebApp.java
@@ -9,28 +9,28 @@ import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatche
*/
public class WebApp extends AbstractAnnotationConfigDispatcherServletInitializer {
- public WebApp() {
- super();
- }
+ public WebApp() {
+ super();
+ }
- @Override
- protected Class>[] getRootConfigClasses() {
- return null;
- }
+ @Override
+ protected Class>[] getRootConfigClasses() {
+ return null;
+ }
- @Override
- protected Class>[] getServletConfigClasses() {
- return new Class>[] { WebMVCConfig.class };
- }
+ @Override
+ protected Class>[] getServletConfigClasses() {
+ return new Class>[] { WebMVCConfig.class, WebMVCSecurity.class, InitSecurity.class };
+ }
- @Override
- protected String[] getServletMappings() {
- return new String[] { "/" };
- }
+ @Override
+ protected String[] getServletMappings() {
+ return new String[] { "/" };
+ }
- @Override
- protected void customizeRegistration(final Dynamic registration) {
- super.customizeRegistration(registration);
- }
+ @Override
+ protected void customizeRegistration(final Dynamic registration) {
+ super.customizeRegistration(registration);
+ }
}
diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCConfig.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCConfig.java
index 51c60247a1..444b780673 100644
--- a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCConfig.java
+++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCConfig.java
@@ -1,18 +1,27 @@
package com.baeldung.thymeleaf.config;
-import com.baeldung.thymeleaf.formatter.NameFormatter;
+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.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
+import org.thymeleaf.TemplateEngine;
import org.thymeleaf.spring4.SpringTemplateEngine;
+import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
-import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
+import org.thymeleaf.templatemode.TemplateMode;
+import org.thymeleaf.templateresolver.ITemplateResolver;
+
+import com.baeldung.thymeleaf.formatter.NameFormatter;
+import com.baeldung.thymeleaf.utils.ArrayUtil;
+
@Configuration
@EnableWebMvc
@@ -21,36 +30,76 @@ import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
* Java configuration file that is used for Spring MVC and Thymeleaf
* configurations
*/
-public class WebMVCConfig extends WebMvcConfigurerAdapter {
+public class WebMVCConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {
- @Bean
- @Description("Thymeleaf Template Resolver")
- public ServletContextTemplateResolver templateResolver() {
- ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
- templateResolver.setPrefix("/WEB-INF/views/");
- templateResolver.setSuffix(".html");
- templateResolver.setTemplateMode("HTML5");
+ private ApplicationContext applicationContext;
- return templateResolver;
+ public void setApplicationContext(ApplicationContext applicationContext) {
+ this.applicationContext = applicationContext;
}
@Bean
- @Description("Thymeleaf Template Engine")
- public SpringTemplateEngine templateEngine() {
- SpringTemplateEngine templateEngine = new SpringTemplateEngine();
- templateEngine.setTemplateResolver(templateResolver());
-
- return templateEngine;
+ 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
- @Description("Thymeleaf View Resolver")
- public ThymeleafViewResolver viewResolver() {
- ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
- viewResolver.setTemplateEngine(templateEngine());
- viewResolver.setOrder(1);
- return viewResolver;
- }
+ 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 TemplateEngine templateEngine(ITemplateResolver templateResolver) {
+ SpringTemplateEngine engine = new SpringTemplateEngine();
+ engine.setTemplateResolver(templateResolver);
+ 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")
diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCSecurity.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCSecurity.java
new file mode 100644
index 0000000000..37844a2976
--- /dev/null
+++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/config/WebMVCSecurity.java
@@ -0,0 +1,49 @@
+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("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()
+ ;
+ }
+
+}
diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/HomeController.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/HomeController.java
index a28d059acc..f1a394cac4 100644
--- a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/HomeController.java
+++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/HomeController.java
@@ -21,7 +21,7 @@ public class HomeController {
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.getDefault());
model.addAttribute("serverTime", dateFormat.format(new Date()));
- return "home";
+ return "home.html";
}
}
diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/InliningController.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/InliningController.java
new file mode 100644
index 0000000000..9e3f14ac8e
--- /dev/null
+++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/InliningController.java
@@ -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", "Thymeleaf 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";
+ }
+}
diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/StudentController.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/StudentController.java
index 912eb521f4..1f40046caa 100644
--- a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/StudentController.java
+++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/StudentController.java
@@ -1,11 +1,9 @@
package com.baeldung.thymeleaf.controller;
-import java.util.ArrayList;
import java.util.List;
import javax.validation.Valid;
-import com.baeldung.thymeleaf.model.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
@@ -13,6 +11,9 @@ 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.
*
@@ -24,46 +25,26 @@ public class StudentController {
public String saveStudent(@Valid @ModelAttribute Student student, BindingResult errors, Model model) {
if (!errors.hasErrors()) {
// get mock objects
- List students = buildStudents();
+ List students = StudentUtils.buildStudents();
// add current student
students.add(student);
model.addAttribute("students", students);
}
- return ((errors.hasErrors()) ? "addStudent" : "listStudents");
+ 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";
+ return "addStudent.html";
}
@RequestMapping(value = "/listStudents", method = RequestMethod.GET)
public String listStudent(Model model) {
- model.addAttribute("students", buildStudents());
+ model.addAttribute("students", StudentUtils.buildStudents());
- return "listStudents";
+ return "listStudents.html";
}
- private List buildStudents() {
- List students = new ArrayList();
-
- 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;
- }
}
diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/model/Student.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/model/Student.java
index bce99f286c..202c04358a 100644
--- a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/model/Student.java
+++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/model/Student.java
@@ -12,49 +12,49 @@ import javax.validation.constraints.NotNull;
*/
public class Student implements Serializable {
- private static final long serialVersionUID = -8582553475226281591L;
+ 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 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 name is required.")
+ private String name;
- @NotNull(message = "Student gender is required.")
- private Character gender;
+ @NotNull(message = "Student gender is required.")
+ private Character gender;
- private Float percentage;
+ private Float percentage;
- public Integer getId() {
- return id;
- }
+ public Integer getId() {
+ return id;
+ }
- public void setId(Integer id) {
- this.id = id;
- }
+ public void setId(Integer id) {
+ this.id = id;
+ }
- public String getName() {
- return name;
- }
+ public String getName() {
+ return name;
+ }
- public void setName(String name) {
- this.name = name;
- }
+ public void setName(String name) {
+ this.name = name;
+ }
- public Character getGender() {
- return gender;
- }
+ public Character getGender() {
+ return gender;
+ }
- public void setGender(Character gender) {
- this.gender = gender;
- }
+ public void setGender(Character gender) {
+ this.gender = gender;
+ }
- public Float getPercentage() {
- return percentage;
- }
+ public Float getPercentage() {
+ return percentage;
+ }
- public void setPercentage(Float percentage) {
- this.percentage = percentage;
- }
+ public void setPercentage(Float percentage) {
+ this.percentage = percentage;
+ }
}
diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/utils/ArrayUtil.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/utils/ArrayUtil.java
new file mode 100644
index 0000000000..d85c70c1b7
--- /dev/null
+++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/utils/ArrayUtil.java
@@ -0,0 +1,8 @@
+package com.baeldung.thymeleaf.utils;
+
+public class ArrayUtil {
+
+ public static String[] array(String... args) {
+ return args;
+ }
+}
\ No newline at end of file
diff --git a/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/utils/StudentUtils.java b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/utils/StudentUtils.java
new file mode 100644
index 0000000000..f7ed254641
--- /dev/null
+++ b/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/utils/StudentUtils.java
@@ -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 students = new ArrayList();
+
+ public static List 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;
+ }
+
+}
diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/js/studentCheck.js b/spring-thymeleaf/src/main/webapp/WEB-INF/js/studentCheck.js
new file mode 100644
index 0000000000..625e0b37e5
--- /dev/null
+++ b/spring-thymeleaf/src/main/webapp/WEB-INF/js/studentCheck.js
@@ -0,0 +1,2 @@
+var count = [[${students.size()}]];
+alert("Number of students in group: " + count);
diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/txt/studentsList.txt b/spring-thymeleaf/src/main/webapp/WEB-INF/txt/studentsList.txt
new file mode 100644
index 0000000000..b27796c6ab
--- /dev/null
+++ b/spring-thymeleaf/src/main/webapp/WEB-INF/txt/studentsList.txt
@@ -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
\ No newline at end of file
diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/csrfAttack.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/csrfAttack.html
new file mode 100644
index 0000000000..7674caa854
--- /dev/null
+++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/csrfAttack.html
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/inliningExample.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/inliningExample.html
new file mode 100644
index 0000000000..cd20746c3a
--- /dev/null
+++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/inliningExample.html
@@ -0,0 +1,12 @@
+
+
+
+
+Inlining example
+
+
+
Title of tutorial: [[${title}]]
+
Description: [(${description})]
+
+
\ No newline at end of file
diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/listStudents.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/listStudents.html
index c25de9eb17..a894e41e88 100644
--- a/spring-thymeleaf/src/main/webapp/WEB-INF/views/listStudents.html
+++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/listStudents.html
@@ -4,6 +4,15 @@
Student List
+
+