Reputation: 143
Here is my TeacherController
:
@Controller
@RequestMapping("/teacherController")
public class TeacherController {
private static final String TEACHER_MODEL = "teacher";
@Autowired
TeacherService teacherService;
@RequestMapping("check")
public ModelAndView index() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("teachers", teacherService.getAll());
modelAndView.setViewName("/teacherViews/teachersPage");
return modelAndView;
}
And here page where I print my table :
<table class="table table-dark" border="1" width="100%" cellpadding="5">
<thead>
<tr>
<th>Teacher ID</th>
<th>Teacher Name</th>
<th>Teacher Position</th>
</tr>
</thead>
<tbody id="teacherBody">
<tr th:each="teacher : ${teachers}">
<td th:text="${teacher.teacherId}" />
<td th:text="${teacher.teacherName}" />
<td th:text="${teacher.position}" />
</tr>
</tbody>
And all that I wrote above I need to rewrite using RestController and AJAX.
I want that table data loads with loading page automatically, as I understand, It can works with js
.
So, my problem is that I need to rewrite my Controller to RestController with AJAX but I don't understand how to do it.
Thanks in advance for your help!!!
Upvotes: 0
Views: 1176
Reputation: 1
Difference between @Controller and @Rest Controller
@Controller
The @Controller annotation has been part of the framework for a very long time.@Controller annotation designates that the annotated class is a controller. It is a specialization of @Controller and is autodetected through classpath scanning. It is typically utilized in amalgamation with annotated handler methods predicated on the @RequestMapping annotation.
For example,
@Controller
@RequestMapping("users")
public class HomeController {
@GetMapping(produces = "application/json")
public @ResponseBody User getAllUsers() {
return findAllUsers();
}
}
The request handling method is annotated with @ResponseBody. This annotation enables automatic serialization of the return object into the HttpResponse.
@RestController
The @RestController annotation was introduced in Spring 4.0 to simplify the engenderment of RESTful web services. It's an convenience annotation that combines @Controller and @ResponseBody
For example,
@RestController
@RequestMapping("users")
public class HomeController {
@GetMapping(produces = "application/json")
public Book getAllUsers() {
return findAllUsers();
}
}
The controller is annotated with the @RestController annotation, consequently the @ResponseBody isn't required. Every request handling method of the controller class automatically serializes return objects into HttpResponse.
After conversion
RestController
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/v1")
public class TeacherController {
@Autowired
TeacherService teacherService;
@GetMapping("/teacher")
public List<Teacher> fetchAllTeacher() {
return teacherService.getAll();
}
}
HTML
<table id="table-content" border='1'>
<tr>
<th>Teacher ID</th>
<th>Teacher Name</th>
<th>Teacher Position</th>
</tr>
Javascript(Not tested)
var service = 'http://localhost/api/v1/';
$(document).ready(function(){
jQuery.support.cors = true;
$.ajax(
{
type: "GET",
url: service + '/teacher/',
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
cache: false,
success: function (data) {
var trHTML = '';
$.each(data, function (i, item) {
trHTML += '<tr><td>' + data.Teacher[i].Name + '</td><td>' + data.Teacher[i].Position + '</td></tr>';
});
$('#table-content').append(trHTML);
},
error: function (msg) {
alert(msg.responseText);
}
});
})
Upvotes: 1