PieChart使用Prime面对3.5和jsf 2.0空置

时间:2013-09-12 11:48:50

标签: jsf-2 primefaces

我目前正在研究JSF 2.0应用程序,其工作原理如下:

  • 1)使用Prime面上传excel文件(woerking fine)\ 2)访问数据    并根据ping测试更新excel的第三列,设置    失败并在MultiplePingtesting bean中传递计数值(有效    罚款)3)ChartBean4应该访问传递和失败的计数和    生成图形(不工作)4)应该使用不同的文件进行测试    生成适当的图表(不工作)

工作环境:面向主要面孔的JSF2.0 3.5,使用Glassfish服务器的netbeans 7.1

问题:如果我尝试访问Chartbean4.java中失败并传递的计数,它将变为零并且饼图出错!!!如果错误,请告诉我

faces-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>

From.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("***********'");

    }

}

1 个答案:

答案 0 :(得分:0)

最后,我得到了一些解决方法的答案,以下是完成的更改

  1. 将faces-config,xml中的范围更改为“request”
  2. 在MultiplePingTesting.java中将i和j变量引入为静态。
  3. 在MultiplePingTesting的正向方法失败并且使用i和j变量
  4. 设置传递的属性

    ** * ** 以下是更新的文件 * ** * ***

    From.xhtml

     <h:form enctype="multipart/form-data">      
        <p:fileUpload fileUploadListener="#{multiplepingtesting.handleFileUpload}"
        mode="advanced" auto="true" 
        allowTypes="/(\.|\/)(xls|xlsx)$/" />
        <p:commandButton value="PieChart" action="#{multiplepingtesting.forward}" ajax="false"></p:commandButton>
        <p:growl id="display" showDetail="true" />
    
        </h:form>
    

    <强> MultiplePingTesting.java (文件中注释了setFailed()和setPassed()代码和附加的forward()方法

    public String forward()
        {
                setFailed(j);
               setPassed(i);
    
      return "PieChart" ;
        }