File tree Expand file tree Collapse file tree
spring-mvc-java/src/main/java/com/baeldung Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -11,10 +11,7 @@ public void initialize(ContactNumberConstraint contactNumber) {
1111
1212 @ Override
1313 public boolean isValid (String contactField , ConstraintValidatorContext cxt ) {
14- if (contactField == null ) {
15- return false ;
16- }
17- return contactField .matches ("[0-9]+" ) && (contactField .length () > 8 ) && (contactField .length () < 14 );
14+ return contactField != null && contactField .matches ("[0-9]+" ) && (contactField .length () > 8 ) && (contactField .length () < 14 );
1815 }
1916
2017}
Original file line number Diff line number Diff line change 33import com .baeldung .model .User ;
44import org .springframework .stereotype .Controller ;
55import org .springframework .ui .Model ;
6+ import org .springframework .web .bind .annotation .GetMapping ;
67import org .springframework .web .bind .annotation .ModelAttribute ;
8+ import org .springframework .web .bind .annotation .PostMapping ;
79import org .springframework .web .bind .annotation .RequestMapping ;
8- import org .springframework .web .bind .annotation .RequestMethod ;
910
1011@ Controller
1112@ RequestMapping ("/" )
1213public class UserController {
1314
14- @ RequestMapping ( value = "/" , method = RequestMethod . GET )
15+ @ GetMapping ( "/" )
1516 public String showForm (final Model model ) {
1617 final User user = new User ();
1718 user .setFirstname ("John" );
@@ -21,7 +22,7 @@ public String showForm(final Model model) {
2122 return "index" ;
2223 }
2324
24- @ RequestMapping ( value = "/processForm" , method = RequestMethod . POST )
25+ @ PostMapping ( "/processForm" )
2526 public String processForm (@ ModelAttribute (value = "user" ) final User user , final Model model ) {
2627 // Insert User into DB
2728 model .addAttribute ("name" , user .getFirstname () + " " + user .getLastname ());
Original file line number Diff line number Diff line change 11package com .baeldung .web .controller ;
22
3- import javax .validation .Valid ;
4-
3+ import com .baeldung .model .ValidatedPhone ;
54import org .springframework .stereotype .Controller ;
65import org .springframework .ui .Model ;
76import org .springframework .validation .BindingResult ;
8- import org .springframework .web .bind .annotation .RequestMapping ;
9- import org .springframework .web .bind .annotation .RequestMethod ;
7+ import org .springframework .web .bind .annotation .GetMapping ;
8+ import org .springframework .web .bind .annotation .PostMapping ;
109import org .springframework .web .servlet .config .annotation .EnableWebMvc ;
1110
12- import com . baeldung . model . ValidatedPhone ;
11+ import javax . validation . Valid ;
1312
1413@ Controller
1514@ EnableWebMvc
1615public class ValidatedPhoneController {
1716
18- @ RequestMapping ( value = "/validatePhone" , method = RequestMethod . GET )
17+ @ GetMapping ( "/validatePhone" )
1918 public String loadFormPage (Model m ) {
2019 m .addAttribute ("validatedPhone" , new ValidatedPhone ());
2120 return "phoneHome" ;
2221 }
2322
24- @ RequestMapping ( value = "/addValidatePhone" , method = RequestMethod . POST )
23+ @ PostMapping ( "/addValidatePhone" )
2524 public String submitForm (@ Valid ValidatedPhone validatedPhone , BindingResult result , Model m ) {
2625 if (result .hasErrors ()) {
2726 return "phoneHome" ;
You can’t perform that action at this time.
0 commit comments