I have the following method in my controller that retrieves data from a query that I have in other methods
@RequestMapping(value= "/obtenerDatos")
public void obtener(HttpServletRequest, HttpServletResponse, Model model){
Object attribute = (Object) request.getSession().getAttribute("listDatos");
List<Factura> factura = (List<Factura>)attribute;
Object attribute2 = (Object) request.getSession().getAttribute("listDatos1");
List<Cliente> cliente = (List<Cliente>)attribute2;
model.addAtribute("listDatos",factura.get(0).getValor());
model.addAtribute("listDatos1",cliente.get(0).getValor());
}
I have in my jsp1 a modal with a button, which sends to call the following function:
function obtenerDatos(){
$.ajax({
type: "POST",
content-type: "applicaction/json",
url: "${pageContext.servletContext.contextPath}/obtenerDatos"
error: function (e){
console.log("ERROR", e);
},
done: function (e){
console.log("DONE", e);
}
});
}
<button type="button" onclick="obtenerDatos()"></button>
Then at the time of clicking on the button, enter my method of my controller and another jsp2, retrieve that data as follows and perform an operation
<input type="hidden" id="valor1" name="valor1" value="${factura.get(0).getValor()}">
<input type="hidden" id="valor2" name="valor2" value="${cliente.get(0).getValor()}">
function sumar(){
var num1 = parseInt($(#valor1).val());
var num2 = parseInt($(#valor2).val());
var total = num1 + num2;
document.getElementById("inputTotal").value=total;
}
I have another jsp that is the one that contains the menu and this is where I call jsp1,
$(document.ready(function(){
iniciaModal();
})
function modal(){
$("#idModal" ).modal("show");
}
< li id=" 1"><a href="#" onclick =" modal()">MODAL</a></li>
Y aquí incluyo el jsp1
<%@include file="../jsp1.jsp" %>
And in the jsp1
function iniciaModal(){
}
<div class="idModal" class="modal-fade">
Aquí tengo el botón que menciono
</div>
and if you paint the result of the sum in my input, but do it until you reload the page, that is, in the first jsp I have the modal with the button that I mentioned, and when I click it recovers the controller data and he retrieves them in another jsp to show the operation, and what I want him to do is that by clicking on the button and closing the modal, the result already appears, and that he does not have to reload the page to see the result
How can I do it? How is ajax implemented to do this action that I want ?, or how do I submit to jsp2, with the button I have in my modal of jsp1?