我目前正在开发 JSF 2.0 应用程序,其工作方式如下:
- 1)使用Prime faces上传excel文件(工作正常)\ 2)访问数据并根据ping测试更新excel的第三列,在MultiplePingtesting bean中设置失败和通过的计数值(工作正常)3)ChartBean4应该访问通过并且计数失败并生成图表(不工作) 4)使用不同文件进行测试应该产生正确的图表(不工作)
工作环境:JSF2.0 with prime faces 3.5, netbeans 7.1 using Glassfish server
问题:如果我尝试访问 Chartbean4.java 中失败和通过的计数,它将为零并且饼图出现错误!!!请让我知道是否有任何错误
面孔-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<managed-bean>
<managed-bean-name>multiplepingtesting</managed-bean-name>
<managed-bean-class>control.MultiplePingTesting</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>chartbean4</managed-bean-name>
<managed-bean-class>control.ChartBean4</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
<managed-property>
<property-name>multiplepingtesting</property-name>
<value>#{multiplepingtesting}</value>
</managed-property>
</managed-bean>
</faces-config>
来自.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:t="http://myfaces.apache.org/tomahawk">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>:: From ::</title>
</h:head>
<h:body>
<h:form enctype="multipart/form-data">
<p:fileUpload fileUploadListener="#{multiplepingtesting.handleFileUpload}"
mode="advanced" auto="true"
allowTypes="/(\.|\/)(xls|xlsx)$/" />
<p:growl id="display" showDetail="true" />
<p:commandButton value="suraj" action="PieChart" ajax="false"></p:commandButton>
</h:form>
// ** MultiplePingTesting.java **//
public class MultiplePingTesting implements Serializable
{
public int passed, failed; //added getters and setters
private int i=0,j=0;
private UploadedFile fileName;//added getters and setters
public void handleFileUpload(FileUploadEvent event)
{
try
{
FileInputStream fileInputStream;
File fName=new File(event.getFile().getFileName());
fileInputStream = new FileInputStream(fName);
System.out.println("insdie try block ");
FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
POIFSFileSystem fsFileSystem = new POIFSFileSystem(fileInputStream);
HSSFWorkbook workbook = new HSSFWorkbook(fsFileSystem);
String ip,message;
double port;
message="";
HSSFSheet sheet = workbook.getSheet("IP");
FileOutputStream out = new FileOutputStream(fName);
short col=2;
int lastrow=sheet.getLastRowNum()+1;
for(int rownum=1;rownum<lastrow;rownum++)
{
System.out.println("********" + rownum);
HSSFRow row = sheet.getRow(rownum);
ip=row.getCell((short)0).getStringCellValue();
port=row.getCell((short)1).getNumericCellValue();
try{
Socket s1 = new Socket(ip, (int) port);
InputStream is = s1.getInputStream();
DataInputStream dis = new DataInputStream(is);
if (dis != null)
{
System.out.println("Connected with ip "+ip+" and port 21");
message="Connected";
i++;
}
else
{
System.out.println("Connection invalid");
message="Connection Timed Out";
}
dis.close();
s1.close();
}
catch(Exception e)
{
System.out.println("Server not Connected,Connection timed out");
message="Server not Connected,Connection timed out";
j++;
}
row.createCell(col).setCellValue(message);
}
workbook.write(out);
out.close();
setFailed(i);
setPassed(j);
System.out.println("value of failed" + i + "*******"+getFailed());
System.out.println("value of succes" + getPassed());
}
catch(Exception e)
{
System.out.println("exception\n"+ e.getStackTrace());
}
}
// * PieChart.xhtml //
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:t="http://myfaces.apache.org/tomahawk">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>:: Piechart::</title>
</h:head>
<h:body >
<h:form>
<td align="right">
<h:outputText value="#{chartbean4.sj}"/>
<p:pieChart id="sample" value="#{chartbean4.pieModel}" legendPosition="w"
title="Sample Pie Chart" style="width:400px;height:300px" />
<h:commandButton name ="multiple_testing" action="From"/>
</td>
</h:form>
</h:body>
</html>
// * ChartBean4.java * /
public class ChartBean4 implements Serializable {
private MultiplePingTesting multiplepingtesting;
public MultiplePingTesting getMultiplepingtesting() {
return multiplepingtesting;
}
public void setMultiplepingtesting(MultiplePingTesting multiplepingtesting) {
this.multiplepingtesting = multiplepingtesting;
}
private PieChartModel pieModel;
@PostConstruct
public void init()
{
System.out.print("***********multiple_test.failed" + multiplepingtesting.getFailed());
createPieModel();
}
public PieChartModel getPieModel() {
return pieModel;
}
private void createPieModel() {
System.out.print("*****Inside Create Pie model******multiple_test.failed" + multiplepingtesting.getFailed());
pieModel = new PieChartModel();
pieModel.set("failed", multiplepingtesting.getFailed());
pieModel.set("success", multiplepingtesting.getPassed());
System.out.print("***********'");
}
}