commandButton没有在jsf中调用action方法

时间:2013-11-21 06:27:59

标签: jsf

FileUpload.java

package com.primefaces.sample;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.apache.myfaces.custom.fileupload.UploadedFile;

@ManagedBean
@RequestScoped
public class FileUpload {

    private UploadedFile upload;
    private String uploadFileName;
    private String dirPath;
    private String imf;

    public String getImf() {
        return imf;
    }

    public void setImf(String imf) {
        this.imf = imf;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    public String getDirPath() {
        return dirPath;
    }

    public void setDirPath(String dirPath) {
        this.dirPath = dirPath;
    }

    public UploadedFile getUpload() {
        return upload;
    }

    public void setUpload(UploadedFile upload) {
        this.upload = upload;
    }

    public FileUpload() {}

    public String uploadFile() {
        File savedFileName;
        dirPath = "D:\\testfile\\";
        InputStream fileContent = null;
        if (upload == null) {
            return "success";
        }

        try {
            fileContent = upload.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        uploadFileName = trimFilePath(upload.getName());
        savedFileName = new File(dirPath + uploadFileName);
        BufferedOutputStream bos = null;

        try {
            bos = new BufferedOutputStream(new FileOutputStream(savedFileName));
        } catch (FileNotFoundException e) {
        }
        byte[] buffer = new byte[1024];
        int len;
        try {
            while ((len = fileContent.read(buffer)) >= 0) {
                bos.write(buffer, 0, len);
            }
        } catch (IOException e) {
        }
        try {
            fileContent.close();
            bos.close();
        } catch (IOException e) {
        }

        // splitingData.insertData(dirPath, uploadFileName);
        return "home";
    }

    public String trimFilePath(String fileName) {
        return fileName.substring(fileName.lastIndexOf("/") + 1).substring(fileName.lastIndexOf("\\") + 1);
    }

    public List<UploadBean> getDisplayData() {
        List<UploadBean> uploadBeans = new ArrayList<UploadBean>();
        try {
            FileInputStream fin = new FileInputStream(new File(dirPath+ uploadFileName));
            BufferedReader br = new BufferedReader(new InputStreamReader(fin));

            // Reading records from file
            br.readLine();// out.println(column);
            String row = null;
            while ((row = br.readLine()) != null) {
                // out.println(row);
                String[] rowtemp = row.split(",");
                UploadBean uploadBean = new UploadBean();
                uploadBean.setHtno(rowtemp[0]);
                uploadBean.setEcode(rowtemp[1]);
                uploadBean.setSubcode(rowtemp[2]);
                uploadBean.setImf(rowtemp[3]);
                uploadBean.setHmfm(rowtemp[4]);
                uploadBean.setCredits(rowtemp[5]);
                uploadBean.setSubname(rowtemp[6]);

                uploadBeans.add(uploadBean);
            }
            // st2.executeUpdate("DELETE FROM resultsdatas WHERE ROWID not in (SELECT MIN(ROWID) FROM resultsdatas GROUP BY HTNO,ECODE,SUBCODE,IMF,HMFM,CREDITS,SUBNAME)");

            fin.close();
        }// try

        catch (FileNotFoundException e) {
            System.out.println(e);
        }
        catch (Exception e) {
            System.out.println(e);
        }
        return uploadBeans;
    }

    public String uploadAll() {
        System.out.println("chek11-----------------"+dirPath);
        try {
            System.out.println("chek1-----------------"+dirPath);
            FileInputStream fin = new FileInputStream(new File(dirPath + uploadFileName));
            System.out.println("chek1-----------------1");
            BufferedReader br = new BufferedReader(new InputStreamReader(fin));

            Connection con = null;
//          Statement st = null;
            PreparedStatement pst = null;

            String classname = "oracle.jdbc.driver.OracleDriver";
            String driver = "jdbc:oracle:thin:@localhost";
            String port = "1521";
            String database = "m6db";
            String user = "asap";
            String pass = "asap";
            Class.forName(classname).newInstance();
            con = DriverManager.getConnection(driver + ":" + port + "/" + database, user, pass);
//          st = con.createStatement();

            String column = br.readLine();// out.println(column);
            uploadFileName = uploadFileName.substring(0, uploadFileName.length() - 4);// to cut the extention ".txt" from the filename

            // Reading column names from file
            String columntemp[] = column.split(",");
            for (int i = 0; i < columntemp.length; i++) {
                System.out.println("columns---------->" + columntemp[i]);
            }

            // Preparing insert query
            String insertquery="insert into "+uploadFileName+" values("; 
            for(int i=0;i<columntemp.length;i++) { 
                insertquery=insertquery+"?";
                if((i+1)<columntemp.length)//to cut the last comma
                    insertquery=insertquery+","; 
            } 
            insertquery=insertquery+")";
            pst=con.prepareStatement(insertquery);

            // Reading records from file
            String row = null;
            while ((row = br.readLine()) != null) {
                String[] rowtemp = row.split(",");
                for(int i=0; i<rowtemp.length; i++) {
                    pst.setString(i+1,rowtemp[i]); 
                }
                pst.executeUpdate();//inserting into database s=s+r;
            }

            // st.executeUpdate("DELETE FROM resultsdatas WHERE ROWID not in (SELECT MIN(ROWID) FROM resultsdatas GROUP BY HTNO,ECODE,SUBCODE,IMF,HMFM,CREDITS,SUBNAME)");
            fin.close();
        }// try

        catch (FileNotFoundException e) {
            System.out.println(e);
        } catch (SQLException e) {
            System.out.println(e);
        } catch (Exception e) {
            System.out.println(e);
        }
        return "login";
    }

    public String single() {
        System.out.println("-----------**---d---");
        /*FacesContext Ctx = FacesContext.getCurrentInstance();
        Map Params = Ctx.getExternalContext().getRequestParameterMap();
        String temp = (String) Params.get("imf");
        System.out.println("-----------**---d---"+temp);*/
        return "login";
    }
}

home.xhtml

<html xmlns="http://www.w3c.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui">
<h:head>
    <link type="text/css" rel="stylesheet" href="skin.css" />
</h:head>
<h:body>
<h:form id="testForm"> 
    <p:fieldset legend="data">  
        <p:dataTable id="fileData" var="dataList" value="#{fileUpload.displayData}">  
            <p:column headerText="Htno">  
                <h:outputText value="#{dataList.htno}" />  
            </p:column>  

            <p:column headerText="Ecode">  
                <h:outputText value="#{dataList.ecode}" />  
            </p:column>  

            <p:column headerText="Subcode">  
                <h:outputText value="#{dataList.subcode}" />  
            </p:column>  

            <p:column headerText="Imf">  
                <h:outputText value="#{dataList.imf}" />  
            </p:column> 

            <p:column headerText="Action"> 
            <p:commandButton image="Upload" ajax="false" style="margin-right:20px;" action="#{fileUpload.single}" ></p:commandButton>
            </p:column>
        </p:dataTable>
    </p:fieldset> 
</h:form>
<h:form id="UploadAll"> 
<h:inputHidden value="#{fileUpload.uploadFileName}" id="uploadFileName" />
<h:inputHidden value="#{fileUpload.dirPath}" id="dirPath" />
<h:commandButton type="submit" value="Upload All" action="#{fileUpload.uploadAll}"></h:commandButton>
</h:form> 
</h:body>

</html>

这里我展示了我的fileUpload.java和home.xhtml文件,在xhtml文件中我有两个按钮。 “全部上传”按钮完全可以从fileUpload.java文件中调用uploadAll方法,但“上传”按钮无法调用单个方法格式fileUpload.java(同一文件)。如果我单击“上传”按钮,它不会调用单一方法,即使它刷新我的home.xhtml页面,并给出一个警告,如下所示。

警告:找到无效标记:查找attr名称或'/&gt;'时意外输入第9行。环境:'“name =”UploadAll:j_idt16“val'。 java.io.FileNotFoundException:nullnull(系统找不到指定的文件)

你能给出解决方案吗? 我可以把上面的按钮调用动作类方法吗?

0 个答案:

没有答案