Skip to content

Commit 741f66d

Browse files
committed
Adding Data to the Spring Model.
1 parent d4c00c6 commit 741f66d

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

src/com/luv2code/springdemo/mvc/HelloWorldController.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.luv2code.springdemo.mvc;
22

33
import org.springframework.stereotype.Controller;
4+
import org.springframework.ui.Model;
45
import org.springframework.web.bind.annotation.RequestMapping;
6+
import javax.servlet.http.HttpServletRequest;
57

68
@Controller
79
public class HelloWorldController {
@@ -19,4 +21,24 @@ public String showForm() {
1921
public String processForm() {
2022
return "helloworld";
2123
}
24+
25+
// new a controller method to read form data and add data to the model
26+
27+
@RequestMapping("/processFormVersionTwo")
28+
public String letsShoutDude(HttpServletRequest request, Model model) {
29+
30+
// read the request parameter from the HTML form
31+
String name = request.getParameter("studentName");
32+
33+
// convert the data to all caps
34+
name = name.toUpperCase();
35+
36+
// create the message
37+
String result = "Yo! " + name;
38+
39+
// add message to the model
40+
model.addAttribute("message", result);
41+
42+
return "helloworld";
43+
}
2244
}

web/WEB-INF/view/helloworld-form.jsp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<title>Hello World - Input Form</title>
55
</head>
66
<body>
7-
<form action="processForm" method="GET">
7+
<form action="processFormVersionTwo" method="GET">
88

99
<input type="text" name="studentName" placeholder="What's your name?" />
1010
<input type="submit" />

web/WEB-INF/view/helloworld.jsp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44
<body>
55

66
Hello World of Spring!
7+
78
<br><br>
9+
810
Student name: ${param.studentName}
911

12+
<br><br>
13+
14+
The message: ${message}
15+
1016
</body>
1117

1218
</html>

0 commit comments

Comments
 (0)