File tree Expand file tree Collapse file tree 9 files changed +176
-0
lines changed
springboot-examples/springboot-freemarker-form-validation
java/com/hellokoding/springboot Expand file tree Collapse file tree 9 files changed +176
-0
lines changed Original file line number Diff line number Diff line change 1+ target
2+ out
3+ .settings
4+ .classpath
5+ .project
6+ .idea
7+ * .iml
Original file line number Diff line number Diff line change 1+ <?xml version =" 1.0" encoding =" UTF-8" ?>
2+ <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" >
3+ <modelVersion >4.0.0</modelVersion >
4+ <groupId >com.hellokoding.springboot</groupId >
5+ <artifactId >freemarker.form.validation</artifactId >
6+ <parent >
7+ <groupId >org.springframework.boot</groupId >
8+ <artifactId >spring-boot-starter-parent</artifactId >
9+ <version >2.1.4.RELEASE</version >
10+ </parent >
11+
12+ <properties >
13+ <java .version>1.8</java .version>
14+ </properties >
15+
16+ <dependencies >
17+ <dependency >
18+ <groupId >org.springframework.boot</groupId >
19+ <artifactId >spring-boot-starter-web</artifactId >
20+ </dependency >
21+ <dependency >
22+ <groupId >org.springframework.boot</groupId >
23+ <artifactId >spring-boot-starter-freemarker</artifactId >
24+ </dependency >
25+ </dependencies >
26+ <build >
27+ <plugins >
28+ <plugin >
29+ <groupId >org.springframework.boot</groupId >
30+ <artifactId >spring-boot-maven-plugin</artifactId >
31+ </plugin >
32+ </plugins >
33+ </build >
34+ </project >
Original file line number Diff line number Diff line change 1+ package com .hellokoding .springboot ;
2+
3+ import org .springframework .stereotype .Controller ;
4+ import org .springframework .ui .Model ;
5+ import org .springframework .validation .BindingResult ;
6+ import org .springframework .web .bind .annotation .GetMapping ;
7+ import org .springframework .web .bind .annotation .PostMapping ;
8+
9+ import javax .validation .Valid ;
10+
11+ @ Controller
12+ public class FormController {
13+ @ GetMapping ("/" )
14+ public String index () {
15+ return "redirect:/form" ;
16+ }
17+
18+ @ GetMapping ("/form" )
19+ public String formGet (Model model ) {
20+ model .addAttribute ("user" , new User ());
21+ return "form" ;
22+ }
23+
24+ @ PostMapping ("/form" )
25+ public String formPost (@ Valid User user , BindingResult bindingResult , Model model ) {
26+ if (!bindingResult .hasErrors ()) {
27+ model .addAttribute ("noErrors" , true );
28+ }
29+ model .addAttribute ("user" , user );
30+ return "form" ;
31+ }
32+ }
Original file line number Diff line number Diff line change 1+ package com .hellokoding .springboot ;
2+
3+ import javax .validation .constraints .Email ;
4+ import javax .validation .constraints .NotEmpty ;
5+
6+ public class User {
7+ @ NotEmpty
8+ private String firstName ;
9+
10+ @ NotEmpty
11+ private String lastName ;
12+
13+ @ NotEmpty
14+ @ Email
15+ private String email ;
16+
17+ public String getFirstName () {
18+ return firstName ;
19+ }
20+
21+ public void setFirstName (String firstName ) {
22+ this .firstName = firstName ;
23+ }
24+
25+ public String getLastName () {
26+ return lastName ;
27+ }
28+
29+ public void setLastName (String lastName ) {
30+ this .lastName = lastName ;
31+ }
32+
33+ public String getEmail () {
34+ return email ;
35+ }
36+
37+ public void setEmail (String email ) {
38+ this .email = email ;
39+ }
40+ }
Original file line number Diff line number Diff line change 1+ package com .hellokoding .springboot ;
2+
3+ import org .springframework .boot .SpringApplication ;
4+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
5+
6+ @ SpringBootApplication
7+ public class WebApplication {
8+ public static void main (String [] args ) throws Exception {
9+ SpringApplication .run (WebApplication .class , args );
10+ }
11+ }
12+
Original file line number Diff line number Diff line change 1+ spring.freemarker.template-loader-path : classpath:/templates
2+ spring.freemarker.suffix : .ftl
Original file line number Diff line number Diff line change 1+ h2 {
2+ color : darkgreen;
3+ }
4+
5+ form b {
6+ color : red;
7+ }
Original file line number Diff line number Diff line change 1+ ( function ( ) {
2+ console . log ( "Hello World!" ) ;
3+ } ) ( ) ;
Original file line number Diff line number Diff line change 1+ <#import "/spring.ftl" as spring / >
2+
3+ <!DOCTYPE html>
4+ <html lang =" en" >
5+ <head >
6+ <meta charset =" UTF-8" >
7+ <title >Form Data Binding and Validation</title >
8+ <link href =" /css/main.css" rel =" stylesheet" >
9+ </head >
10+ <body >
11+ <h2 >Form Data Binding and Validation</h2 >
12+
13+ <@spring.bind "user"/ >
14+ <#if user?? && noErrors?? >
15+ Your submitted data<br >
16+ First name: ${user.firstName} <br >
17+ Last name: ${user.lastName} <br >
18+ Email: ${user.email} <br >
19+ <#else >
20+ <form action =" /form" method =" post" >
21+ First name:<br >
22+ <@spring.formInput "user.firstName"/ >
23+ <@spring.showErrors "<br >"/>
24+ <br ><br >
25+ Last name:<br >
26+ <@spring.formInput "user.lastName"/ >
27+ <@spring.showErrors "<br >"/>
28+ <br ><br >
29+ Email:<br >
30+ <@spring.formInput "user.email"/ >
31+ <@spring.showErrors "<br >"/>
32+ <br ><br >
33+ <input type =" submit" value =" Submit" >
34+ </form >
35+ </#if >
36+
37+ <script src =" /js/main.js" ></script >
38+ </body >
39+ </html >
You can’t perform that action at this time.
0 commit comments