大家好,我是Spring Boot的新手。我正在尝试从前端向后端发送表单并将其显示在同一站点中。我怎样才能做到这一点?这是我的html。
<form action="/" method="post">
<input class="search" type="text">
<button onclick="sendData()">Send your data</button>
</form>```
I will display the data on the same site on localhost:8080.
对于后端,您必须定义一个控制器,它将接收来自前端的请求。
@RestController
public class controller_class{
@PostMapping("/")
public ResponseEntity<?> controller_name(@RequestBody String requestBody){
// logic goes here
return ResponseEntity.ok().build();
}
}
对于前端,您有很多选择。您可以使用React,Angular等。将请求发送到后端。
但是,在您的情况下,您似乎正在使用原始JavaScript,在这种情况下,您可以使用Ajax来调用后端。