How to validate the download of a file when there is no data, using spring?

0

I have the following method in the controller that passes parameters to a service method to do the query search, and the information is downloaded into a csv text file

@RequestMapping(value= "/views/formulario", method = RequestMethod.POST, produces="text/csv")
public void obtieneDatos(@ModelAttribute("consultaForm") Factura fact, Model model, HttpServletResponse response) {
   
   String id = fact.getId();
   String cvePag=fact.getClavePago();
   Date fecha = fact.getFechaInicial();

   List<Factura> factList = obtieneInfoService.consultaDatos(id, cvePag, fecha);

   response.addHeader("Content-Type", "application/csv");
   response.addHeader("Content-Disposition", "attachment; filename=export.csv");
   try {
       PrintWriter out = response.getWriter();
       out.write("nombreCol1;nombreCol2;...;nombreColN;");
       out.write("\n");

      if(factList !=null && !factList.isEmpty){
		 for(Factura result: factList) {
           out.write(result.toCSVRepresentation());
           out.write("\n");
		 }
		}
       out.flush();
       out.close();
   } catch (IOException ix) {
       throw new RuntimeException("There was an error while retrieving CSV data", ix);
   } 
  } 

In my form I have the fields and a search button, when I click on the button, the text file is downloaded automatically, even if you do not put values in the inputs, or fill them with data that do not have information, even so, download it, and what I want to do is not let it happen, that if I click on the button without filling in the fields, send me a message that says, you must fill in the fields, and if I fill them and there is no information with these data, which says no records were found.

In my controller code, I put an if for the list, that if it is different from null, do the iteration, and try to put an else and say no data, and paint it on the console, but still download the file, How can I do it, so that I only download it when there is data?

This is my vision code

function consultar(){
var valido = true;
var valId = document.getElementaryById('idPago').value;
var valClavePago = document.getElementaryById('clavePago').value;
var valFecha = document.getElementaryById('fechaPago').value;

if(valId.trim() =="" && valClavePago.trim() == "" && valFecha.trim() == ""){

  HelperDialogs.mostrar("Debes ingresar al menos un valor");
  valido = false;
}
  return valido;
}


<form: form id="consulta" modelAttribute="consultaForm">
  <tr>
    <td><label>ID/label></td>
    <td><form: input type="text"  id="idPago" path="id"/></td>
   </tr>
   
   <tr>
    <td><label>CLAVE PAGO</label></td>
    <td><form: input type="text" id="clavePago" path="clavePago"/></td>
   </tr>
   
   <tr>
   
   
   <tr>
    <td><label>FECHA PAGO</label></td>
    <td><form: input type="text" id="fechaPago" path="fechaPago"/></td>
    <input type="submit" onclick="consultar()"></input>
   </tr>
</form:form>

that validates that the fields are not empty at the moment of clicking on the button, but at the same time makes the download of the file, How can I control that? I've seen that they use ajax, but I really have no idea, could someone help me? Thanks

    
asked by Root93 11.06.2018 в 00:22
source

1 answer

0

Just wait to see if there is data BEFORE ADDING THE HEADER S.

If there is data, add the headers and write in out the contents of the file; if there is no data then what you decide:

  • Write the HTML of an error message ("'NO DATA')

  • Redirect to an error page.

  • ...

answered by 11.06.2018 в 16:09