Skip to content

Commit

Permalink
Spring Boot with Thymeleaf part 2 - Forms with validation
Browse files Browse the repository at this point in the history
  • Loading branch information
hubek committed Aug 2, 2015
1 parent f902d43 commit 13b03a1
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# Spring-Boot-with-Thymeleaf-part-2-forms-with-validation
Spring Boot with Thymeleaf tutorial, part 2 - Forms with validation
All tutorial you can find on jvmhub.com -> http://jvmhub.com/2015/08/02/spring-boot-with-thymeleaf-tutorial-part-2-forms-with-validation
49 changes: 49 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<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.jvmhub</groupId>
<artifactId>spring-boot-thymeleaf-form</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>

<name>spring-boot-thymeleaf-form</name>
<description>Spring Boot with Thymeleaf, part 2 - Forms</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.jvmhub.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootThymeleafApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBootThymeleafApplication.class, args);
}
}
30 changes: 30 additions & 0 deletions src/main/java/com/jvmhub/springboot/controller/Home.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.jvmhub.springboot.controller;

import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jvmhub.springboot.form.Post;

@Controller
public class Home {

@RequestMapping(value="/", method=RequestMethod.GET)
public String index(Post post) {
return "index";
}

@RequestMapping(value = "/", method = RequestMethod.POST)
public String addNewPost(@Valid Post post, BindingResult bindingResult, Model model) {
if (bindingResult.hasErrors()) {
return "index";
}
model.addAttribute("title", post.getTitle());
model.addAttribute("content", post.getContent());
return "result";
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/jvmhub/springboot/form/Post.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.jvmhub.springboot.form;

import javax.validation.constraints.Size;

public class Post {

@Size(min=4, max=35)
private String title;

@Size(min=30, max= 1000)
private String content;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}
}
Empty file.
27 changes: 27 additions & 0 deletions src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Spring Boot and Thymeleaf example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h3>Spring Boot and Thymeleaf, part 2 - forms</h3>
<form action="#" th:action="@{/}" th:object="${post}" method="post">
<table>
<tr>
<td>Title:</td>
<td><input type="text" th:field="*{title}" /></td>
<td th:if="${#fields.hasErrors('title')}" th:errors="*{title}">Title error message</td>
</tr>
<tr>
<td>Content:</td>
<td><input type="text" th:field="*{content}" /></td>
<td th:if="${#fields.hasErrors('content')}" th:errors="*{content}">Content error message</td>
</tr>
<tr>
<td><button type="submit">Submit post</button></td>
</tr>
</table>
</form>
</body>
</html>
14 changes: 14 additions & 0 deletions src/main/resources/templates/result.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Spring Boot and Thymeleaf example</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h3>Spring Boot and Thymeleaf, part 2 - forms</h3>
<h4>Title:</h4>
<p th:text="${title}" />
<h4>Content:</h4>
<p th:text="${content}" />
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.jvmhub.springboot;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SpringBootThymeleafApplication.class)
@WebAppConfiguration
public class SpringBootThymeleafApplicationTests {

@Test
public void contextLoads() {
}

}

0 comments on commit 13b03a1

Please sign in to comment.