Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
How to get the values from ajax to spring rest api controller

I am trying to get data from ajax to my spring rest API controller.

I have this image attached below. I am trying to delete or edit the data that the user selects.

enter image description here

JS file

    function submit(){
    if(document.getElementById('deleteButton').clicked == true)
    {
    var button="delete";
    }

    if(document.getElementById('editButton').clicked == true)
    {
    var button="edit";
    }

    var check = document.getElementsByName('check');
                var selected = new Array();
                for (var i=0; i<check.length; i++) {
                    if (check[i].checked) {
                        selected.push(check[i].value);
                    }
                }

                    $.ajax({
                        url:"/submit",
                        type:"POST",
                        contentType: "application/json",
                        dataType:'json',
                        data: {selected:selected,button:button},
                        success:function(data){
                          alert("SUCCESS");
                        },
                    });

    }

JSP File

    <h1 align="center">My Employees</h1>
<table border="1" cellpadding="10">
    <tr>
        <th></th>
        <th>Employee Number</th>
        <th>Name</th>
        <th>Address</th>
        <th>Phone Number</th>
    </tr>
    <c:forEach items="${emp}" var="emp">
        <tr>
            <td><input type="checkbox" name="check"/></td>  
            <td>${emp.id }</td>
            <td>${emp.name }</td>
            <td>${emp.address }</td>
            <td>${emp.phoneNo }</td>
        </tr>
    </c:forEach>
</table>

<button onclick="submit()" id="deleteButton" value="delete">Delete</button>
<button onclick="submit()" id="editButton" value="edit">Edit</button>

I am using ajax and js to know which button is clicked and then trying to send the values to the controller. But I don't know how to use it because I am a newbie in the front end.

All the help is appreciated, I am ready to provide all the details.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Giving a specific answer to your question is a pretty tall order. Since you mention Spring in the title, I'm assuming your back end is some sort of web app container that supports Spring and Java handlers.

The Java code you need to write would typically use some annotations to identify the controller class that will receive the Ajax requests, do whatever processing is necessary, and return a response body that the JavaScript can then interpret and display a result to the user.

Rather than try to guess what you would need, let me point you to Mkyong's Spring 4 MVC Ajax Hello World Example and Spring Boot Ajax example. Once you get into this, you will likely find you need examples of how to do specific things. You can find lots of examples at Mkyong's site. I also recommend the Baeldung.com tutorials on REST with Spring. There a several "hello world" examples to get you started plus specific tutorials on the many variations of how to make requests and receive responses.

Handling Ajax requests is very commonplace now and Spring has a lot of built-in handling of parsing incoming JSON and marshaling outgoing JSON back to the browser. You can still do it yourself and perhaps the first time you should, but I wouldn't fight what you more or less get for free.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!