So I have a method that is correctly creating a CSV file with a bunch of data when called by doGet(HttpServletRequest request, HttpServletResponse response)
which is called when I create a button like this:
link = new HtmlOutputLink();
HtmlGraphicImage img = new HtmlGraphicImage();
img.setStyle("background-color: #FFFFFF;");
img.setTitle("Click to Export these requests to csv file");
img.setValue("../images/Export.PNG");
link.getChildren().add(img);
link.setValue(resp.encodeURL(Constants.TXT_ALL_DIV_TEAM_EXPORT_LINK));
cell = new DataTableCell();
cell.setType(CellType.DATA);
cell.setFormat(new Format(Format.CENTER));
cell.addElement(link);
headerRow.addElement(cell);
When the button is clicked the doGet method then calls the method which creates the CSV file (which, again, is working correctly called from doGet).
However, I have to change this from an image to a CommandButton, the command button is a custom class that extends the javax.faces.component.html.HtmlCommandButton package, so now I have this:
HtmlOutputLink link = new HtmlOutputLink();
CommandButton alertsButton = new CommandButton();
alertsButton.setId(UI_EXPORT_ID);
alertsButton.setValue(UI_EXPORT_TXT);
alertsButton.setOnclick("javascript:showWaitLayer();jsCBDupdateComponent('" + "form" + "', this );");
alertsButton.setBlockSubmit(true);
alertsButton.setImmediate(true);
alertsButton.addActionListener(this);
link.getChildren().add(alertsButton);
cell = new DataTableCell();
cell.setType(CellType.DATA);
cell.setFormat(new Format(Format.CENTER));
cell.addElement(link);
headerRow.addElement(cell);
When this button is clicked it calls processAction() in which I instantiate the HttpServletResponse used to pass in to the working method.
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse();
response.setContentType("application/octet-stream");
HomeController homeController = (HomeController) context.getApplication().createValueBinding("#{HomeController}").getValue(context);
homeController.createExportFile(response);
EDIT: Adding the createExportFile method with lots taken out for readability.
EDIT2: I've changed the createExportFile so that no HttpServletResponse needs to be passed in. This way the method doesn't have any dependencies on passed in parameters. Both of the buttons (the one with the img and the one that is the CommandButton) call this method and run through without errors in the exact same method. Only the img button creates the excel file though.
public void createExportFile()
throws IOException, PersistenceException, SQLException {
FacesContext context = FacesContext.getCurrentInstance();
HttpServletResponse resp = (HttpServletResponse) context.getExternalContext().getResponse();
resp.setContentType("application/octet-stream");
resp.setContentLength(500 * this.getWorkAllDivDeptList().size());
resp.setHeader("Content-Disposition", "attachment; filename=\""
+ "AllDivTeam.csv" + "\""); Map<String, HashSet<String>> stateDateMap = new HashMap<String, HashSet<String>>();
ArrayList<DynamicFieldInfo> txtFieldAllList = new ArrayList<DynamicFieldInfo>();
RequestReader kanbanReader;
try {
//Get all of the data from the DB
} catch (MidTierException mte) {
mte.printStackTrace();
}
String rowTxt = getExportRowHdrTxt(txtFieldAllList, addlColCnt);
response.getOutputStream().write(rowTxt.getBytes(), 0, rowTxt.length());
kanbanReader = new RequestReader("");
for (AllActiveWorkListInfo bwi : (ArrayList<AllActiveWorkListInfo>) this
.getFilteredAllDivDeptList()) {
HashSet<String> set = (HashSet<String>) stateDateMap.get(bwi.getMID());
if (null != set && !set.isEmpty()) {
Iterator<String> itr = set.iterator();
while (itr.hasNext()) {
rowTxt = getExportRowTxt(bwi, txtFieldAllList,
kanbanReader, (String) itr.next());
response.getOutputStream().write(rowTxt.getBytes(), 0,
rowTxt.length());
}
} else {
rowTxt = getExportRowTxt(bwi, txtFieldAllList, kanbanReader, "");
response.getOutputStream().write(rowTxt.getBytes(), 0,
rowTxt.length());
}
if (count++ == 200) {
response.getOutputStream().flush();
}
}
response.getOutputStream().flush();
response.getOutputStream().close();
}
Adding Headers:
(Request-Line) POST /kanban/faces/kanbanRepAllDivDeptTickets HTTP/1.1
Accept */*
Accept-Encoding gzip, deflate
Accept-Language en-us
Cache-Control no-cache
Connection Keep-Alive
Content-Length 530
Content-Type application/x-www-form-urlencoded
Cookie _cbdModemCheck=false; JSESSIONID=08ADA3D60982F9D13478AF729D6E5205; s_fid=24245A567AE4BB33-0F8E3B5CF3FBEED7
Host localhost:8080
Referer http://localhost:8080/kanban/faces/kanbanRepAllDivDeptTickets
User-Agent Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)
When I debug it goes through all of the process to create the csv exactly like when called from doGet but it never opens the dialog to download or cancel. It's throwing no exceptions and I'm completely out of ideas.
Does anyone see where I'm making an incorrect assumption?
Thanks for your time, Mike