diff --git a/spring-web-modules/spring-mvc-basics-2/README.md b/spring-web-modules/spring-mvc-basics-2/README.md index cbc9f93e93..ae8256fc4c 100644 --- a/spring-web-modules/spring-mvc-basics-2/README.md +++ b/spring-web-modules/spring-mvc-basics-2/README.md @@ -12,5 +12,4 @@ This module contains articles about Spring MVC - [Request Method Not Supported (405) in Spring](https://www.baeldung.com/spring-request-method-not-supported-405) - [Spring @RequestParam Annotation](https://www.baeldung.com/spring-request-param) - [Spring @RequestParam vs @PathVariable Annotations](https://www.baeldung.com/spring-requestparam-vs-pathvariable) -- More articles: [[more -->]](/spring-mvc-basics-3) -- More articles: [[<-- prev]](/spring-mvc-basics) +- More articles: [[<-- prev]](../spring-mvc-basics)[[more -->]](../spring-mvc-basics-3) diff --git a/spring-web-modules/spring-mvc-basics-3/README.md b/spring-web-modules/spring-mvc-basics-3/README.md index 86c26d8a60..391c8380ab 100644 --- a/spring-web-modules/spring-mvc-basics-3/README.md +++ b/spring-web-modules/spring-mvc-basics-3/README.md @@ -10,4 +10,4 @@ This module contains articles about Spring MVC - [Using Enums as Request Parameters in Spring](https://www.baeldung.com/spring-enum-request-param) - [Guide to Flash Attributes in a Spring Web Application](https://www.baeldung.com/spring-web-flash-attributes) - [Reading HttpServletRequest Multiple Times in Spring](https://www.baeldung.com/spring-reading-httpservletrequest-multiple-times) -- More articles: [[<-- prev]](/spring-mvc-basics-2)[[more -->]](/spring-mvc-basics-4) +- More articles: [[<-- prev]](../spring-mvc-basics-2)[[more -->]](../spring-mvc-basics-4) diff --git a/spring-web-modules/spring-mvc-basics-4/README.md b/spring-web-modules/spring-mvc-basics-4/README.md index f003f7df01..50ceb724d1 100644 --- a/spring-web-modules/spring-mvc-basics-4/README.md +++ b/spring-web-modules/spring-mvc-basics-4/README.md @@ -10,4 +10,4 @@ The "REST With Spring" Classes: https://bit.ly/restwithspring - [Spring Optional Path variables](https://www.baeldung.com/spring-optional-path-variables) - [JSON Parameters with Spring MVC](https://www.baeldung.com/spring-mvc-send-json-parameters) - [How to Set JSON Content Type In Spring MVC](https://www.baeldung.com/spring-mvc-set-json-content-type) -- More articles: [[<-- prev]](/spring-mvc-basics-3) +- More articles: [[<-- prev]](../spring-mvc-basics-3)[[more -->]](../spring-mvc-basics-5) diff --git a/spring-web-modules/spring-mvc-basics-5/.gitignore b/spring-web-modules/spring-mvc-basics-5/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/spring-web-modules/spring-mvc-basics-5/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/spring-web-modules/spring-mvc-basics-5/README.md b/spring-web-modules/spring-mvc-basics-5/README.md new file mode 100644 index 0000000000..83a335f4d3 --- /dev/null +++ b/spring-web-modules/spring-mvc-basics-5/README.md @@ -0,0 +1,14 @@ +## Spring MVC Basics + +This module contains articles about the basics of Spring MVC. Articles about more specific areas of Spring MVC have +their own module. + +### The Course +The "REST With Spring" Classes: https://bit.ly/restwithspring + +### Relevant Articles: +- [Spring MVC Custom Validation](https://www.baeldung.com/spring-mvc-custom-validator) +- [Using Spring @ResponseStatus to Set HTTP Status Code](https://www.baeldung.com/spring-response-status) +- [Spring MVC and the @ModelAttribute Annotation](https://www.baeldung.com/spring-mvc-and-the-modelattribute-annotation) +- [The HttpMediaTypeNotAcceptableException in Spring MVC](https://www.baeldung.com/spring-httpmediatypenotacceptable) +- More articles: [[<-- prev]](../spring-mvc-basics-4) diff --git a/spring-web-modules/spring-mvc-basics-5/pom.xml b/spring-web-modules/spring-mvc-basics-5/pom.xml new file mode 100644 index 0000000000..3b64f15c4b --- /dev/null +++ b/spring-web-modules/spring-mvc-basics-5/pom.xml @@ -0,0 +1,57 @@ + + + 4.0.0 + spring-mvc-basics-5 + 0.1-SNAPSHOT + spring-mvc-basics-5 + jar + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-validation + + + + org.apache.tomcat.embed + tomcat-embed-jasper + + + javax.servlet + jstl + + + org.springframework.boot + spring-boot-starter-test + test + + + + + spring-mvc-basics + + + org.springframework.boot + spring-boot-maven-plugin + + com.baeldung.Application + JAR + + + + + + \ No newline at end of file diff --git a/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/Application.java b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/Application.java new file mode 100644 index 0000000000..d58049fb35 --- /dev/null +++ b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/Application.java @@ -0,0 +1,11 @@ +package com.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/spring-web-modules/spring-mvc-basics/src/main/java/com/baeldung/customvalidator/ContactNumberConstraint.java b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/customvalidator/ContactNumberConstraint.java similarity index 100% rename from spring-web-modules/spring-mvc-basics/src/main/java/com/baeldung/customvalidator/ContactNumberConstraint.java rename to spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/customvalidator/ContactNumberConstraint.java diff --git a/spring-web-modules/spring-mvc-basics/src/main/java/com/baeldung/customvalidator/ContactNumberValidator.java b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/customvalidator/ContactNumberValidator.java similarity index 100% rename from spring-web-modules/spring-mvc-basics/src/main/java/com/baeldung/customvalidator/ContactNumberValidator.java rename to spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/customvalidator/ContactNumberValidator.java diff --git a/spring-web-modules/spring-mvc-basics/src/main/java/com/baeldung/customvalidator/FieldsValueMatch.java b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/customvalidator/FieldsValueMatch.java similarity index 100% rename from spring-web-modules/spring-mvc-basics/src/main/java/com/baeldung/customvalidator/FieldsValueMatch.java rename to spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/customvalidator/FieldsValueMatch.java diff --git a/spring-web-modules/spring-mvc-basics/src/main/java/com/baeldung/customvalidator/FieldsValueMatchValidator.java b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/customvalidator/FieldsValueMatchValidator.java similarity index 100% rename from spring-web-modules/spring-mvc-basics/src/main/java/com/baeldung/customvalidator/FieldsValueMatchValidator.java rename to spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/customvalidator/FieldsValueMatchValidator.java diff --git a/spring-web-modules/spring-mvc-basics/src/main/java/com/baeldung/web/controller/NewUserController.java b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/customvalidator/NewUserController.java similarity index 91% rename from spring-web-modules/spring-mvc-basics/src/main/java/com/baeldung/web/controller/NewUserController.java rename to spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/customvalidator/NewUserController.java index fddd8baabb..c71d862941 100644 --- a/spring-web-modules/spring-mvc-basics/src/main/java/com/baeldung/web/controller/NewUserController.java +++ b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/customvalidator/NewUserController.java @@ -1,6 +1,4 @@ -package com.baeldung.web.controller; - -import javax.validation.Valid; +package com.baeldung.customvalidator; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; @@ -8,7 +6,7 @@ import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; -import com.baeldung.model.NewUserForm; +import javax.validation.Valid; @Controller public class NewUserController { diff --git a/spring-web-modules/spring-mvc-basics/src/main/java/com/baeldung/model/NewUserForm.java b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/customvalidator/NewUserForm.java similarity index 97% rename from spring-web-modules/spring-mvc-basics/src/main/java/com/baeldung/model/NewUserForm.java rename to spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/customvalidator/NewUserForm.java index b4fe82ab4a..0685f0373c 100644 --- a/spring-web-modules/spring-mvc-basics/src/main/java/com/baeldung/model/NewUserForm.java +++ b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/customvalidator/NewUserForm.java @@ -1,4 +1,4 @@ -package com.baeldung.model; +package com.baeldung.customvalidator; import com.baeldung.customvalidator.FieldsValueMatch; diff --git a/spring-web-modules/spring-mvc-basics/src/main/java/com/baeldung/model/ValidatedPhone.java b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/customvalidator/ValidatedPhone.java similarity index 90% rename from spring-web-modules/spring-mvc-basics/src/main/java/com/baeldung/model/ValidatedPhone.java rename to spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/customvalidator/ValidatedPhone.java index 8cea915e6e..25676e49ec 100644 --- a/spring-web-modules/spring-mvc-basics/src/main/java/com/baeldung/model/ValidatedPhone.java +++ b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/customvalidator/ValidatedPhone.java @@ -1,4 +1,4 @@ -package com.baeldung.model; +package com.baeldung.customvalidator; import com.baeldung.customvalidator.ContactNumberConstraint; diff --git a/spring-web-modules/spring-mvc-basics/src/main/java/com/baeldung/web/controller/ValidatedPhoneController.java b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/customvalidator/ValidatedPhoneController.java similarity index 91% rename from spring-web-modules/spring-mvc-basics/src/main/java/com/baeldung/web/controller/ValidatedPhoneController.java rename to spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/customvalidator/ValidatedPhoneController.java index 73e1e4bb25..be347a6777 100644 --- a/spring-web-modules/spring-mvc-basics/src/main/java/com/baeldung/web/controller/ValidatedPhoneController.java +++ b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/customvalidator/ValidatedPhoneController.java @@ -1,4 +1,4 @@ -package com.baeldung.web.controller; +package com.baeldung.customvalidator; import javax.validation.Valid; @@ -8,8 +8,6 @@ import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; -import com.baeldung.model.ValidatedPhone; - @Controller public class ValidatedPhoneController { diff --git a/spring-web-modules/spring-mvc-basics/src/main/java/com/baeldung/exception/HttpMediaTypeNotAcceptableExceptionExampleController.java b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/exception/HttpMediaTypeNotAcceptableExceptionExampleController.java similarity index 100% rename from spring-web-modules/spring-mvc-basics/src/main/java/com/baeldung/exception/HttpMediaTypeNotAcceptableExceptionExampleController.java rename to spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/exception/HttpMediaTypeNotAcceptableExceptionExampleController.java diff --git a/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/modelattribute/Employee.java b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/modelattribute/Employee.java new file mode 100644 index 0000000000..0ec3c5c374 --- /dev/null +++ b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/modelattribute/Employee.java @@ -0,0 +1,61 @@ +package com.baeldung.modelattribute; + +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement +public class Employee { + + private long id; + private String name; + private String contactNumber; + private String workingArea; + + public Employee() { + super(); + } + + public Employee(final long id, final String name, final String contactNumber, final String workingArea) { + this.id = id; + this.name = name; + this.contactNumber = contactNumber; + this.workingArea = workingArea; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + public long getId() { + return id; + } + + public void setId(final long id) { + this.id = id; + } + + public String getContactNumber() { + return contactNumber; + } + + public void setContactNumber(final String contactNumber) { + this.contactNumber = contactNumber; + } + + public String getWorkingArea() { + return workingArea; + } + + public void setWorkingArea(final String workingArea) { + this.workingArea = workingArea; + } + + @Override + public String toString() { + return "Employee [id=" + id + ", name=" + name + ", contactNumber=" + contactNumber + ", workingArea=" + workingArea + "]"; + } + +} diff --git a/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/modelattribute/EmployeeController.java b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/modelattribute/EmployeeController.java new file mode 100644 index 0000000000..7fc9f56dce --- /dev/null +++ b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/modelattribute/EmployeeController.java @@ -0,0 +1,58 @@ +package com.baeldung.modelattribute; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.ui.ModelMap; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.servlet.ModelAndView; + +import java.util.HashMap; +import java.util.Map; + +@Controller +public class EmployeeController { + + Map employeeMap = new HashMap<>(); + + @ModelAttribute("employees") + public void initEmployees() { + employeeMap.put(1L, new Employee(1L, "John", "223334411", "rh")); + employeeMap.put(2L, new Employee(2L, "Peter", "22001543", "informatics")); + employeeMap.put(3L, new Employee(3L, "Mike", "223334411", "admin")); + } + + @RequestMapping(value = "/employee", method = RequestMethod.GET) + public ModelAndView showForm() { + return new ModelAndView("employeeHome", "employee", new Employee()); + } + + @RequestMapping(value = "/employee/{Id}", produces = { "application/json", "application/xml" }, method = RequestMethod.GET) + public @ResponseBody Employee getEmployeeById(@PathVariable final Long Id) { + return employeeMap.get(Id); + } + + @RequestMapping(value = "/addEmployee", method = RequestMethod.POST) + public String submit(@ModelAttribute("employee") final Employee employee, final BindingResult result, final ModelMap model) { + if (result.hasErrors()) { + return "error"; + } + model.addAttribute("name", employee.getName()); + model.addAttribute("contactNumber", employee.getContactNumber()); + model.addAttribute("workingArea", employee.getWorkingArea()); + model.addAttribute("id", employee.getId()); + + employeeMap.put(employee.getId(), employee); + + return "employeeView"; + } + + @ModelAttribute + public void addAttributes(final Model model) { + model.addAttribute("msg", "Welcome to the Netherlands!"); + } +} diff --git a/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/responsestatus/Book.java b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/responsestatus/Book.java new file mode 100644 index 0000000000..8e603040dc --- /dev/null +++ b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/responsestatus/Book.java @@ -0,0 +1,42 @@ +package com.baeldung.responsestatus; + +public class Book { + + private int id; + private String author; + private String title; + + public Book() { + } + + public Book(int id, String author, String title) { + this.id = id; + this.author = author; + this.title = title; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + +} diff --git a/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/responsestatus/ResponseStatusRestController.java b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/responsestatus/ResponseStatusRestController.java new file mode 100644 index 0000000000..e3ace14b93 --- /dev/null +++ b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/responsestatus/ResponseStatusRestController.java @@ -0,0 +1,47 @@ +package com.baeldung.responsestatus; + +import java.util.concurrent.ThreadLocalRandom; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + + +@RestController +public class ResponseStatusRestController { + + @GetMapping("/teapot") + @ResponseStatus(HttpStatus.I_AM_A_TEAPOT) + public void teaPot() { + } + + @GetMapping("empty") + @ResponseStatus(HttpStatus.NO_CONTENT) + public void emptyResponse() { + } + + @GetMapping("empty-no-responsestatus") + public void emptyResponseWithoutResponseStatus() { + } + + @PostMapping("create") + @ResponseStatus(HttpStatus.CREATED) + public Book createEntity() { + // here we would create and persist an entity + int randomInt = ThreadLocalRandom.current() + .nextInt(1, 100); + Book entity = new Book(randomInt, "author" + randomInt, "title" + randomInt); + return entity; + } + + @PostMapping("create-no-responsestatus") + public Book createEntityWithoutResponseStatus() { + // here we would create and persist an entity + int randomInt = ThreadLocalRandom.current() + .nextInt(1, 100); + Book entity = new Book(randomInt, "author" + randomInt, "title" + randomInt); + return entity; + } +} diff --git a/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/spring/web/config/WebConfig.java b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/spring/web/config/WebConfig.java new file mode 100644 index 0000000000..f1a0e2d1a8 --- /dev/null +++ b/spring-web-modules/spring-mvc-basics-5/src/main/java/com/baeldung/spring/web/config/WebConfig.java @@ -0,0 +1,49 @@ +package com.baeldung.spring.web.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.resource.PathResourceResolver; +import org.springframework.web.servlet.view.BeanNameViewResolver; +import org.springframework.web.servlet.view.InternalResourceViewResolver; +import org.springframework.web.servlet.view.JstlView; + +@Configuration +public class WebConfig implements WebMvcConfigurer { + + @Override + public void addViewControllers(final ViewControllerRegistry registry) { + registry.addViewController("/") + .setViewName("index"); + } + + @Bean + public ViewResolver viewResolver() { + final InternalResourceViewResolver bean = new InternalResourceViewResolver(); + bean.setViewClass(JstlView.class); + bean.setPrefix("/WEB-INF/view/"); + bean.setSuffix(".jsp"); + bean.setOrder(2); + return bean; + } + + /** Static resource locations */ + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/resources/**/*") + .addResourceLocations("/", "/resources/") + .setCachePeriod(3600) + .resourceChain(true) + .addResolver(new PathResourceResolver()); + } + + @Bean + public BeanNameViewResolver beanNameViewResolver(){ + BeanNameViewResolver beanNameViewResolver = new BeanNameViewResolver(); + beanNameViewResolver.setOrder(1); + return beanNameViewResolver; + } +} \ No newline at end of file diff --git a/spring-web-modules/spring-mvc-basics-5/src/main/resources/application.properties b/spring-web-modules/spring-mvc-basics-5/src/main/resources/application.properties new file mode 100644 index 0000000000..935f91554b --- /dev/null +++ b/spring-web-modules/spring-mvc-basics-5/src/main/resources/application.properties @@ -0,0 +1 @@ +server.servlet.context-path=/spring-mvc-basics diff --git a/spring-web-modules/spring-mvc-basics-5/src/main/webapp/WEB-INF/view/employeeHome.jsp b/spring-web-modules/spring-mvc-basics-5/src/main/webapp/WEB-INF/view/employeeHome.jsp new file mode 100644 index 0000000000..fa5812faea --- /dev/null +++ b/spring-web-modules/spring-mvc-basics-5/src/main/webapp/WEB-INF/view/employeeHome.jsp @@ -0,0 +1,33 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> + + + +Form Example - Register an Employee + + +

Welcome, Enter The Employee Details

+ + + + + + + + + + + + + + + + + + +
Name
Id
Contact Number
+
+ + + + \ No newline at end of file diff --git a/spring-web-modules/spring-mvc-basics-5/src/main/webapp/WEB-INF/view/employeeView.jsp b/spring-web-modules/spring-mvc-basics-5/src/main/webapp/WEB-INF/view/employeeView.jsp new file mode 100644 index 0000000000..9a9b879a35 --- /dev/null +++ b/spring-web-modules/spring-mvc-basics-5/src/main/webapp/WEB-INF/view/employeeView.jsp @@ -0,0 +1,25 @@ +<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> + + +Spring MVC Form Handling + + + +

Submitted Employee Information

+

${msg}

+ + + + + + + + + + + + + +
Name :${name}
ID :${id}
Contact Number :${contactNumber}
+ + \ No newline at end of file diff --git a/spring-web-modules/spring-mvc-basics-5/src/main/webapp/WEB-INF/view/index.jsp b/spring-web-modules/spring-mvc-basics-5/src/main/webapp/WEB-INF/view/index.jsp new file mode 100644 index 0000000000..4f4eb0068d --- /dev/null +++ b/spring-web-modules/spring-mvc-basics-5/src/main/webapp/WEB-INF/view/index.jsp @@ -0,0 +1,7 @@ + + + + +

This is the body of the index view

+ + \ No newline at end of file diff --git a/spring-web-modules/spring-mvc-basics-5/src/main/webapp/WEB-INF/view/phoneHome.jsp b/spring-web-modules/spring-mvc-basics-5/src/main/webapp/WEB-INF/view/phoneHome.jsp new file mode 100644 index 0000000000..ebc9052639 --- /dev/null +++ b/spring-web-modules/spring-mvc-basics-5/src/main/webapp/WEB-INF/view/phoneHome.jsp @@ -0,0 +1,38 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> + + + + + Sample Form + + + + +
+ +

Phone Number

+
${message}
+ + + + + + +
+ + +
+
+ + diff --git a/spring-web-modules/spring-mvc-basics-5/src/main/webapp/WEB-INF/view/userHome.jsp b/spring-web-modules/spring-mvc-basics-5/src/main/webapp/WEB-INF/view/userHome.jsp new file mode 100644 index 0000000000..38c08d5a18 --- /dev/null +++ b/spring-web-modules/spring-mvc-basics-5/src/main/webapp/WEB-INF/view/userHome.jsp @@ -0,0 +1,55 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> + + + + + User Form + + + +
+ +

New User

+
${message}
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + +
+
+ + diff --git a/spring-web-modules/spring-mvc-basics-5/src/test/java/com/baeldung/SpringContextTest.java b/spring-web-modules/spring-mvc-basics-5/src/test/java/com/baeldung/SpringContextTest.java new file mode 100644 index 0000000000..f475e5d626 --- /dev/null +++ b/spring-web-modules/spring-mvc-basics-5/src/test/java/com/baeldung/SpringContextTest.java @@ -0,0 +1,13 @@ +package com.baeldung; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +public class SpringContextTest { + + @Test + public void whenSpringContextIsBootstrapped_thenNoExceptions() { + + } +} diff --git a/spring-web-modules/spring-mvc-basics/src/test/java/com/baeldung/web/controller/ClassValidationMvcIntegrationTest.java b/spring-web-modules/spring-mvc-basics-5/src/test/java/com/baeldung/customvalidator/ClassValidationMvcIntegrationTest.java similarity index 98% rename from spring-web-modules/spring-mvc-basics/src/test/java/com/baeldung/web/controller/ClassValidationMvcIntegrationTest.java rename to spring-web-modules/spring-mvc-basics-5/src/test/java/com/baeldung/customvalidator/ClassValidationMvcIntegrationTest.java index 24182f492a..f06c8d7002 100644 --- a/spring-web-modules/spring-mvc-basics/src/test/java/com/baeldung/web/controller/ClassValidationMvcIntegrationTest.java +++ b/spring-web-modules/spring-mvc-basics-5/src/test/java/com/baeldung/customvalidator/ClassValidationMvcIntegrationTest.java @@ -1,4 +1,4 @@ -package com.baeldung.web.controller; +package com.baeldung.customvalidator; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; diff --git a/spring-web-modules/spring-mvc-basics/src/test/java/com/baeldung/web/controller/CustomMVCValidatorIntegrationTest.java b/spring-web-modules/spring-mvc-basics-5/src/test/java/com/baeldung/customvalidator/CustomMVCValidatorIntegrationTest.java similarity index 97% rename from spring-web-modules/spring-mvc-basics/src/test/java/com/baeldung/web/controller/CustomMVCValidatorIntegrationTest.java rename to spring-web-modules/spring-mvc-basics-5/src/test/java/com/baeldung/customvalidator/CustomMVCValidatorIntegrationTest.java index 11b72e1650..104915f9ab 100644 --- a/spring-web-modules/spring-mvc-basics/src/test/java/com/baeldung/web/controller/CustomMVCValidatorIntegrationTest.java +++ b/spring-web-modules/spring-mvc-basics-5/src/test/java/com/baeldung/customvalidator/CustomMVCValidatorIntegrationTest.java @@ -1,10 +1,4 @@ -package com.baeldung.web.controller; - -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; +package com.baeldung.customvalidator; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -13,6 +7,12 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + public class CustomMVCValidatorIntegrationTest { private MockMvc mockMvc; diff --git a/spring-web-modules/spring-mvc-basics/src/test/java/com/baeldung/exception/HttpMediaTypeNotAcceptableExceptionControllerIntegrationTest.java b/spring-web-modules/spring-mvc-basics-5/src/test/java/com/baeldung/exception/HttpMediaTypeNotAcceptableExceptionControllerIntegrationTest.java similarity index 100% rename from spring-web-modules/spring-mvc-basics/src/test/java/com/baeldung/exception/HttpMediaTypeNotAcceptableExceptionControllerIntegrationTest.java rename to spring-web-modules/spring-mvc-basics-5/src/test/java/com/baeldung/exception/HttpMediaTypeNotAcceptableExceptionControllerIntegrationTest.java diff --git a/spring-web-modules/spring-mvc-basics/src/test/java/com/baeldung/web/controller/EmployeeControllerModelAttributeIntegrationTest.java b/spring-web-modules/spring-mvc-basics-5/src/test/java/com/baeldung/modelattribute/EmployeeControllerModelAttributeIntegrationTest.java similarity index 98% rename from spring-web-modules/spring-mvc-basics/src/test/java/com/baeldung/web/controller/EmployeeControllerModelAttributeIntegrationTest.java rename to spring-web-modules/spring-mvc-basics-5/src/test/java/com/baeldung/modelattribute/EmployeeControllerModelAttributeIntegrationTest.java index 5195adabd5..ffe2034a48 100644 --- a/spring-web-modules/spring-mvc-basics/src/test/java/com/baeldung/web/controller/EmployeeControllerModelAttributeIntegrationTest.java +++ b/spring-web-modules/spring-mvc-basics-5/src/test/java/com/baeldung/modelattribute/EmployeeControllerModelAttributeIntegrationTest.java @@ -1,4 +1,4 @@ -package com.baeldung.web.controller; +package com.baeldung.modelattribute; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; diff --git a/spring-web-modules/spring-mvc-basics-5/src/test/java/com/baeldung/responsestatus/ResponseStatusRestControllerIntegrationTest.java b/spring-web-modules/spring-mvc-basics-5/src/test/java/com/baeldung/responsestatus/ResponseStatusRestControllerIntegrationTest.java new file mode 100644 index 0000000000..cb5f1925bf --- /dev/null +++ b/spring-web-modules/spring-mvc-basics-5/src/test/java/com/baeldung/responsestatus/ResponseStatusRestControllerIntegrationTest.java @@ -0,0 +1,52 @@ +package com.baeldung.responsestatus; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +public class ResponseStatusRestControllerIntegrationTest { + + private MockMvc mockMvc; + + @BeforeEach + public void setup() { + this.mockMvc = MockMvcBuilders.standaloneSetup(new ResponseStatusRestController()) + .build(); + } + + @Test + public void whenTeapotEndpointCalled_thenTeapotResponseObtained() throws Exception { + this.mockMvc.perform(get("/teapot")) + .andExpect(status().isIAmATeapot()); + } + + @Test + public void whenEmptyNoContentEndpointCalled_thenNoContentResponseObtained() throws Exception { + this.mockMvc.perform(get("/empty")) + .andExpect(status().isNoContent()); + } + + @Test + public void whenEmptyWithoutResponseStatusEndpointCalled_then200ResponseObtained() throws Exception { + this.mockMvc.perform(get("/empty-no-responsestatus")) + .andExpect(status().isOk()); + } + + @Test + public void whenCreateWithCreatedEndpointCalled_thenCreatedResponseObtained() throws Exception { + this.mockMvc.perform(post("/create")) + .andExpect(status().isCreated()); + } + + @Test + public void whenCreateWithoutResponseStatusEndpointCalled_thenCreatedResponseObtained() throws Exception { + this.mockMvc.perform(post("/create-no-responsestatus")) + .andExpect(status().isOk()); + } + +} diff --git a/spring-web-modules/spring-mvc-basics-5/src/test/resources/logback-test.xml b/spring-web-modules/spring-mvc-basics-5/src/test/resources/logback-test.xml new file mode 100644 index 0000000000..8d4771e308 --- /dev/null +++ b/spring-web-modules/spring-mvc-basics-5/src/test/resources/logback-test.xml @@ -0,0 +1,12 @@ + + + + + [%d{ISO8601}]-[%thread] %-5level %logger - %msg%n + + + + + + + \ No newline at end of file diff --git a/spring-web-modules/spring-mvc-basics/README.md b/spring-web-modules/spring-mvc-basics/README.md index 49d23aef25..cce4eb4f59 100644 --- a/spring-web-modules/spring-mvc-basics/README.md +++ b/spring-web-modules/spring-mvc-basics/README.md @@ -14,8 +14,4 @@ The "REST With Spring" Classes: https://bit.ly/restwithspring - [Guide to Spring Handler Mappings](https://www.baeldung.com/spring-handler-mappings) - [Spring MVC Content Negotiation](https://www.baeldung.com/spring-mvc-content-negotiation-json-xml) - [Spring @RequestMapping New Shortcut Annotations](https://www.baeldung.com/spring-new-requestmapping-shortcuts) -- [Spring MVC Custom Validation](https://www.baeldung.com/spring-mvc-custom-validator) -- [Using Spring @ResponseStatus to Set HTTP Status Code](https://www.baeldung.com/spring-response-status) -- [Spring MVC and the @ModelAttribute Annotation](https://www.baeldung.com/spring-mvc-and-the-modelattribute-annotation) -- [The HttpMediaTypeNotAcceptableException in Spring MVC](https://www.baeldung.com/spring-httpmediatypenotacceptable) -- More articles: [[more -->]](/spring-mvc-basics-2) +- More articles: [[more -->]](../spring-mvc-basics-2) diff --git a/spring-web-modules/spring-mvc-basics/pom.xml b/spring-web-modules/spring-mvc-basics/pom.xml index a91f4004ff..ccd6773e6f 100644 --- a/spring-web-modules/spring-mvc-basics/pom.xml +++ b/spring-web-modules/spring-mvc-basics/pom.xml @@ -20,10 +20,6 @@ org.springframework.boot spring-boot-starter-web - - org.springframework.boot - spring-boot-starter-validation - commons-fileupload commons-fileupload