Java 23053 02 (#14432)
* JAVA-23053: renaming the module * JAVA-23053: correction made for More articles links in spring-thymeleaf
This commit is contained in:
@@ -2,26 +2,14 @@
|
||||
|
||||
This module contains articles about Spring with Thymeleaf
|
||||
|
||||
### Relevant Articles:
|
||||
- [CSRF Protection with Spring MVC and Thymeleaf](https://www.baeldung.com/csrf-thymeleaf-with-spring-security)
|
||||
- [Conditionals in Thymeleaf](https://www.baeldung.com/spring-thymeleaf-conditionals)
|
||||
- [Iteration in Thymeleaf](https://www.baeldung.com/thymeleaf-iteration)
|
||||
- [Spring with Thymeleaf Pagination for a List](https://www.baeldung.com/spring-thymeleaf-pagination)
|
||||
## Relevant Articles:
|
||||
|
||||
### Build the Project
|
||||
|
||||
mvn clean install
|
||||
|
||||
### Run the Project
|
||||
|
||||
mvn cargo:run
|
||||
- **note**: starts on port '8082'
|
||||
|
||||
Access the pages using the URLs:
|
||||
|
||||
- http://localhost:8082/spring-thymeleaf-4/
|
||||
- http://localhost:8082/spring-thymeleaf-4/addStudent/
|
||||
- http://localhost:8082/spring-thymeleaf-4/listStudents/
|
||||
|
||||
The first URL is the home page of the application. The home page has links to the second and third pages.
|
||||
- [Changing the Thymeleaf Template Directory in Spring Boot](https://www.baeldung.com/spring-thymeleaf-template-directory)
|
||||
- [Add a Checked Attribute to Input Conditionally in Thymeleaf](https://www.baeldung.com/thymeleaf-conditional-checked-attribute)
|
||||
- [Spring MVC Data and Thymeleaf](https://www.baeldung.com/spring-mvc-thymeleaf-data)
|
||||
- [Upload Image With Spring Boot and Thymeleaf](https://www.baeldung.com/spring-boot-thymeleaf-image-upload)
|
||||
- [Getting a URL Attribute Value in Thymeleaf](https://www.baeldung.com/thymeleaf-url-attribute-value)
|
||||
- [Expression Types in Thymeleaf](https://www.baeldung.com/java-thymeleaf-expression-types)
|
||||
- [Difference Between th:text and th:value in Thymeleaf](https://www.baeldung.com/java-thymeleaf-text-vs-value)
|
||||
- More articles: [[<-- prev]](../spring-thymeleaf-3) [[next -->]](../spring-thymeleaf-5)
|
||||
|
||||
|
||||
@@ -9,139 +9,33 @@
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-spring-5</artifactId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-spring-5</relativePath>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- Spring -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<exclusions>
|
||||
<!-- Exclude Commons Logging in favor of SLF4j -->
|
||||
<exclusion>
|
||||
<groupId>commons-logging</groupId>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-webmvc</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-commons</artifactId>
|
||||
<version>${spring-data.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
<version>${javax.validation-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate.validator</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>${hibernate-validator.version}</version>
|
||||
</dependency>
|
||||
<!-- Spring Security -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-web</artifactId>
|
||||
<version>${spring-security.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-config</artifactId>
|
||||
<version>${spring-security.version}</version>
|
||||
</dependency>
|
||||
<!-- Thymeleaf -->
|
||||
<dependency>
|
||||
<groupId>org.thymeleaf</groupId>
|
||||
<artifactId>thymeleaf</artifactId>
|
||||
<version>${org.thymeleaf-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.thymeleaf</groupId>
|
||||
<artifactId>thymeleaf-spring5</artifactId>
|
||||
<version>${org.thymeleaf-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>nz.net.ultraq.thymeleaf</groupId>
|
||||
<artifactId>thymeleaf-layout-dialect</artifactId>
|
||||
<version>${thymeleaf-layout-dialect.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.thymeleaf.extras</groupId>
|
||||
<artifactId>thymeleaf-extras-java8time</artifactId>
|
||||
<version>${org.thymeleaf.extras-version}</version>
|
||||
</dependency>
|
||||
<!-- Servlet -->
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>${javax.servlet-api.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>${spring.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-test</artifactId>
|
||||
<version>${spring-security.version}</version>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-war-plugin</artifactId>
|
||||
<configuration>
|
||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.cargo</groupId>
|
||||
<artifactId>cargo-maven2-plugin</artifactId>
|
||||
<version>${cargo-maven2-plugin.version}</version>
|
||||
<configuration>
|
||||
<wait>true</wait>
|
||||
<container>
|
||||
<containerId>jetty9x</containerId>
|
||||
<type>embedded</type>
|
||||
<systemProperties>
|
||||
</systemProperties>
|
||||
</container>
|
||||
<configuration>
|
||||
<properties>
|
||||
<cargo.servlet.port>8082</cargo.servlet.port>
|
||||
</properties>
|
||||
</configuration>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<finalName>spring-thymeleaf-4</finalName>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<spring-data.version>2.3.2.RELEASE</spring-data.version>
|
||||
<org.thymeleaf-version>3.0.11.RELEASE</org.thymeleaf-version>
|
||||
<org.thymeleaf.extras-version>3.0.4.RELEASE</org.thymeleaf.extras-version>
|
||||
<thymeleaf-layout-dialect.version>2.4.1</thymeleaf-layout-dialect.version>
|
||||
<javax.validation-version>2.0.1.Final</javax.validation-version>
|
||||
<hibernate-validator.version>6.0.11.Final</hibernate-validator.version>
|
||||
<!-- Maven plugins -->
|
||||
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
|
||||
<tomcat7-maven-plugin.version>2.2</tomcat7-maven-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.thymeleaf;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.thymeleaf;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.thymeleaf.templatemode.TemplateMode;
|
||||
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
|
||||
|
||||
@Configuration
|
||||
public class ThymeleafConfig {
|
||||
|
||||
@Bean
|
||||
public ClassLoaderTemplateResolver secondaryTemplateResolver() {
|
||||
ClassLoaderTemplateResolver secondaryTemplateResolver = new ClassLoaderTemplateResolver();
|
||||
secondaryTemplateResolver.setPrefix("templates-2/");
|
||||
secondaryTemplateResolver.setSuffix(".html");
|
||||
secondaryTemplateResolver.setTemplateMode(TemplateMode.HTML);
|
||||
secondaryTemplateResolver.setCharacterEncoding("UTF-8");
|
||||
secondaryTemplateResolver.setOrder(1);
|
||||
secondaryTemplateResolver.setCheckExistence(true);
|
||||
|
||||
return secondaryTemplateResolver;
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.thymeleaf.attribute;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
@Controller
|
||||
public class CheckedAttributeController {
|
||||
|
||||
@GetMapping("/checked")
|
||||
public String displayCheckboxForm(Model model) {
|
||||
Engine engine = new Engine(true);
|
||||
model.addAttribute("engine", engine);
|
||||
model.addAttribute("flag", true);
|
||||
return "attribute/index";
|
||||
}
|
||||
|
||||
private static class Engine {
|
||||
private Boolean active;
|
||||
|
||||
public Engine(Boolean active) {
|
||||
this.active = active;
|
||||
}
|
||||
|
||||
public Boolean getActive() {
|
||||
return active;
|
||||
}
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.baeldung.thymeleaf.attributes;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/attributes")
|
||||
public class AttributeController {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(AttributeController.class);
|
||||
|
||||
@GetMapping
|
||||
public String show(Model model) {
|
||||
model.addAttribute("title", "Baeldung");
|
||||
model.addAttribute("email", "[email protected]");
|
||||
return "attributes/index";
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public String submit(String email) {
|
||||
logger.info("Email: {}", email);
|
||||
return "redirect:attributes";
|
||||
}
|
||||
|
||||
}
|
||||
-11
@@ -1,11 +0,0 @@
|
||||
package com.baeldung.thymeleaf.config;
|
||||
|
||||
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
|
||||
|
||||
public class InitSecurity extends AbstractSecurityWebApplicationInitializer {
|
||||
|
||||
public InitSecurity() {
|
||||
super(WebMVCSecurity.class);
|
||||
|
||||
}
|
||||
}
|
||||
-36
@@ -1,36 +0,0 @@
|
||||
package com.baeldung.thymeleaf.config;
|
||||
|
||||
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
|
||||
|
||||
import javax.servlet.ServletRegistration.Dynamic;
|
||||
|
||||
/**
|
||||
* Java configuration file that is used for web application initialization
|
||||
*/
|
||||
public class WebApp extends AbstractAnnotationConfigDispatcherServletInitializer {
|
||||
|
||||
public WebApp() {
|
||||
super();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getRootConfigClasses() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?>[] getServletConfigClasses() {
|
||||
return new Class<?>[] { WebMVCConfig.class };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] getServletMappings() {
|
||||
return new String[] { "/" };
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void customizeRegistration(final Dynamic registration) {
|
||||
super.customizeRegistration(registration);
|
||||
}
|
||||
|
||||
}
|
||||
-115
@@ -1,115 +0,0 @@
|
||||
package com.baeldung.thymeleaf.config;
|
||||
|
||||
import com.baeldung.thymeleaf.formatter.NameFormatter;
|
||||
import com.baeldung.thymeleaf.utils.ArrayUtil;
|
||||
import nz.net.ultraq.thymeleaf.LayoutDialect;
|
||||
import nz.net.ultraq.thymeleaf.decorators.strategies.GroupingStrategy;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Description;
|
||||
import org.springframework.context.support.ResourceBundleMessageSource;
|
||||
import org.springframework.format.FormatterRegistry;
|
||||
import org.springframework.web.servlet.LocaleResolver;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
|
||||
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
|
||||
import org.thymeleaf.extras.java8time.dialect.Java8TimeDialect;
|
||||
import org.thymeleaf.spring5.ISpringTemplateEngine;
|
||||
import org.thymeleaf.spring5.SpringTemplateEngine;
|
||||
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
|
||||
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
|
||||
import org.thymeleaf.templatemode.TemplateMode;
|
||||
import org.thymeleaf.templateresolver.ITemplateResolver;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan({ "com.baeldung.thymeleaf" })
|
||||
/*
|
||||
Java configuration file that is used for Spring MVC and Thymeleaf
|
||||
configurations
|
||||
*/
|
||||
public class WebMVCConfig implements WebMvcConfigurer, ApplicationContextAware {
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ViewResolver htmlViewResolver() {
|
||||
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
|
||||
resolver.setTemplateEngine(templateEngine(htmlTemplateResolver()));
|
||||
resolver.setContentType("text/html");
|
||||
resolver.setCharacterEncoding("UTF-8");
|
||||
resolver.setViewNames(ArrayUtil.array("*.html"));
|
||||
return resolver;
|
||||
}
|
||||
|
||||
private ISpringTemplateEngine templateEngine(ITemplateResolver templateResolver) {
|
||||
SpringTemplateEngine engine = new SpringTemplateEngine();
|
||||
engine.addDialect(new LayoutDialect(new GroupingStrategy()));
|
||||
engine.addDialect(new Java8TimeDialect());
|
||||
engine.setTemplateResolver(templateResolver);
|
||||
engine.setTemplateEngineMessageSource(messageSource());
|
||||
return engine;
|
||||
}
|
||||
|
||||
private ITemplateResolver htmlTemplateResolver() {
|
||||
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
|
||||
resolver.setApplicationContext(applicationContext);
|
||||
resolver.setPrefix("/WEB-INF/views/");
|
||||
resolver.setCacheable(false);
|
||||
resolver.setTemplateMode(TemplateMode.HTML);
|
||||
return resolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Description("Spring Message Resolver")
|
||||
public ResourceBundleMessageSource messageSource() {
|
||||
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
||||
messageSource.setBasename("messages");
|
||||
return messageSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocaleResolver localeResolver() {
|
||||
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
|
||||
localeResolver.setDefaultLocale(new Locale("en"));
|
||||
return localeResolver;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocaleChangeInterceptor localeChangeInterceptor() {
|
||||
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
|
||||
localeChangeInterceptor.setParamName("lang");
|
||||
return localeChangeInterceptor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(localeChangeInterceptor());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
registry.addResourceHandler("/resources/**", "/css/**")
|
||||
.addResourceLocations("/WEB-INF/resources/", "/WEB-INF/css/");
|
||||
}
|
||||
|
||||
@Override
|
||||
@Description("Custom Conversion Service")
|
||||
public void addFormatters(FormatterRegistry registry) {
|
||||
registry.addFormatter(new NameFormatter());
|
||||
}
|
||||
}
|
||||
-44
@@ -1,44 +0,0 @@
|
||||
package com.baeldung.thymeleaf.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
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.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
||||
import org.springframework.security.web.SecurityFilterChain;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
|
||||
public class WebMVCSecurity {
|
||||
|
||||
@Bean
|
||||
public InMemoryUserDetailsManager userDetailsService() {
|
||||
UserDetails user = User.withUsername("user1")
|
||||
.password("{noop}user1Pass")
|
||||
.authorities("ROLE_USER")
|
||||
.build();
|
||||
|
||||
return new InMemoryUserDetailsManager(user);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public WebSecurityCustomizer webSecurityCustomizer() {
|
||||
return (web) -> web.ignoring()
|
||||
.antMatchers("/resources/**");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||
http.authorizeRequests()
|
||||
.anyRequest()
|
||||
.authenticated()
|
||||
.and()
|
||||
.httpBasic();
|
||||
return http.build();
|
||||
}
|
||||
}
|
||||
-44
@@ -1,44 +0,0 @@
|
||||
package com.baeldung.thymeleaf.controller;
|
||||
|
||||
import com.baeldung.thymeleaf.model.Book;
|
||||
import com.baeldung.thymeleaf.service.BookService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
@Controller
|
||||
public class BookController {
|
||||
|
||||
@Autowired
|
||||
private BookService bookService;
|
||||
|
||||
@RequestMapping(value = "/listBooks", method = RequestMethod.GET)
|
||||
public String listBooks(Model model, @RequestParam("page") Optional<Integer> page, @RequestParam("size") Optional<Integer> size) {
|
||||
final int currentPage = page.orElse(1);
|
||||
final int pageSize = size.orElse(5);
|
||||
|
||||
Page<Book> bookPage = bookService.findPaginated(PageRequest.of(currentPage - 1, pageSize));
|
||||
|
||||
model.addAttribute("bookPage", bookPage);
|
||||
|
||||
int totalPages = bookPage.getTotalPages();
|
||||
if (totalPages > 0) {
|
||||
List<Integer> pageNumbers = IntStream.rangeClosed(1, totalPages)
|
||||
.boxed()
|
||||
.collect(Collectors.toList());
|
||||
model.addAttribute("pageNumbers", pageNumbers);
|
||||
}
|
||||
|
||||
return "listBooks.html";
|
||||
}
|
||||
}
|
||||
-26
@@ -1,26 +0,0 @@
|
||||
package com.baeldung.thymeleaf.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* Handles requests for the application home page.
|
||||
*
|
||||
*/
|
||||
@Controller
|
||||
public class HomeController {
|
||||
|
||||
@RequestMapping(value = "/", method = RequestMethod.GET)
|
||||
public String getHome(Model model) {
|
||||
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.getDefault());
|
||||
model.addAttribute("serverTime", dateFormat.format(new Date()));
|
||||
return "home.html";
|
||||
}
|
||||
|
||||
}
|
||||
-48
@@ -1,48 +0,0 @@
|
||||
package com.baeldung.thymeleaf.controller;
|
||||
|
||||
import com.baeldung.thymeleaf.model.Student;
|
||||
import com.baeldung.thymeleaf.utils.StudentUtils;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Handles requests for the student model.
|
||||
*
|
||||
*/
|
||||
@Controller
|
||||
public class StudentController {
|
||||
|
||||
@RequestMapping(value = "/saveStudent", method = RequestMethod.POST)
|
||||
public String saveStudent(@Valid @ModelAttribute Student student, BindingResult errors, Model model) {
|
||||
if (!errors.hasErrors()) {
|
||||
// get mock objects
|
||||
List<Student> students = StudentUtils.buildStudents();
|
||||
// add current student
|
||||
students.add(student);
|
||||
model.addAttribute("students", students);
|
||||
}
|
||||
return ((errors.hasErrors()) ? "addStudent.html" : "listStudents.html");
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/addStudent", method = RequestMethod.GET)
|
||||
public String addStudent(Model model) {
|
||||
model.addAttribute("student", new Student());
|
||||
return "addStudent.html";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/listStudents", method = RequestMethod.GET)
|
||||
public String listStudent(Model model) {
|
||||
|
||||
model.addAttribute("students", StudentUtils.buildStudents());
|
||||
|
||||
return "listStudents.html";
|
||||
}
|
||||
|
||||
}
|
||||
-18
@@ -1,18 +0,0 @@
|
||||
package com.baeldung.thymeleaf.controller;
|
||||
|
||||
import com.baeldung.thymeleaf.utils.TeacherUtils;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
@Controller
|
||||
public class TeacherController {
|
||||
|
||||
@RequestMapping(value = "/listTeachers", method = RequestMethod.GET)
|
||||
public String getInfo(Model model) {
|
||||
model.addAttribute("teachers", TeacherUtils.buildTeachers());
|
||||
|
||||
return "listTeachers.html";
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.baeldung.thymeleaf.expression;
|
||||
|
||||
public class Dino {
|
||||
private int id;
|
||||
private String name;
|
||||
private String color;
|
||||
private String weight;
|
||||
|
||||
public Dino(int id, String name, String color, String weight) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.color = color;
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
public Dino() {
|
||||
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getColor() {
|
||||
return color;
|
||||
}
|
||||
|
||||
public void setColor(String color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public String getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
public void setWeight(String weight) {
|
||||
this.weight = weight;
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.thymeleaf.expression;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Controller
|
||||
public class DinoController {
|
||||
|
||||
ArrayList<Dino> dinos = new ArrayList<Dino>();
|
||||
|
||||
@RequestMapping("/")
|
||||
public String dinoList(Model model) {
|
||||
Dino dinos = new Dino(1, "alpha", "red", "50kg");
|
||||
|
||||
model.addAttribute("dinos", new Dino());
|
||||
model.addAttribute("dinos", dinos);
|
||||
System.out.println(dinos);
|
||||
|
||||
return "templates-3/index";
|
||||
|
||||
}
|
||||
|
||||
@RequestMapping("/create")
|
||||
public String dinoCreate(Model model) {
|
||||
|
||||
model.addAttribute("dinos", new Dino());
|
||||
|
||||
return "templates-3/form";
|
||||
|
||||
}
|
||||
|
||||
@PostMapping("/dino")
|
||||
public String dinoSubmit(@ModelAttribute Dino dino, Model model) {
|
||||
|
||||
model.addAttribute("dino", dino);
|
||||
return "templates-3/result";
|
||||
}
|
||||
|
||||
}
|
||||
-30
@@ -1,30 +0,0 @@
|
||||
package com.baeldung.thymeleaf.formatter;
|
||||
|
||||
import org.springframework.format.Formatter;
|
||||
import org.thymeleaf.util.StringUtils;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
*
|
||||
* Name formatter class that implements the Spring Formatter interface.
|
||||
* Formats a name(String) and return the value with spaces replaced by commas.
|
||||
*
|
||||
*/
|
||||
public class NameFormatter implements Formatter<String> {
|
||||
|
||||
@Override
|
||||
public String print(String input, Locale locale) {
|
||||
return formatName(input, locale);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String parse(String input, Locale locale) throws ParseException {
|
||||
return formatName(input, locale);
|
||||
}
|
||||
|
||||
private String formatName(String input, Locale locale) {
|
||||
return StringUtils.replace(input, " ", ",");
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.thymeleaf.imageupload;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
@Controller public class UploadController {
|
||||
|
||||
public static String UPLOAD_DIRECTORY = System.getProperty("user.dir") + "/uploads";
|
||||
|
||||
@GetMapping("/uploadimage") public String displayUploadForm() {
|
||||
return "imageupload/index";
|
||||
}
|
||||
|
||||
@PostMapping("/upload") public String uploadImage(Model model, @RequestParam("image") MultipartFile file) throws IOException {
|
||||
StringBuilder fileNames = new StringBuilder();
|
||||
Path fileNameAndPath = Paths.get(UPLOAD_DIRECTORY, file.getOriginalFilename());
|
||||
fileNames.append(file.getOriginalFilename());
|
||||
Files.write(fileNameAndPath, file.getBytes());
|
||||
model.addAttribute("msg", "Uploaded images: " + fileNames.toString());
|
||||
return "imageupload/index";
|
||||
}
|
||||
}
|
||||
-29
@@ -1,29 +0,0 @@
|
||||
package com.baeldung.thymeleaf.model;
|
||||
|
||||
public class Book {
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public Book(int id, String name) {
|
||||
super();
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
-59
@@ -1,59 +0,0 @@
|
||||
package com.baeldung.thymeleaf.model;
|
||||
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
*
|
||||
* Simple student POJO with few fields
|
||||
*
|
||||
*/
|
||||
public class Student implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -8582553475226281591L;
|
||||
|
||||
@NotNull(message = "Student ID is required.")
|
||||
@Min(value = 1000, message = "Student ID must be at least 4 digits.")
|
||||
private Integer id;
|
||||
|
||||
@NotNull(message = "Student name is required.")
|
||||
private String name;
|
||||
|
||||
@NotNull(message = "Student gender is required.")
|
||||
private Character gender;
|
||||
|
||||
private Float percentage;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Character getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public void setGender(Character gender) {
|
||||
this.gender = gender;
|
||||
}
|
||||
|
||||
public Float getPercentage() {
|
||||
return percentage;
|
||||
}
|
||||
|
||||
public void setPercentage(Float percentage) {
|
||||
this.percentage = percentage;
|
||||
}
|
||||
}
|
||||
-77
@@ -1,77 +0,0 @@
|
||||
package com.baeldung.thymeleaf.model;
|
||||
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Teacher implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 946941572942270450L;
|
||||
|
||||
@NotNull(message = "Teacher ID is required.")
|
||||
@Min(value = 1000, message = "Teacher ID must be at least 4 digits.")
|
||||
private Integer id;
|
||||
|
||||
@NotNull(message = "Teacher name is required.")
|
||||
private String name;
|
||||
|
||||
@NotNull(message = "Teacher gender is required.")
|
||||
private String gender;
|
||||
|
||||
private boolean isActive;
|
||||
|
||||
private List<String> courses = new ArrayList<String>();
|
||||
|
||||
private String additionalSkills;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public void setGender(String gender) {
|
||||
this.gender = gender;
|
||||
}
|
||||
|
||||
public boolean isActive() {
|
||||
return isActive;
|
||||
}
|
||||
|
||||
public void setActive(boolean isActive) {
|
||||
this.isActive = isActive;
|
||||
}
|
||||
|
||||
public List<String> getCourses() {
|
||||
return courses;
|
||||
}
|
||||
|
||||
public void setCourses(List<String> courses) {
|
||||
this.courses = courses;
|
||||
}
|
||||
|
||||
public String getAdditionalSkills() {
|
||||
return additionalSkills;
|
||||
}
|
||||
|
||||
public void setAdditionalSkills(String additionalSkills) {
|
||||
this.additionalSkills = additionalSkills;
|
||||
}
|
||||
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.thymeleaf.mvcdata;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import com.baeldung.thymeleaf.mvcdata.repository.EmailData;
|
||||
|
||||
@Configuration
|
||||
public class BeanConfig {
|
||||
@Bean
|
||||
public EmailData emailData() {
|
||||
return new EmailData();
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.baeldung.thymeleaf.mvcdata;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import com.baeldung.thymeleaf.mvcdata.repository.EmailData;
|
||||
|
||||
@Controller
|
||||
public class EmailController {
|
||||
private EmailData emailData = new EmailData();
|
||||
private ServletContext servletContext;
|
||||
|
||||
public EmailController(ServletContext servletContext) {
|
||||
this.servletContext = servletContext;
|
||||
}
|
||||
|
||||
@GetMapping(value = "/email/modelattributes")
|
||||
public String emailModel(Model model) {
|
||||
model.addAttribute("emaildata", emailData);
|
||||
return "mvcdata/email-model-attributes";
|
||||
}
|
||||
|
||||
@ModelAttribute("emailModelAttribute")
|
||||
EmailData emailModelAttribute() {
|
||||
return emailData;
|
||||
}
|
||||
|
||||
@GetMapping(value = "/email/requestparameters")
|
||||
public String emailRequestParameters(
|
||||
@RequestParam(value = "emailsubject") String emailSubject,
|
||||
@RequestParam(value = "emailcontent") String emailContent,
|
||||
@RequestParam(value = "emailaddress") String emailAddress1,
|
||||
@RequestParam(value = "emailaddress") String emailAddress2,
|
||||
@RequestParam(value = "emaillocale") String emailLocale) {
|
||||
return "mvcdata/email-request-parameters";
|
||||
}
|
||||
|
||||
@GetMapping("/email/sessionattributes")
|
||||
public String emailSessionAttributes(HttpSession httpSession) {
|
||||
httpSession.setAttribute("emaildata", emailData);
|
||||
return "mvcdata/email-session-attributes";
|
||||
}
|
||||
|
||||
@GetMapping("/email/servletcontext")
|
||||
public String emailServletContext() {
|
||||
servletContext.setAttribute("emailsubject", emailData.getEmailSubject());
|
||||
servletContext.setAttribute("emailcontent", emailData.getEmailBody());
|
||||
servletContext.setAttribute("emailaddress", emailData.getEmailAddress1());
|
||||
servletContext.setAttribute("emaillocale", emailData.getEmailLocale());
|
||||
return "mvcdata/email-servlet-context";
|
||||
}
|
||||
|
||||
@GetMapping("/email/beandata")
|
||||
public String emailBeanData() {
|
||||
return "mvcdata/email-bean-data";
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package com.baeldung.thymeleaf.mvcdata.repository;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class EmailData implements Serializable {
|
||||
private String emailSubject;
|
||||
private String emailBody;
|
||||
private String emailLocale;
|
||||
private String emailAddress1;
|
||||
private String emailAddress2;
|
||||
|
||||
public EmailData() {
|
||||
this.emailSubject = "You have received a new message";
|
||||
this.emailBody = "Good morning !";
|
||||
this.emailLocale = "en-US";
|
||||
this.emailAddress1 = "[email protected]";
|
||||
this.emailAddress2 = "[email protected]";
|
||||
}
|
||||
|
||||
public String getEmailSubject() {
|
||||
return this.emailSubject;
|
||||
}
|
||||
|
||||
public String getEmailBody() {
|
||||
return this.emailBody;
|
||||
}
|
||||
|
||||
public String getEmailLocale() {
|
||||
return this.emailLocale;
|
||||
}
|
||||
|
||||
public String getEmailAddress1() {
|
||||
return this.emailAddress1;
|
||||
}
|
||||
|
||||
public String getEmailAddress2() {
|
||||
return this.emailAddress2;
|
||||
}
|
||||
|
||||
public List<String> getEmailAddresses() {
|
||||
List<String> emailAddresses = new ArrayList<>();
|
||||
emailAddresses.add(getEmailAddress1());
|
||||
emailAddresses.add(getEmailAddress2());
|
||||
return emailAddresses;
|
||||
}
|
||||
}
|
||||
-37
@@ -1,37 +0,0 @@
|
||||
package com.baeldung.thymeleaf.service;
|
||||
|
||||
import com.baeldung.thymeleaf.model.Book;
|
||||
import com.baeldung.thymeleaf.utils.BookUtils;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class BookService {
|
||||
|
||||
final private List<Book> books = BookUtils.buildBooks();
|
||||
|
||||
public Page<Book> findPaginated(Pageable pageable) {
|
||||
int pageSize = pageable.getPageSize();
|
||||
int currentPage = pageable.getPageNumber();
|
||||
int startItem = currentPage * pageSize;
|
||||
List<Book> list;
|
||||
|
||||
if (books.size() < startItem) {
|
||||
list = Collections.emptyList();
|
||||
} else {
|
||||
int toIndex = Math.min(startItem + pageSize, books.size());
|
||||
list = books.subList(startItem, toIndex);
|
||||
}
|
||||
|
||||
Page<Book> bookPage = new PageImpl<Book>(list, PageRequest.of(currentPage, pageSize), books.size());
|
||||
|
||||
return bookPage;
|
||||
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.thymeleaf.templatedir;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
@Controller
|
||||
public class HelloController {
|
||||
|
||||
@GetMapping("/hello")
|
||||
public String sayHello() {
|
||||
return "hello";
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.thymeleaf.url;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
@Controller
|
||||
public class UrlController {
|
||||
@GetMapping("/search") public String test(Model model, @RequestParam("query") String query){
|
||||
return "url/index";
|
||||
}
|
||||
}
|
||||
-8
@@ -1,8 +0,0 @@
|
||||
package com.baeldung.thymeleaf.utils;
|
||||
|
||||
public class ArrayUtil {
|
||||
|
||||
public static String[] array(String... args) {
|
||||
return args;
|
||||
}
|
||||
}
|
||||
-28
@@ -1,28 +0,0 @@
|
||||
package com.baeldung.thymeleaf.utils;
|
||||
|
||||
import com.baeldung.thymeleaf.model.Book;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class BookUtils {
|
||||
|
||||
private static List<Book> books = new ArrayList<Book>();
|
||||
|
||||
private static final int NUM_BOOKS = 30;
|
||||
|
||||
private static final int MIN_BOOK_NUM = 1000;
|
||||
|
||||
public static List<Book> buildBooks() {
|
||||
if (books.isEmpty()) {
|
||||
IntStream.range(0, NUM_BOOKS).forEach(n -> {
|
||||
books.add(new Book(MIN_BOOK_NUM + n + 1, "Spring in Action"));
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
return books;
|
||||
}
|
||||
|
||||
}
|
||||
-34
@@ -1,34 +0,0 @@
|
||||
package com.baeldung.thymeleaf.utils;
|
||||
|
||||
import com.baeldung.thymeleaf.model.Student;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class StudentUtils {
|
||||
|
||||
private static List<Student> students = new ArrayList<Student>();
|
||||
|
||||
public static List<Student> buildStudents() {
|
||||
if (students.isEmpty()) {
|
||||
Student student1 = new Student();
|
||||
student1.setId(1001);
|
||||
student1.setName("John Smith");
|
||||
student1.setGender('M');
|
||||
student1.setPercentage(Float.valueOf("80.45"));
|
||||
|
||||
students.add(student1);
|
||||
|
||||
Student student2 = new Student();
|
||||
student2.setId(1002);
|
||||
student2.setName("Jane Williams");
|
||||
student2.setGender('F');
|
||||
student2.setPercentage(Float.valueOf("60.25"));
|
||||
|
||||
students.add(student2);
|
||||
}
|
||||
|
||||
return students;
|
||||
}
|
||||
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user