BAEL-3307: Handling URL Encoded data in Form Submission in a Spring REST Service (#8255)
This commit is contained in:
parent
3685d331d9
commit
d7adefab3f
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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<String> getSuccess() {
|
||||||
|
return new ResponseEntity<String>("Thank you for submitting feedback.", HttpStatus.OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping(
|
||||||
|
path = "/feedback",
|
||||||
|
consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
|
||||||
|
public ResponseEntity<String> handleNonBrowserSubmissions(@RequestParam MultiValueMap paramMap) throws Exception {
|
||||||
|
// Save feedback data
|
||||||
|
return new ResponseEntity<String>("Thank you for submitting feedback", HttpStatus.OK);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,41 @@
|
||||||
|
<!DOCTYPE HTML>
|
||||||
|
<html xmlns:th="http://www.thymeleaf.org">
|
||||||
|
<head>
|
||||||
|
<title>Poetry Contest: Submission</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<form action="#" method="post" th:action="@{/web/feedback}" th:object="${feedback}">
|
||||||
|
|
||||||
|
<table border='0' cellpadding='2' cellspacing='2' width='480px'>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td align='center'>
|
||||||
|
<label for="email">Email:</label>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<input th:field=*{emailId} type="text"/>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td align='center'>
|
||||||
|
<label for="comment">Comment:</label>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<textarea cols="30" rows="30" th:field=*{comment}/>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td align='center'>
|
||||||
|
<input type="submit" value="Submit Feedback">
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
|
||||||
|
</table>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue