如何从 JavaScript 获取数组到 JavaController?

How to get array from JavaScript to JavaController?

我正在保存用户在书写时按住按钮多长时间的数据。现在数据保存在 const holdingTimes,但我想在 html 中添加任何按钮,例如 SAVE,然后将数据从 js 发送到我的 JavaController。

html:

<div class="container">
    <p>Write some text:</p>
    <form>
        <div class="form-group">
            <textarea class="form-control" rows="5" id="myInput"></textarea>
        </div>
    </form>
</div>

js:

<script>
    var pressedTime; var releasedTime; var holdingTime; 
    const holdingTimes = [];
 
    document.getElementById("myInput").addEventListener("keydown", pressedFunction);
    document.getElementById("myInput").addEventListener("keyup", releasedFunction);

    function pressedFunction() {
        pressedTime = Date.now();
    }

    function releasedFunction() {
        releasedTime = Date.now() ;
        holdingTime = releasedTime - pressedTime;
      
        holdingTimes.push(holdingTime);
    }
</script>

对如何构建 JavaController 和 SAVE 按钮有什么想法吗?

JavaController 看起来像:

@Controller
@Scope(WebApplicationContext.SCOPE_SESSION)
@RequestMapping("/getValues")

public class JavaController {

int [] holdingTimes;

@RequestMapping("/save")
    public String getData(int[] holdingTimesFromJs, Model model) {
        
        holdingTimes = holdingTimesFromJs;        

        return "redirect:/";
    }
}
On click of button, call a Javascript function which will send an Ajax POST request to the Controller.
                
JavaScript Code:

const holdingTimes = [];
holdingTimes.push(10);
holdingTimes.push(20);
holdingTimes.push(30);
            
$.ajax({
    'url' : "/test-url",
    "data" : JSON.stringify(holdingTimes),
    'method' : "POST",
    'contentType' : 'application/json'
}).done(function(data) {
    // handling code for success response
});
            
            
Spring Controller:    

@PostMapping("/test-url")
public String testArrayUpload(@RequestBody int[] times) {
    //your logic goes here
    return "test";
}