2015-11-08 140 views
0

我得到的文件空值上傳我的代碼如下....請讓我知道什麼是缺少Primefaces +春文件上傳

@Component("businessInformationBean") 
@Scope("view") 
public class BusinessInformationBean extends AbstractBaseBean { 

    private static final Logger LOGGER = Logger 
      .getLogger(BusinessInformationBean.class); 

    private static final long serialVersionUID = 1L; 

    @Autowired 
    private BusinessInformationService businessInformationService; 

    @Value("#{addressBean}") 
    private AddressBean address; 

    private String firmName; 
    private Date openingDate; 
    private Date activityDate; 
    private String businessType; 
    private Date seasonStartDate; 
    private Date seasonEndDate; 
    private Date closerDate; 
    private String natureOfBusiness; 
    private UploadedFile file; 
    private String bankAccholdername; 
    private String bankName; 
    private String bankBranchName; 
    private String accountNumber; 
    private String accountType; 
    private String bankAddress; 
    private String bankIFSCCode; 
    private String bankCity; 
    private String bankDistrict; 
    private String bankState; 
    private Integer bankZipcode; 


    public void save(ActionEvent event) { 
     LOGGER.info("businessInformationBean Save"); 
     try 
     { 
      FacesContext fc = getFacesContext(); 

      if (frmValidation()) 
      { 
       MmBusinessInformation businessInformation = businessInformationService 
         .fetchBusinessInformationServiceByLoginUser(getCurrentUser()); 
       mapWebBackToDomain(businessInformation); 
       businessInformationService.saveOrUpdate(businessInformation); 
       renderRecordSaveMessage(); 
      } 
      else 
      { 
       fc.renderResponse(); 
      } 
     } 
     catch (Exception e) 
     { 
      LOGGER.error(e.getMessage()); 
      renderFatalError(); 
     } 
    } 

    public UploadedFile getFile() { 
     return file; 
    } 

    public void setFile(UploadedFile file) { 
     this.file = file; 
    } 

我的web.xml低於:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app 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-app_3_0.xsd" id="WebApp_ID" version="3.0"> 
    <display-name>ems-web</display-name> 
    <welcome-file-list> 
     <welcome-file>login.xhtml</welcome-file> 
    </welcome-file-list> 

    <!-- Change to "Production" when you are ready to deploy --> 
    <context-param> 
     <param-name>javax.faces.PROJECT_STAGE</param-name> 
     <param-value>Development</param-value> 
    </context-param> 
    <context-param> 
     <param-name>primefaces.THEME</param-name> 
     <param-value>redmond</param-value> 
    </context-param> 
    <!-- Spring Security Facelets Tag Library --> 
    <context-param> 
     <param-name>javax.faces.FACELETS_LIBRARIES</param-name> 
     <param-value>/WEB-INF/springsecurity.taglib.xml</param-value> 
    </context-param> 

    <context-param> 
     <param-name>primefaces.FONT_AWESOME</param-name> 
     <param-value>true</param-value> 
    </context-param> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>    
      /WEB-INF/applicationContext.xml 
      /WEB-INF/security-context.xml 
     </param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <listener> 
     <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
    </listener> 
    <servlet> 
     <servlet-name>Faces Servlet</servlet-name> 
     <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>Faces Servlet</servlet-name> 
     <url-pattern>*.xhtml</url-pattern> 
    </servlet-mapping> 



<filter> 
     <filter-name>PrimeFaces FileUpload Filter</filter-name> 
     <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class> 
     <init-param> 
      <param-name>uploadDirectory</param-name> 
      <param-value>/home/alekss/uploads</param-value> 
     </init-param> 
     <init-param> 
      <description>Maximum size of stored in memory file</description> 
      <param-name>thresholdSize</param-name> 
      <param-value>10240000</param-value> 
     </init-param> 
    </filter> 
    <filter-mapping> 
     <filter-name>PrimeFaces FileUpload Filter</filter-name> 
     <servlet-name>Faces Servlet</servlet-name> 
    </filter-mapping> 







    <filter> 
     <filter-name>hibernateFilter</filter-name> 
     <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> 
    </filter> 
    <filter-mapping> 
     <filter-name>hibernateFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 

    <!-- Spring security filter --> 
    <filter> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <filter-class>org.springframework.web.filter.DelegatingFilterProxy 
     </filter-class> 
    </filter> 
    <filter-mapping> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <url-pattern>/*</url-pattern> 
     <dispatcher>FORWARD</dispatcher> 
    <dispatcher>REQUEST</dispatcher> 

    </filter-mapping> 
</web-app> 

和.xhtml文件代碼如下

<p:fileUpload id="fileUp" fileUploadListener="#{businessInformationBean.file}" mode="commons" /> 

請給我意見缺什麼..its很多迷惑我的... 在此先感謝。

回答

0

在P中的fileUploadListener屬性:文件上傳預計與FileUploadEvent參數的方法,是這樣的:

public void handleFileUpload(FileUploadEvent event) { 
    //try { 
     UploadedFile uf = event.getFile(); 
     //uf.getFileName(); 
     //uf.getInputstream(); 

    //} catch (IOException e) { 
     //Do something 
    //} 
} 

<p:fileUpload id="fileUp" fileUploadListener="#{businessInformationBean.handleFileUpload}" mode="commons" /> 

然後,你可以得到流,文件名等

此外,檢查您的形式有enctype屬性。

<h:form id="myForm" enctype="multipart/form-data"> 
+0

感謝DAVO :) 是其工作的感謝答覆:) 讓我分享我的整個情況......我有一種形式... [名/ idProfImage/ 地址/ 圖像] 爲了這個,我想要兩個文件上傳控制器.... 作爲該文件的上傳控制其上傳時上傳按鈕,單擊 我是新的primefaces :)所以從你這什麼建議嗎? – Minesh

+0

我只想在save()方法中獲取圖像obj,以便我可以繼續使用其他表單數據以用於保存目的.... – Minesh

+0

在您的handleFileUpload方法中,可以設置控制器/ bean變量file = event.getFile );然後在你的保存方法中,你可以得到文件變量來獲得流,file.getInputStream() – DavoCoder