我正在尝试根据演示中给出的示例来上传Primefaces 3.0的组件。我还添加了commons-fileupload-1.2.2.jar和commons-io-2.0.1.jar以及primefaces-3.1.jar文件。
我运行上传器我得到一个java.lang.NullPointerException任何人都可以帮我解决这个问题,我已经广泛搜索了互联网,找不到有效的解决方案。
我正在使用NetBeans 7.2.1和GlassFish进行部署。
我的代码: XHTML:
<h:form id="frm" enctype="multipart/form-data">
<table>
<tr><td><div style="height:300px;"><img src="images/adcar.jpg" style="top:0px;"/>
</div>
</td>
<td>
<h:form id="img" enctype="multipart/form-data">
<div style=" -moz-border-radius:15px; border:1px solid; width:600px; border-color:#930303;">
<br></br>
<p:panel style="border:none;">
<p:messages></p:messages>
</p:panel>
<h:panelGrid columns="2">
<h:outputText value="Nom Projet :"/>
<h:inputText id="Nom" value="#{ajoutprjt.nomProjet}" />
<h:outputText value="Ref Projet :"></h:outputText><h:inputText value="#{ajoutprjt.idProjet}" />
<h:outputText value="Date de Début :"></h:outputText><h:inputText value="#{ajoutprjt.dateDeb}" />
<h:outputText value="Date de Fin :"></h:outputText><h:inputText value="#{ajoutprjt.dateFin}" />
<h:outputText value="Chef :"></h:outputText>
<h:selectOneMenu value="#{ajoutprjt.idChef}">
<f:selectItem itemValue="#{null}" itemLabel="-- select one --" />
<f:selectItems value="#{ajoutprjt.tdata}" var="cmd"
itemValue="#{cmd.id}" itemLabel="#{cmd.id} #{cmd.nom} #{cmd.prenom}" />
</h:selectOneMenu>
Documents : <p:fileUpload value="#{ajoutprjt.file}" id="fileupload" mode="simple"></p:fileUpload>
<h:outputText></h:outputText><p:panel style="border: none;"><p:commandButton value="Ajouter" action="#{ajoutprjt.ajoutP}" ajax="false"></p:commandButton><p:commandButton value="Annuler" action="#{ajoutprjt.annuleraj}" ajax="false"></p:commandButton></p:panel>
</h:panelGrid>
</div>
</h:form>
</td></tr> </table>
</h:form>[/code]
我的豆子:
package beans;
import com.persistence.Compte;
import com.persistence.Projets;
import com.persistence.geoUtil;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.primefaces.model.UploadedFile;
/**
*
* @author SA
*/
@ManagedBean
@RequestScoped
public class ajoutprjt implements Serializable{
private String idpro;
private String IdProjet;
private String NomProjet;
private String DateDeb;
private String DateFin;
private String nomimg;
private UploadedFile file;
private String IdChef;
private DataModel<Compte> tdata ;
public ajoutprjt(){
}
// getters et Setters
...
public UploadedFile getFile() {
System.out.println("Je suis sous GetFile: " );
return file;
}
public void setFile(UploadedFile file) {
System.out.println("je Suis Sous SetFile : ");
this.file = file;
}
public void ajoutP(){
// String p=Integer.toString(puis);
System.out.println("Ajout Projet de ref : " +IdProjet);
System.out.println("Ajout Projet de Idchef est : " +IdChef);
System.out.println("file a ajouter e : " +this.getFile().getSize());
if(IdProjet.equals("")==false && NomProjet.equals("")==false && DateDeb.equals("")==false && DateFin.equals("")==false && IdChef.equals("")==false){
try{
Session se=geoUtil.getSessionFactory().getCurrentSession();
Transaction tr=se.beginTransaction();
Query q = se.createQuery("SELECT p from Projets p where IdProjet = :rf");
q.setParameter("rf", IdProjet);
List<Object[]> list = (List<Object[]>) q.list();
if(list.size() == 0){
Projets p =new Projets();
p.setNomProjet(NomProjet);
p.setIdProjet(IdProjet);
p.setDateDeb(DateDeb);
p.setDateFin(DateFin);
p.setIdChef(IdChef);
// UploadedFile file = event.getFile();
if(this.getFile()!= null) {
String fileName = file.getFileName();
byte[] fileBytes = file.getContents();
p.setNomimg(fileName);
p.setImg(fileBytes);}
else{
p.setNomimg("Pas de fichier");
String s = "a";
byte[] a = s.getBytes();
p.setImg(a);
}
se.persist(p);
tr.commit();
context.addMessage(null,new FacesMessage(FacesMessage.SEVERITY_INFO,"Projet ajouté avec succes","Compte chef ajouté avec succes"));
this.setNomProjet("");
this.setIdProjet("");
this.setDateDeb("");
this.setDateFin("");
this.setIdChef("");
}else{
context.addMessage(null,new FacesMessage(FacesMessage.SEVERITY_ERROR,"Attention:Projet déjas existe","Attention"));
}
}
catch(Exception e1){
e1.printStackTrace();
context.addMessage(null,new FacesMessage(FacesMessage.SEVERITY_ERROR,"Attention:Erreur d'insertion","Attention"));
}
}
else{
context.addMessage(null,new FacesMessage(FacesMessage.SEVERITY_WARN,"Attention Tous les champs doivent etre remplis!!","Attention"));
}
}
}
这是我的web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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">
<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>aristo</param-value>
</context-param>
<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>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/auth.xhtml</welcome-file>
</welcome-file-list>
<resource-ref>
<description>Base de données pour app Ontologie
Projet master Pro</description>
<res-ref-name>jdbc/gcn</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>
org.primefaces.webapp.filter.FileUploadFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
</web-app>
非常感谢任何帮助。 这是堆栈跟踪:
javax.faces.el.EvaluationException: java.lang.NullPointerException
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NullPointerException
at beans.ajoutprjt.ajoutP(ajoutprjt.java:176)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.sun.el.parser.AstValue.invoke(AstValue.java:254)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:302)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
... 32 more
答案 0 :(得分:1)
我不明白如何在“actionListener”上使用此方法“ajoutP”,如“actionListener =”#{ajoutprjt.ajoutP}“,以便在方法”ajoutP(FileUploadEvent event)“中有一个参数。生成以下错误«javax.el.MethodNotFoundException»
答案 1 :(得分:0)
你似乎在那个页面上有很多东西。以下是我将如何进行调试:
使用PrimeFace网站上的这个示例,只需创建一个单独的 fileuploadController和查看页面,就像他们在此link
尝试让上面的步骤先工作,一旦你有了工作,然后开始慢慢地将这些元素添加到你现有的当前JSF页面和控制器,并改变以适应你的要求(从头开始,而不是你的内容)已经修改并发布在此页面上。)
我注意到了一件事:
答案 2 :(得分:-1)
在xhtml页面中使用actionListener
代替action
<p:commandButton value="Ajouter" actionListener="#{ajoutprjt.ajoutP}"
ajax="false"></p:commandButton>
将ManagedBean ajoutP()
方法更改为
public void ajoutP(FileUploadEvent event){
OutputStream out=null;
InputStream fileContent=null;
try{
UploadedFile uploadedFile= event.getFile();
out= new FileOutputStream(new
File("D:/")+uploadedFile.getFileName());
fileContent= uploadedFile.getInputstream();
int read=0;
final byte[] bytes= new byte[1024];
while((read= fileContent.read(bytes))!=-1){
out.write(bytes,0,read);
}
}
catch(FileNotFoundException fnfe){
}
catch(IOException ioex){
}
}