1 year ago
#342271
user6631554
How to pass variable value from web form to another web page via controller class?
Main class:
package com.example.handlingformsubmission;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HandlingFormSubmissionApplication {
public static void main(String[] args) {
SpringApplication.run(HandlingFormSubmissionApplication.class, args);
}
}
Controller:(here in Post mapping something wrong with code which is supposed to pass values of variable to another page, but it doesn't):
package com.example.handlingformsubmission;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
public class GreetingController {
@GetMapping("/z.html")
public String greetingForm(Model model) {
model.addAttribute("z.html", new Greeting());
return "z.html";
}
@GetMapping("/zu.html")
public String greetingForm2(Model model) {
model.addAttribute("zu.html", new Greeting());
return "zu.html";
}
@GetMapping("/zus.html")
public String greetingForm3(Model model) {
model.addAttribute("zus.html", new Greeting());
return "zus.html";
}
@GetMapping("/zusu.html")
public String greetingForm4(Model model) {
model.addAttribute("zusu.html", new Greeting());
return "zusu.html";
}
@PostMapping("/zusu_generated.html")
public String greetingSubmit(@ModelAttribute Greeting greeting, Model model) {
model.addAttribute("zusu_generated.html",greeting);
model.addAttribute("Unit", greeting.getUnit());
model.addAttribute("Restarttype", greeting.getRestarttype());
return "zusu_generated.html";
}
}
Class where variables are declared (and getters and setters are configured):
package com.example.handlingformsubmission;
public class Greeting {
public String Restarttype;
public String Unit;
public String getRestarttype() {
return Restarttype;
}
public void setRestarttype(String Restarttype) {
this.Restarttype = Restarttype;
}
public String getUnit() {
return Unit;
}
public void setUNIT(String Unit) {
this.Unit = Unit;
}
}
HTML web page where web form accepts values of variable:
<!DOCTYPE HTML>
<html xmlns:th="https://www.thymeleaf.org" xmlns:form="http://www.w3.org/1999/xhtml">
<head>
<title>Getting Started: Handling Form Submission</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="css.css" />
<style type="text/css">
.block1 {
width: 20px;
background: #ffffff;
padding: 5px;
padding-right: 20px;
border: solid 1px white;
float: left;
margin-top: 17%; /* Отступ сверху */
}
.block2 {
width: 1360px;
background: #ffffff;
padding: 5px;
padding-right: 20px;
border: solid 1px black;
float: left;
margin-top: 17%; /* Отступ сверху */
text-align:center;
}
.block3 {
width: 20px;
background: #ffffff;
padding: 5px;
padding-right: 20px;
border: solid 1px white;
float: left;
margin-top: 17%; /* Отступ сверху */
}
a.hover: underline(none)
</style>
</head>
<body>
<div class="row">
<div class="block1"></div>
<div class="block2">
<h1>Nokia BSC3i ZUSU command</h1>
<form action="#" th:action="@{/zusu_generated.html}" th:object="${greeting}" method="post">
<table align = center>
<tr>
<td> ZUSU:</td>
<td>
<label for="Unit">Unit:</label>
<select name="Unit" id="Unit">
<optgroup label="Unit">
<option value="BCSU">BCSU</option>
<option value="Clock">Clock</option>
<option value="ET">ET</option>
</optgroup>
</select>
</td>
<td>
C
</td>
<td> = </td>
<td>
<label for="Restart value"> </label>
<select name="Restart type" id="Restart value">
<optgroup label="Restart type">
<option value="TOT">TOT</option>
<option value="HALF">HALF</option>
<option value="PARTIAL">PARTIAL</option>
</optgroup>
</select>
</td>
</tr>
</table>
<p><input type="submit" value="Generate command" /> <input type="reset" value="Reset"
/></p>
</form>
</div>
<div class="block3"></div>
</div>
</body>
</html>
HTML web page where variable values should appear, but they don't appear:
<!DOCTYPE HTML>
<html xmlns:th="https://www.thymeleaf.org" xmlns:form="http://www.w3.org/1999/xhtml">
<head>
<title>Getting Started: Handling Form Submission</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="css.css" />
<style type="text/css">
.block1 {
width: 20px;
background: #ffffff;
padding: 5px;
padding-right: 20px;
border: solid 1px white;
float: left;
margin-top: 17%; /* Отступ сверху */
}
.block2 {
width: 1360px;
background: #ffffff;
padding: 5px;
padding-right: 20px;
border: solid 1px black;
float: left;
margin-top: 17%; /* Отступ сверху */
text-align:center;
}
.block3 {
width: 20px;
background: #ffffff;
padding: 5px;
padding-right: 20px;
border: solid 1px white;
float: left;
margin-top: 17%; /* Отступ сверху */
}
a.hover: underline(none)
</style>
</head>
<body>
<div class="row">
<div class="block1"></div>
<div class="block2">
<h1>Nokia BSC3i ZUSU command</h1>
<form action="#" th:action="@{/greeting}" th:object="${greeting}" method="post">
<table align = center>
<tr>
<td>ZUSU:</td>
<td>
<label for="Unit">Unit:</label>
<select name="Unit" id="Unit">
<optgroup label="Unit">
<option value="BCSU">BCSU</option>
<option value="Clock">Clock</option>
<option value="ET">ET</option>
</optgroup>
</select>
</td>
<td>
C
</td>
<td> = </td>
<td>
<label for="Restart value"> </label>
<select name="Restart type" id="Restart value">
<optgroup label="Restart type">
<option value="TOT">TOT</option>
<option value="HALF">HALF</option>
<option value="PARTIAL">PARTIAL</option>
</optgroup>
</select>
</td>
</tr>
<tr>
<td>
<p th:text="'Generated command is: ZUSU: UNIT = ' + ${greeting.Unit} + ' C = ' + ${greeting.Restarttype}" />
</td>
</tr>
</table>
<p><input type="submit" value="Copy to memory" /> <input type="reset" value="Reset" />
</p>
</form>
</div>
<div class="block3"></div>
</div>
</body>
</html>
java
spring
getter-setter
0 Answers
Your Answer