How to show a report with jasperreport when clicking on a button?

0

I have the following code in my jsp, and what I want to do is that when I click on the button, it sends me to another page to show the report, what it does is that if it enters my method that I have in the controller but shows nothing or marks error

<script>
	function generarDocumento(){
		var data = "consultaForm";
		var url: "${pageContext.request.ContextPath}/report";
		$.ajax({
		type: "POST",
		url: url,
		contentType: "application/json",
		data: data,
		dataType: 'json',
		
		});
	
	}
</script>
<form id="consultaForm">

	<button type="button" onclick="generarDocumento();">GENERAR</button>

</form>

and the code in my controller

@RequestMapping("/report")
public String verReporte(Model model, @RequestParam(name = "format", defaultValue="pdf", required = false) String format{

	model.addAttribute("format", format);
	model.addAttribute("datasource", facturaService.consultaAll());
	model.addAttribute("autor", "Dev");

	return "prueba_reporte";
	
	
}

and that is if I enter the url /report directly in the browser if you show me my report, but I want to do it from my button of my jsp How can I do it?

What do I have to change to send it from my button? Thanks -

    
asked by Root93 02.10.2018 в 06:08
source

2 answers

1

Is it really necessary to use ajax to load your report? Because I see that the data you send in ajax is just a string. Then why not do it for GET with a simple link?

<a href="${pageContext.request.ContextPath}/report">Generar</a>

Or if you use Bootstrap you can click on the link:

<a href="${pageContext.request.ContextPath}/report" class="btn btn-default">Generar</a>

If it is necessary to send the data by POST you can do it this way

<form action="${pageContext.request.ContextPath}/report" method="POST" target="_blank">
  <input type="hidden" name="param1" value="value1">
  <input type="hidden" name="param2" value="value2">
  <input type="submit" value="GENERAR"></form>

This way the data will be sent by POST and the result will open in a new window or tab.

    
answered by 02.10.2018 / 19:07
source
1

If you are always going to send the data by POST to the report you should change

@RequestMapping("/report")

for

@RequestMapping(value={"/report"}, method=RequestMethod.POST)
    
answered by 02.10.2018 в 19:30