-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spring Boot with Thymeleaf part 2 - Forms with validation
- Loading branch information
Showing
9 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/jvmhub/springboot/SpringBootThymeleafApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
18 changes: 18 additions & 0 deletions
18
src/test/java/com/jvmhub/springboot/SpringBootThymeleafApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
} | ||
|
||
} |