我如何使用Spring Boot从主页发送发帖请求并对其进行操作?

问题描述 投票:0回答:1

大家好,我是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.


java html spring backend boot
1个回答
0
投票

对于后端,您必须定义一个控制器,它将接收来自前端的请求。

@RestController
public class controller_class{

    @PostMapping("/")
    public ResponseEntity<?> controller_name(@RequestBody String requestBody){
        // logic goes here
        return ResponseEntity.ok().build();
    }
}

对于前端,您有很多选择。您可以使用React,Angular等。将请求发送到后端。

但是,在您的情况下,您似乎正在使用原始JavaScript,在这种情况下,您可以使用Ajax来调用后端。

© www.soinside.com 2019 - 2024. All rights reserved.