BAEL-4201 - Initial Commit (#9957)
* BAEL-4201 - Initial commit * BAEL-4201 - Initial Commit * Update UserAccountController.java removed the additional brackets * Update UserAccountValidationTest.java Updated the tests * Bael - 4201 - recommit for github issue fix For 'This commit does not belong to any branch on this repository' - delete and recommitting controller * Bael - 4201 - recommit for github issue fix For 'This commit does not belong to any branch on this repository' - delete and recommitting test * Bael - 4201 - add for github issue fix For 'This commit does not belong to any branch on this repository' - adding controller again * Bael - 4201 - add for github issue fix For 'This commit does not belong to any branch on this repository' - adding test again * BAEL-4201: Initial Commit * Update UserAccountUnitTest.java * BAEL-4201: updated to fix the commit issue * BAEL-4201 - temporary deleting Deleting to fix branch issue * BAEL-4201 - commiting test again * BAEL-4201 Fixing build issues * BAEL-4201 Github commit issue * BAEL-4201 Github commit issue - webapp folder * BAEL-4201 - commiting test after view upload * Bael- 4201 ThymeLeaf related issues * Bael- 4201 ThymeLeaf related issues * Bael- 4201 ThymeLeaf related issues * Bael- 4201 ThymeLeaf related issues - added html * Bael- 4201 ThymeLeaf issue ; updated suffix- html * Bael - 4201 white label issue * Bael - 4201 white label issue * BAEL-4201 : moving html to templates * BAEL-4201 - moving to templates * BAEL-4201 - moving to templates * BAEL-4201 - review related updates * BAEL-4201 - review related updates * BAEL-4201 - review related updates * BAEL-4201 - review related updates * BAEL-4201 - review related updates * BAEL-4201 - review related updates * BAEL-4201 - review related updates * BAEL-4201 - added the spring-boot-starter-validation dependency
This commit is contained in:
+12
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.springvalidation;
|
||||
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
||||
|
||||
public class ServletInitializer extends SpringBootServletInitializer {
|
||||
|
||||
@Override
|
||||
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
|
||||
return application.sources(SpringValidationApplication.class);
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.springvalidation;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringValidationApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringValidationApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.springvalidation.controller;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
|
||||
import com.baeldung.springvalidation.domain.UserAccount;
|
||||
import com.baeldung.springvalidation.interfaces.BasicInfo;
|
||||
|
||||
@Controller
|
||||
public class UserAccountController {
|
||||
|
||||
@GetMapping("/getUserForm")
|
||||
public String showUserForm(Model theModel) {
|
||||
UserAccount theUser = new UserAccount();
|
||||
theModel.addAttribute("useraccount", theUser);
|
||||
return "userHome";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/saveBasicInfo", method = RequestMethod.POST)
|
||||
public String saveBasicInfo(@Valid @ModelAttribute("useraccount") UserAccount useraccount, BindingResult result, ModelMap model) {
|
||||
if (result.hasErrors()) {
|
||||
return "error";
|
||||
}
|
||||
return "success";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/saveBasicInfoStep1", method = RequestMethod.POST)
|
||||
public String saveBasicInfoStep1(@Validated(BasicInfo.class) @ModelAttribute("useraccount") UserAccount useraccount, BindingResult result, ModelMap model) {
|
||||
if (result.hasErrors()) {
|
||||
return "error";
|
||||
}
|
||||
return "success";
|
||||
}
|
||||
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
package com.baeldung.springvalidation.domain;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
import com.baeldung.springvalidation.interfaces.AdvanceInfo;
|
||||
import com.baeldung.springvalidation.interfaces.BasicInfo;
|
||||
|
||||
public class UserAccount {
|
||||
|
||||
@NotNull(groups = BasicInfo.class)
|
||||
@Size(min = 4, max = 15, groups = BasicInfo.class)
|
||||
private String password;
|
||||
|
||||
@NotBlank(groups = BasicInfo.class)
|
||||
private String name;
|
||||
|
||||
@Min(value = 18, message = "Age should not be less than 18", groups = AdvanceInfo.class)
|
||||
private int age;
|
||||
|
||||
@NotBlank(groups = AdvanceInfo.class)
|
||||
private String phone;
|
||||
|
||||
@Valid
|
||||
@NotNull(groups = AdvanceInfo.class)
|
||||
private UserAddress useraddress;
|
||||
|
||||
public UserAddress getUseraddress() {
|
||||
return useraddress;
|
||||
}
|
||||
|
||||
public void setUseraddress(UserAddress useraddress) {
|
||||
this.useraddress = useraddress;
|
||||
}
|
||||
|
||||
public UserAccount() {
|
||||
|
||||
}
|
||||
|
||||
public UserAccount(String email, String password, String name, int age) {
|
||||
this.password = password;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getPhone() {
|
||||
return phone;
|
||||
}
|
||||
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.springvalidation.domain;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
public class UserAddress {
|
||||
|
||||
@NotBlank
|
||||
private String countryCode;
|
||||
|
||||
public String getCountryCode() {
|
||||
return countryCode;
|
||||
}
|
||||
|
||||
public void setCountryCode(String countryCode) {
|
||||
this.countryCode = countryCode;
|
||||
}
|
||||
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.springvalidation.interfaces;
|
||||
|
||||
public interface AdvanceInfo {
|
||||
// validation group marker interface
|
||||
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.springvalidation.interfaces;
|
||||
|
||||
public interface BasicInfo {
|
||||
// validation group marker interface
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user