I have a JSF page, a drop down where values are getting fetched from service.
<h:selectOneMenu id="valueList" value="#" style="height:20px;">
<f:selectItem itemValue="Select Action" itemLabel="Select Action" />
<f:selectItems value="#{sampleService.sampleMethod}"
var="SampleExport" itemValue="#{SampleExport}"
itemLabel="#{SampleExport}">
</f:selectItems>
</h:selectOneMenu>
exportlist contains – abc, xyz
sampleService class
public class SampleServiceImpl implements .... {
private List<String> sampleList;
public List<String> getSampleList;() {
return sampleList;
}
public void setSampleList;(List<String> sampleList;) {
this.sampleList=sampleList;;
}
/**
* Method for List to be displayed in drop down
*/
public void sampleMethod(){
if (sampleList== null) {
sampleList = new ArrayList<String>();
sampleList.add("abc");
sampleList.add("xyz");
}
setSsampleList(sampleList);
}
}
and there is an action button, which is used to generate a pdf based on the selection of type of value i.e. abc or xyz.
clicktoPdf button
<ui:define name="actions">
<h:commandButton styleClass="inputbutton" value="GeneratePdf" id="export"
action="#{generatePdf.pdfReport}" style="float:right;width : 73px;" />
</ui:define>
public class GeneratePdf {
public void pdfReport() {
..........
...code....
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
HSSFRow row = sheet.createRow(0);
HSSFCell cell = row.createCell(0);
..........
.code..........
methodAbc(){
.....
}
method Xyz(){
......
}
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
externalContext.setResponseContentType("application/vnd.ms-excel");
externalContext.setResponseHeader("Content-Disposition",
"attachment; filename=\"Sample Report\"");
workbook.write(externalContext.getResponseOutputStream());
facesContext.responseComplete();
}
}
i need to generate pdf for the values selected from drop down. if “abc” is selected, it should call methodAbc(), if “xyz” is selected it should call
methodXyz().
Also the drop down list could contain more values – abc,xyz,pqr,rst etc etc. I know its not feasible to add method for every value in the drop down.
Create a private property:
bind it to the selectonemenu:
On submission, the
selectedValuewill have the value selected on the drop down. You can use it in thepdfReport()method.