Cześć, pogubiłem się. Sprawa wygląda tak:
Formularz ludzi (wyświetla dane), możliwe akcje: usuń (wyrzuca prompt czy aby na pewno chcę usunąć) - i edycja danych.
Problem następujący --> działa jak należy gdy mam samo (localhost:9999/all-customers) ale jak dodam do ścieżki (localhost:9999/test/all-customers) to zwraca tylko JSONa w przypadku Edycji, w przypadku Delete usuwa po ID ale nie wyrzuca prompt czyli nie działa "łączenie" warstw
Wychodzi na to, że model nie konsumuje mojego uri (źle odnoszę się do kodu javy w js'ie?)
kod JS wygląda tak: //
$(document).ready(function () {
$('.nBtn, .table .eBtn').on('click', function (event) {
event.preventDefault();
var href = $(this).attr('href');
var text = $(this).text();
if (text === 'Edit') {
$.get(href, function (customer, status) {
$('.myForm #id').val(customer.id);
$('.myForm #firstName').val(customer.firstName);
$('.myForm #lastName').val(customer.lastName);
$('.myForm #age').val(customer.age);
});
$('.myForm #exampleModal').modal();
} else {
$('.myForm #id').val('');
$('.myForm #firstName').val('');
$('.myForm #lastName').val('');
$('.myForm #age').val('');
$('.myForm #exampleModal').modal();
}
});
$('.table .deleteBtn').on('click', function (event) {
event.preventDefault();
var href = $(this).attr('href');
$('#exampleModalLong #delRef').attr('href', href);
$('#exampleModalLong').modal();
});
});
@Controller
public class CustomerController {
private CustomerRepository customerRepository;
public CustomerController(CustomerRepository customerRepository) {
this.customerRepository = customerRepository;
}
@GetMapping("/test/all-customers")
public String showPage(Model model, @RequestParam(defaultValue = "0") int page) {
model.addAttribute("data", customerRepository.findAll(PageRequest.of(page, 10, Sort.unsorted())));
model.addAttribute("currentPage", page);
return "index";
}
@PostMapping("/save")
public String save(Customer customer) {
customerRepository.save(customer);
return "redirect:/test/all-customers";
}
@GetMapping("/test/delete")
public String deleteCustomer(Long id) {
customerRepository.deleteById(id);
return "redirect:/test/all-customers";
}
@GetMapping("/test/findOne")
@ResponseBody
public Customer findOne(Long id) {
return customerRepository.findById(id).get();
}
}
+html odpowiedzialny za te dwa przyciski
<a th:href="@{findOne/(id=${customer.id})}" class="btn btn-primary eBtn">Edit</a>
<a th:href="@{delete/(id=${customer.id})}" class="btn btn-danger deleteBtn">Delete</a>
Pomóżcie proszę:D
i raz jeszcze, jak nie ma "test" w adresach URL to działa