diff --git a/spring-mvc-forms/pom.xml b/spring-mvc-forms/pom.xml index 35ed00c0e9..e5ffb52801 100644 --- a/spring-mvc-forms/pom.xml +++ b/spring-mvc-forms/pom.xml @@ -46,6 +46,12 @@ commons-fileupload ${fileupload.version} + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + @@ -89,16 +95,17 @@ - 4.3.4.RELEASE + 4.3.7.RELEASE 2.6 1.2 2.3.1 3.1.0 3.6.0 1.8 - 5.3.3.Final + 5.4.0.Final enter-location-of-server 1.3.2 + 2.8.7 diff --git a/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/configuration/ApplicationConfiguration.java b/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/configuration/ApplicationConfiguration.java index 3f7889422f..7292d95b21 100644 --- a/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/configuration/ApplicationConfiguration.java +++ b/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/configuration/ApplicationConfiguration.java @@ -27,6 +27,16 @@ class ApplicationConfiguration extends WebMvcConfigurerAdapter { bean.setSuffix(".jsp"); return bean; } + + + @Bean + public InternalResourceViewResolver htmlViewResolver() { + InternalResourceViewResolver bean = new InternalResourceViewResolver(); + bean.setPrefix("/WEB-INF/html/"); + bean.setSuffix(".html"); + bean.setOrder(2); + return bean; + } @Bean public MultipartResolver multipartResolver() { diff --git a/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/controller/UserController.java b/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/controller/UserController.java new file mode 100644 index 0000000000..880b224dd9 --- /dev/null +++ b/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/controller/UserController.java @@ -0,0 +1,63 @@ +package com.baeldung.springmvcforms.controller; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import javax.validation.Valid; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.validation.ObjectError; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +import com.baeldung.springmvcforms.domain.User; + +@Controller +public class UserController { + + List users = new ArrayList() { + { + add(new User("ana@yahoo.com", "pass", "Ana", 20)); + add(new User("bob@yahoo.com", "pass", "Bob", 30)); + add(new User("john@yahoo.com", "pass", "John", 40)); + add(new User("mary@yahoo.com", "pass", "Mary", 30)); + } + }; + + @GetMapping("/userPage") + public String getUserProfilePage() { + return "user"; + } + + @PostMapping(value = "/user", produces = MediaType.APPLICATION_JSON_VALUE) + @ResponseBody + public ResponseEntity saveUser(@Valid User user, BindingResult result, Model model) { + if (result.hasErrors()) { + List errors = new ArrayList<>(); + result.getAllErrors().forEach(item->{ + errors.add(item.getDefaultMessage()); + }); + return new ResponseEntity<>(errors, HttpStatus.OK); + } else { + if (users.stream().anyMatch(it -> user.getEmail().equals(it.getEmail()))) { + return new ResponseEntity<>(Collections.singletonList("Email already exists!"), HttpStatus.CONFLICT); + } else { + users.add(user); + return new ResponseEntity<>(HttpStatus.CREATED); + } + } + } + + @GetMapping(value = "/users", produces = MediaType.APPLICATION_JSON_VALUE) + @ResponseBody + public List getUsers() { + return users; + } + +} diff --git a/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/domain/User.java b/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/domain/User.java new file mode 100644 index 0000000000..49f006f422 --- /dev/null +++ b/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/domain/User.java @@ -0,0 +1,71 @@ +package com.baeldung.springmvcforms.domain; + +import javax.validation.constraints.Digits; +import javax.validation.constraints.Min; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; + +import org.hibernate.validator.constraints.Email; +import org.hibernate.validator.constraints.NotBlank; + +public class User { + + @NotNull + @Email + private String email; + + @NotNull + @Size(min = 4, max = 15) + private String password; + + @NotBlank + private String name; + + @Min(18) + @Digits(integer = 2, fraction = 0) + private int age; + + public User() { + + } + + public User(String email, String password, String name, int age) { + super(); + this.email = email; + this.password = password; + this.name = name; + this.age = age; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } +} diff --git a/spring-mvc-forms/src/main/webapp/WEB-INF/html/user.html b/spring-mvc-forms/src/main/webapp/WEB-INF/html/user.html new file mode 100644 index 0000000000..a505f51f32 --- /dev/null +++ b/spring-mvc-forms/src/main/webapp/WEB-INF/html/user.html @@ -0,0 +1,68 @@ + + + + +Users + + + + + + + + + + +Email: + + + Invalid email! + Email is required! + +✓ + +Password: + + + Password must be at least 4 characters! + Password must not be longer than 15 characters! + Password is required! + +✓ + +Name: + + + Name is required! + +✓ + + Age: + + + Age must be greater than 18! + Age must be greater than 18! + + ✓ + +Save +Reset + + + +{{errorMessage}} +{{message}} + + +All Users: + + +EmailNameAge + + +{{usr.email}} {{usr.name}} {{usr.age}} + + + + \ No newline at end of file diff --git a/spring-mvc-forms/src/main/webapp/css/user.css b/spring-mvc-forms/src/main/webapp/css/user.css new file mode 100644 index 0000000000..bcfb62a33c --- /dev/null +++ b/spring-mvc-forms/src/main/webapp/css/user.css @@ -0,0 +1,77 @@ +.form-label { + display:block; + margin-top:16px; + font-weight:bold; +} + +.form-input { + border-radius:5px; + display:inline; +} + +.form-input p { + margin:0; +} + +.form-button { + margin:5px; +} + +.form-error input.ng-invalid { + border-color:red; +} + +.check { + display:inline; + color:green; + font-weight:bold; +} + +.error-messages { + color:red; +} + +.error-messages p { + margin:0; +} + +#customers { + font-family: "Trebuchet MS", Arial, Helvetica, sans-serif; + border-collapse: collapse; + width: 100%; +} + +#users td, #users th { + border: 1px solid dodgerblue; + border-collapse:collapse; + padding: 4px; + width:200px; +} + +#users tr:nth-child(even){background-color: lavender;} + +#users th { + padding-top: 10px; + padding-bottom: 10px; + text-align: left; + background-color: dodgerblue; +} + +#users { + border-collapse:collapse; +} + +button { + border-radius:5px; + cursor:pointer; + margin:10px; + padding:5px; +} + +.form-button-save { + background-color:lightgreen; +} + +.form-button-reset { + background-color:lightpink; +} \ No newline at end of file diff --git a/spring-mvc-forms/src/main/webapp/js/app.js b/spring-mvc-forms/src/main/webapp/js/app.js new file mode 100644 index 0000000000..6a290b3cf0 --- /dev/null +++ b/spring-mvc-forms/src/main/webapp/js/app.js @@ -0,0 +1,70 @@ +var app = angular.module('app', ['ngMessages']); + +app.controller('UserCtrl', ['$scope','UserService', function ($scope,UserService) { + + $scope.submitted = false; + + $scope.getUsers = function() { + UserService.getUsers().then( function(data){ + $scope.users = data; + }); + } + + $scope.saveUser = function () { + $scope.submitted = true; + if ($scope.userForm.$valid){ + UserService.saveUser($scope.user) + .then (function success(response){ + $scope.message = 'User added!'; + $scope.errorMessage = ''; + $scope.getUsers(); + $scope.user = null; + $scope.submitted = false; + }, + function error(response){ + if (response.status == 409){ + $scope.errorMessage = response.data[0]; + } + else { + $scope.errorMessage = 'Error adding user!'; + } + $scope.message = ''; + }); + } + } + + $scope.getUsers(); + + $scope.resetForm = function () { + $scope.userForm.$setPristine(); + $scope.user=null; + $scope.message=''; + $scope.errorMessage=''; + $scope.submitted = false; + } + +}]); + +app.service('UserService',['$http', function ($http) { + + this.saveUser = function saveUser(user){ + return $http({ + method: 'POST', + url: 'user', + params: {email:user.email, password:user.password, name:user.name, age:user.age}, + headers: 'Accept:application/json' + }); + } + + + this.getUsers = function getUsers(){ + return $http({ + method: 'GET', + url: 'users', + headers:'Accept:application/json' + }).then( function(response){ + return response.data; + } ); + } + +}]); \ No newline at end of file
Invalid email!
Email is required!
Password must be at least 4 characters!
Password must not be longer than 15 characters!
Password is required!
Name is required!
Age must be greater than 18!