1
My project is a Struts application, and my problem is that I’m not getting the return of the back-end method for the ajax method in the graphical interface, and I’m needing the modal form to return two methods. In modal there is the method cadastrarJustificativa
to register when the form is submitted.
<form class="form-horizontal" role="form" id="formCadastrarJustificativa" action="AdministracaoRemessa!cadastrarJustificativa" method="post">
And in this piece of code also in the same form.
<div class="modal-footer">
<button type="button" class="btn btn-primary" onclick="cadastrarJustificativa()">Enviar</button>
</div>
This modal is sending a request for a back-end method called cadastrarJustificativa
and the back-end method receives data from the ajax request called cadastrarJustificativa()
.
In this form there are several validations as you can see below:
function cadastrarJustificativa() {
var dateFormat = 'DD-MM-YYYY';
var data = new Date();
// var anoAtual = data.getFullYear();
$('#msgModalJaExisteRemessa').modal('show');
var anoInicial = $('#idPeriodoInicio').val().split("/");
var anoFim = $('#idPeriodoFim').val().split("/");
$('#existeRemessa').val();
var $msgErro = $('#msgErro');
if($('#idUJ').val() === ""){
$msgErro.text("O UJ é obrigatorio")
$("#modalAlert").removeClass("hidden");
}
else if($('#idPeriodoInicio').val() === ""){
$msgErro.text("O Período inicial é obrigatório");
$("#modalAlert").removeClass("hidden");
}
else if($('#idPeriodoFim').val() === ""){
$msgErro.text("O Período fim é obrigatorio");
$("#modalAlert").removeClass("hidden");
}
else if ($('#idMotivo').val() === "") {
$msgErro.text("O motivo é obrigatório")
$("#modalAlert").removeClass("hidden");
}
else if(anoInicial[1] > anoFim[1] || anoInicial[0] > anoFim[0]){
$msgErro.text("Data Início não pode ser maior que a Data FIM e não pode ser uma data futura")
$("#modalAlert").removeClass("hidden");
}
else if (!moment(moment(anoInicial[1]+"/"+anoInicial[0]).format(dateFormat),dateFormat,true).isValid()) {
$msgErro.text("A data de início não é válida");
$("#modalAlert").removeClass("hidden");
}
else if(!moment(moment( anoFim[1]+"/"+anoFim[0]).format(dateFormat),dateFormat,true).isValid()) {
$msgErro.text("A data fim não é válida");
$("#modalAlert").removeClass("hidden");
}
else {
dataInicio = $('#idPeriodoInicio').val();
dataFim = $('#idPeriodoFim').val();
uj = $('#idUJ').val();
$.get("AdministracaoRemessa!verificadorRemessaExistente", 'periodoInicio='+ dataInicio + '&periodoFim=' + dataFim + '&ujRemessa=' + uj, function(data){
alert("o que existe dentro de data" + data);
});
// $('#formCadastrarJustificativa').submit();
}
}
The idea is it goes through all the validations, and one of them is to check whether the date that is being registered already exists in the database, so that it can make a query in the database before submitting the form. I had the idea to use the ajax request below:
dataInicio = $('#idPeriodoInicio').val();
dataFim = $('#idPeriodoFim').val();
uj = $('#idUJ').val();
$.get("AdministracaoRemessa!verificadorRemessaExistente", 'periodoInicio='+ dataInicio + '&periodoFim=' + dataFim + '&ujRemessa=' + uj, function(data){
alert("o que existe dentro de data" + data);
});
This request can send the information peacefully to the called back-end method verificadorRemessaExistente
:
public String verificadorRemessaExistente() {
String verificadorRemessa;
UsuarioLogadoExterno usuarioLogado =
(UsuarioLogadoExterno) getInSession("LOGADO");
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yy");
SimpleDateFormat formatMesAno = new SimpleDateFormat("MM/yyyy");
Date dataInicio = null;
Date dataFim = null;
Integer uj = null;
try {
dataInicio = format.parse("01/" + getPeriodoInicio());
dataFim = format.parse("01/" + getPeriodoFim());
uj = getUjRemessa();
} catch (Exception e) {
e.printStackTrace();
}
String dataSeparada[] = formatMesAno.format(dataInicio).split("/");
boolean verificadorData = prestacaoFolhaService.verificarMesAnoUJ(
Integer.parseInt(dataSeparada[1]),
Integer.parseInt(dataSeparada[0]), uj);
if (verificadorData) {
verificadorRemessa = "não contêm";
} else {
verificadorRemessa = "contêm";
}
return verificadorRemessa;
}
My method works perfectly, the problem is that I am not able to send the return of the back-end method to my ajax request just below:
$.get("AdministracaoRemessa!verificadorRemessaExistente", 'periodoInicio='+ dataInicio + '&periodoFim=' + dataFim + '&ujRemessa=' + uj, function(data){
alert("o que existe dentro de data" + data);
})
The variable data
is returning the entire web page instead of just returning me the value of my back-end method verificadorRemessaExistente
.
How do I make my ajax request receive the value of my back-end verificadorRemessaExistente
?
OBS: The form can successfully register, what I need and perform the validation to know if the date that the user is trying to register already exists in the database.
Depending on the return of the ajax request below would put a validation similar to this below if the method was working perfectly.
dataInicio = $('#idPeriodoInicio').val();
dataFim = $('#idPeriodoFim').val();
uj = $('#idUJ').val();
$.get("AdministracaoRemessa!verificadorRemessaExistente", 'periodoInicio='+ dataInicio + '&periodoFim=' + dataFim + '&ujRemessa=' + uj, function(data){
if(data === 'contém'){
$msgErro.text("Já existe remessa para o período informado.");
$("#modalAlert").removeClass("hidden");
}else{
$('#formCadastrarJustificativa').submit();
}
}
});
Want to receive that date in which format?
– MauroAlmeida
I’m open to questions!
– wladyband
I think the project frameworks requires that all Bach-end methods returns String, that’s why I made the checkRemessaExistent method returns a String.
– wladyband
You can include in your question the link to the framework you are using, along with indicating the version?
– MauroAlmeida
I can’t unfortunately send the project framework link, but I can inform I’m using Struts 2
– wladyband
https://struts.apache.org/index.html, couldn’t send this because? I was just wondering if you were really using this.
– MauroAlmeida
I’m sorry, it’s because I didn’t quite understand the question.
– wladyband
Behold here if this solves your problem.
– MauroAlmeida
It gave to have a vague notion of how to solve, being that it is still not enough.
– wladyband
Let’s go continue this discussion in chat.
– MauroAlmeida