我有一个 JSF 页面,一个从服务中获取值的下拉列表。
<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>
导出列表包含 - abc、xyz
示例服务类
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);
}
}
还有一个操作按钮,用于根据选择的值类型(即 abc 或 xyz)生成 pdf。
点击转PDF按钮
<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();
}
}
我需要为从下拉列表中选择的值生成 pdf。如果选择“abc”,它应该调用methodAbc(),如果选择“xyz”,它应该调用methodXyz()。
此外,下拉列表可能包含更多值 - abc、xyz、pqr、rst 等。我知道为下拉列表中的每个值添加方法是不可行的。