I have a spring boot controller which returns a view and i wanted to change it by consuming an ajax endpoint, but at the same time get the values from the form with a modelAttribute and then send a list or multiple lists on the page and iterate through those lists with thymeleaf.Is it possible? Here is the controller:
@RequestMapping(value="/search", method=RequestMethod.POST)
@ResponseBody
public String search(@ModelAttribute("specification") Specification specification, Model model) {
List<SearchResultAutovit> list;
list = scrapper.searchMethod(specification.getPrice,specification.getModel);
if (list == null || list.isEmpty()) {
model.addAttribute("msg","Something");
} else {
model.addAttribute("listaAutovit", list);
}
return "?";
}
Ajax request:
$(".btn.btn-danger").on('click', {function fire_ajax_submit() {
var str = $(".form-inline.justify-content-center").serialize();
$.ajax({
type:"post",
data:str,
url:"/search",
async: false,
dataType: "json",
success: function(){
alert("success");
}
});
}
I don't want to manipulate the page from the ajax succes part because i'm already doing that with thymeleaf when the model is sent to the page.