在前端,我们定义了一个``元素,使用`type='number'`来指定它的类型为数字。用户输入的数字可以通过`document.getElementById('inputNumber').value`获取到。

在后端,我们使用`@PostMapping`注解来指定接收POST请求的URL路径为`/receiveNumber`。`@RequestBody`注解用于指定请求体中的数据将被绑定到`number`参数上,而`number`参数的类型为`Integer`,这样就能直接接收到前端发送的数字数据了。根据实际需求,你可以在`receiveNumber`方法中进行进一步的处理。

前端代码示例:

<input type='number' id='inputNumber' />

<button onclick='sendData()'>提交</button>

<script>
    function sendData() {
        var numberValue = document.getElementById('inputNumber').value;
        
        // 发送数据给后端
        // ...
    }
</script>

后端Spring代码示例:

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class NumberController {

    @PostMapping("/receiveNumber")
    public void receiveNumber(@RequestBody Integer number) {
        // 处理接收到的数字
        // ...
    }
}
前端 input 类型为 number,Spring 后端如何接收数据

原文地址: http://www.cveoy.top/t/topic/bY6C 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录