From d7adefab3fe28c656237764f09914b8230552300 Mon Sep 17 00:00:00 2001 From: Tapan Avasthi Date: Sun, 1 Dec 2019 22:02:29 +0530 Subject: [PATCH] BAEL-3307: Handling URL Encoded data in Form Submission in a Spring REST Service (#8255) --- .../baeldung/form_submission/Application.java | 14 +++++++ .../controllers/FeedbackForm.java | 42 +++++++++++++++++++ .../form_submission/model/Feedback.java | 23 ++++++++++ .../main/resources/templates/feedback.html | 41 ++++++++++++++++++ 4 files changed, 120 insertions(+) create mode 100644 spring-mvc-simple-2/src/main/java/com/baeldung/form_submission/Application.java create mode 100644 spring-mvc-simple-2/src/main/java/com/baeldung/form_submission/controllers/FeedbackForm.java create mode 100644 spring-mvc-simple-2/src/main/java/com/baeldung/form_submission/model/Feedback.java create mode 100644 spring-mvc-simple-2/src/main/resources/templates/feedback.html diff --git a/spring-mvc-simple-2/src/main/java/com/baeldung/form_submission/Application.java b/spring-mvc-simple-2/src/main/java/com/baeldung/form_submission/Application.java new file mode 100644 index 0000000000..34c14141b0 --- /dev/null +++ b/spring-mvc-simple-2/src/main/java/com/baeldung/form_submission/Application.java @@ -0,0 +1,14 @@ +package com.baeldung.form_submission; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; + +@SpringBootApplication +public class Application extends SpringBootServletInitializer { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/spring-mvc-simple-2/src/main/java/com/baeldung/form_submission/controllers/FeedbackForm.java b/spring-mvc-simple-2/src/main/java/com/baeldung/form_submission/controllers/FeedbackForm.java new file mode 100644 index 0000000000..791fc75cef --- /dev/null +++ b/spring-mvc-simple-2/src/main/java/com/baeldung/form_submission/controllers/FeedbackForm.java @@ -0,0 +1,42 @@ +package com.baeldung.form_submission.controllers; + +import com.baeldung.form_submission.model.Feedback; +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.util.MultiValueMap; +import org.springframework.web.bind.annotation.*; + +@Controller +public class FeedbackForm { + + @GetMapping(path = "/feedback") + public String getFeedbackForm(Model model) { + Feedback feedback = new Feedback(); + model.addAttribute("feedback", feedback); + return "feedback"; + } + + @PostMapping( + path = "/web/feedback", + consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE}) + public String handleBrowserSubmissions(Feedback feedback) throws Exception { + // Save feedback data + return "redirect:/feedback/success"; + } + + @GetMapping("/feedback/success") + public ResponseEntity getSuccess() { + return new ResponseEntity("Thank you for submitting feedback.", HttpStatus.OK); + } + + @PostMapping( + path = "/feedback", + consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE}) + public ResponseEntity handleNonBrowserSubmissions(@RequestParam MultiValueMap paramMap) throws Exception { + // Save feedback data + return new ResponseEntity("Thank you for submitting feedback", HttpStatus.OK); + } +} \ No newline at end of file diff --git a/spring-mvc-simple-2/src/main/java/com/baeldung/form_submission/model/Feedback.java b/spring-mvc-simple-2/src/main/java/com/baeldung/form_submission/model/Feedback.java new file mode 100644 index 0000000000..f8d416460c --- /dev/null +++ b/spring-mvc-simple-2/src/main/java/com/baeldung/form_submission/model/Feedback.java @@ -0,0 +1,23 @@ +package com.baeldung.form_submission.model; + +public class Feedback { + private String emailId; + private String comment; + + public String getEmailId() { + return this.emailId; + } + + public void setEmailId(String emailId) { + this.emailId = emailId; + } + + public String getComment() { + return this.comment; + } + + public void setComment(String comment) { + this.comment = comment; + } + +} diff --git a/spring-mvc-simple-2/src/main/resources/templates/feedback.html b/spring-mvc-simple-2/src/main/resources/templates/feedback.html new file mode 100644 index 0000000000..4b6a487fc2 --- /dev/null +++ b/spring-mvc-simple-2/src/main/resources/templates/feedback.html @@ -0,0 +1,41 @@ + + + + Poetry Contest: Submission + + + +
+ + + + + + + + + + +
+ + + +
+ + +