0

I need to implement a call to a method that I have in my controller. But I want to call this method from a scriptlet and I don't know how to do it. I am trying to export data that I get from the server to CSV.

This is the scriptlet that I have so far:

<%  
String csvDataIn = request.getParameter("exportCSVParam");
String csvFileName = request.getParameter("exportCSVFileName");
if (csvFileName == null || csvFileName == "") csvFileName = "export.csv";

String strHeader = "attachment; filename=" + csvFileName;
String contentType = "application/octet-stream";
response.setContentType(contentType);
response.addHeader("content-disposition",strHeader);

ServletOutputStream ostr = response.getOutputStream();    
String data=csvDataIn;//DATA GOES HERE;
ostr.write(data.getBytes("ISO-8859-1"));
ostr.flush();
ostr.close();
%>

Assume that I want to call a method getDataAsCsv() that I have in my controller that returns a String with the CSV data that I want to print in that file. Lines 1 and 2 (csvDataIn, csvFileName) should be deleted since I am not going to send parameters to this jsp. How do you do that?. How do you bind the controller bean with this scriptlet.

I am new to spring and I am still learning about this. Probably the solution is very simple but I am stuck with this.

4

1 に答える 1

2

You can't, because you shouldn't. All this code should go in the controller.

(technically, you can have a JSTL function and call it, or simply call a static method, or even get the controller with WebApplicationContextUtils.getRequiredWebApplicationContext(..).getBean(..), but all these will be ugly)

于 2012-04-10T20:28:06.907 に答える