Skip to content

Commit 680d71a

Browse files
tapankavasthisjmillington
authored andcommitted
BAEL-3307: Handling URL Encoded data in Form Submission in a Spring REST Service (eugenp#8255)
1 parent 36c2642 commit 680d71a

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.form_submission;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
6+
7+
@SpringBootApplication
8+
public class Application extends SpringBootServletInitializer {
9+
10+
public static void main(String[] args) {
11+
SpringApplication.run(Application.class, args);
12+
}
13+
14+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.baeldung.form_submission.controllers;
2+
3+
import com.baeldung.form_submission.model.Feedback;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.http.MediaType;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.ui.Model;
9+
import org.springframework.util.MultiValueMap;
10+
import org.springframework.web.bind.annotation.*;
11+
12+
@Controller
13+
public class FeedbackForm {
14+
15+
@GetMapping(path = "/feedback")
16+
public String getFeedbackForm(Model model) {
17+
Feedback feedback = new Feedback();
18+
model.addAttribute("feedback", feedback);
19+
return "feedback";
20+
}
21+
22+
@PostMapping(
23+
path = "/web/feedback",
24+
consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
25+
public String handleBrowserSubmissions(Feedback feedback) throws Exception {
26+
// Save feedback data
27+
return "redirect:/feedback/success";
28+
}
29+
30+
@GetMapping("/feedback/success")
31+
public ResponseEntity<String> getSuccess() {
32+
return new ResponseEntity<String>("Thank you for submitting feedback.", HttpStatus.OK);
33+
}
34+
35+
@PostMapping(
36+
path = "/feedback",
37+
consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
38+
public ResponseEntity<String> handleNonBrowserSubmissions(@RequestParam MultiValueMap paramMap) throws Exception {
39+
// Save feedback data
40+
return new ResponseEntity<String>("Thank you for submitting feedback", HttpStatus.OK);
41+
}
42+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.form_submission.model;
2+
3+
public class Feedback {
4+
private String emailId;
5+
private String comment;
6+
7+
public String getEmailId() {
8+
return this.emailId;
9+
}
10+
11+
public void setEmailId(String emailId) {
12+
this.emailId = emailId;
13+
}
14+
15+
public String getComment() {
16+
return this.comment;
17+
}
18+
19+
public void setComment(String comment) {
20+
this.comment = comment;
21+
}
22+
23+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE HTML>
2+
<html xmlns:th="http://www.thymeleaf.org">
3+
<head>
4+
<title>Poetry Contest: Submission</title>
5+
</head>
6+
<body>
7+
8+
<form action="#" method="post" th:action="@{/web/feedback}" th:object="${feedback}">
9+
10+
<table border='0' cellpadding='2' cellspacing='2' width='480px'>
11+
12+
<tr>
13+
<td align='center'>
14+
<label for="email">Email:</label>
15+
</td>
16+
<td>
17+
<input th:field=*{emailId} type="text"/>
18+
</td>
19+
</tr>
20+
21+
<tr>
22+
<td align='center'>
23+
<label for="comment">Comment:</label>
24+
</td>
25+
<td>
26+
<textarea cols="30" rows="30" th:field=*{comment}/>
27+
</td>
28+
</tr>
29+
30+
<tr>
31+
<td align='center'>
32+
<input type="submit" value="Submit Feedback">
33+
</td>
34+
</tr>
35+
36+
37+
</table>
38+
</form>
39+
40+
</body>
41+
</html>

0 commit comments

Comments
 (0)