我想上传文件并在jsf页面中显示它们,为此我使用的是tomahawk 1.1.12、jsf 2.0和jpa 2,我遵循了The BalusC教程,但该项目没有部署,并给出了错误:java.lang.NoClassDefFoundError:Lorg/apache/myfaces/custom/fileupload/UpdatedFilepom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>maven.test</groupId>
<artifactId>mavenTest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<repositories>
<repository>
<id>oss.sonatype.org</id>
<name>OSS Sonatype Staging</name>
<url>https://oss.sonatype.org/content/groups/staging</url>
</repository>
<repository>
<id>prime-repo</id>
<name>PrimeFaces Maven Repository</name>
<url>http://repository.primefaces.org</url>
<layout>default</layout>
</repository>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>http://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>apache-maven-snapshots</id>
<url>
http://people.apache.org/repo/m2-snapshot-repository
</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>myfaces-staging</id>
<url>
http://people.apache.org/builds/myfaces/m2-staging-repository
</url>
<releases>
<enabled>false</enabled>
<!--
Enable to test a MyFaces core release candidate with tomahawk
-->
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.0-RC1</version>
<exclusions>
<exclusion>
<groupId>org.eclipse.persistence</groupId>
<artifactId>commonj.sdo</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.1.11</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.1.11</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<dependency>
<groupId>org.apache.myfaces.tomahawk</groupId>
<artifactId>tomahawk</artifactId>
<version>1.1.12</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<!-- <dependency>
<groupId>org.apache.myfaces.core</groupId>
<artifactId>myfaces-api</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.myfaces.tomahawk</groupId>
<artifactId>tomahawk</artifactId>
<version>1.1.14</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>-->
</dependencies>
</project>
我的托管bean:
package mbeans;
//import java.io.IOException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.faces.application.FacesMessage;
//import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.Part;
/*
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
//import org.apache.commons.el.Logger;
//import org.apache.log4j.*;
//import org.apache.log4j.spi.LoggerFactory;
*/
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.myfaces.custom.fileupload.UploadedFile;
import daoImpl.DocDAO;
import entities.Document;
//import org.slf4j.*;
@ManagedBean
@RequestScoped
public class DocBean {
public DocDAO docDAO;
private Part file;
private String titreDocument;
private String descriptionDocument;
private String lien;
private String dateMise;
private String Categorie;
private Integer sizeDocument;
private String sousCategorie;
public DocBean(){
docDAO=new DocDAO();
}
private UploadedFile uploadedFile;
private String fileName;
// Actions ------------------------------------------------------------------------------------
public void submit() {
// Just to demonstrate what information you can get from the uploaded file.
System.out.println("File type: " + uploadedFile.getContentType());
System.out.println("File name: " + uploadedFile.getName());
System.out.println("File size: " + uploadedFile.getSize() + " bytes");
// Prepare filename prefix and suffix for an unique filename in upload folder.
String prefix = FilenameUtils.getBaseName(uploadedFile.getName());
String suffix = FilenameUtils.getExtension(uploadedFile.getName());
// Prepare file and outputstream.
File file = null;
OutputStream output = null;
try {
// Create file with unique name in upload folder and write to it.
file = File.createTempFile(prefix + "_", "." + suffix, new File("c:/data"));
output = new FileOutputStream(file);
IOUtils.copy(uploadedFile.getInputStream(), output);
fileName = file.getName();
// Show succes message.
FacesContext.getCurrentInstance().addMessage("uploadForm", new FacesMessage(
FacesMessage.SEVERITY_INFO, "File upload succeed!", null));
} catch (IOException e) {
// Cleanup.
if (file != null) file.delete();
// Show error message.
FacesContext.getCurrentInstance().addMessage("uploadForm", new FacesMessage(
FacesMessage.SEVERITY_ERROR, "File upload failed with I/O error.", null));
// Always log stacktraces (with a real logger).
e.printStackTrace();
} finally {
IOUtils.closeQuietly(output);
}
}
// Getters ------------------------------------------------------------------------------------
public UploadedFile getUploadedFile() {
return uploadedFile;
}
public String getFileName() {
return fileName;
}
// Setters ------------------------------------------------------------------------------------
public void setUploadedFile(UploadedFile uploadedFile) {
this.uploadedFile = uploadedFile;
}
//@EJB
//private DocDAO fileUploaderEJB;
//private final static Logger logger = LoggerFactory.getLogger(DocDAO.class);
//public void handleFileUpload(FileUploadEvent event) {
/* titreDocument = event.getFile().getFileName();
//String contentType = event.getFile().getContentType();
byte[] bytes = event.getFile().getContents();
Document garbage = new Document();
garbage.setDescriptionDocument(titreDocument);
garbage.setFile(bytes);
garbage.setDescriptionDocument("info about the file");
fileUploaderEJB.uploadGarbage(garbage);
//((Log) logger).info("Uploaded: {}");
FacesMessage msg = new FacesMessage("Succesful", event.getFile()
.getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);*/
//}
public String upload() throws IOException{
file.write("C:\data\"+getFilename(file));
return"succes";
}
private static String getFilename(Part part){
for(String cd: part.getHeader("content-disposition").split(";")){
if(cd.trim().startsWith("filename")){
String filename=cd.substring(cd.indexOf('=') + 1).trim().replace(""", "");
return filename.substring(filename.lastIndexOf('/') + 1).substring(filename.lastIndexOf('\') + 1);
}
}
return null;
}
/*public List<Document> getAlldoc(){
return docDAO.getAllDoc();
}
public String createdoc(){
docDAO.createDoc(titreDocument, descriptionDocument, sousCategorie, Categorie, sizeDocument, dateMise, lien);;
sizeDocument=null;
titreDocument="";
descriptionDocument="";
lien="";
sousCategorie="";
dateMise="";
Categorie="";
return "success";
}*/
public String getSousCategorie() {
return sousCategorie;
}
public void setSousCategorie(String sousCategorie) {
this.sousCategorie = sousCategorie;
}
public String getTitreDocument() {
return titreDocument;
}
public void setTitreDocument(String titreDocument) {
this.titreDocument = titreDocument;
}
public String getDescriptionDocument() {
return descriptionDocument;
}
public void setDescriptionDocument(String descriptionDocument) {
this.descriptionDocument = descriptionDocument;
}
public String getDateMise() {
return dateMise;
}
public void setDateMise(String dateMise) {
this.dateMise = dateMise;
}
public String getCategorie() {
return Categorie;
}
public void setCategorie(String categorie) {
Categorie = categorie;
}
public Integer getSizeDocument() {
return sizeDocument;
}
public void setSizeDocument(Integer sizeDocument) {
this.sizeDocument = sizeDocument;
}
public String getLien() {
return lien;
}
public void setLien(String lien) {
this.lien = lien;
}
public Part getFile() {
return file;
}
public void setFile(Part file) {
this.file = file;
}
}
我的实体:
package entities;
import java.io.Serializable;
import javax.persistence.*;
/**
* The persistent class for the document database table.
*
*/
@Entity
@Table(name="document")
@NamedQuery(name="Document.findAll", query="SELECT d FROM Document d")
public class Document {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name=""idDocument"", unique=true, nullable=false)
private Integer idDocument;
@Column(length=45)
private String categorie;
@Column(name=""dateMise"", length=45)
private String dateMise;
@Column(name=""descriptionDocument"", length=60)
private String descriptionDocument;
@Lob
@Column
private byte[] file;
@Column(length=45)
private String lien;
@Column(name=""sizeDocument"")
private Integer sizeDocument;
@Column(name=""sousCategorie"", length=45)
private String sousCategorie;
@Column(name=""titreDocument"", length=45)
private String titreDocument;
public Document() {
}
public Integer getIdDocument() {
return this.idDocument;
}
public void setIdDocument(Integer idDocument) {
this.idDocument = idDocument;
}
public String getCategorie() {
return this.categorie;
}
public void setCategorie(String categorie) {
this.categorie = categorie;
}
public String getDateMise() {
return this.dateMise;
}
public void setDateMise(String dateMise) {
this.dateMise = dateMise;
}
public String getDescriptionDocument() {
return this.descriptionDocument;
}
public void setDescriptionDocument(String descriptionDocument) {
this.descriptionDocument = descriptionDocument;
}
public byte[] getFile() {
return this.file;
}
public void setFile(byte[] file) {
this.file = file;
}
public String getLien() {
return this.lien;
}
public void setLien(String lien) {
this.lien = lien;
}
public Integer getSizeDocument() {
return this.sizeDocument;
}
public void setSizeDocument(Integer sizeDocument) {
this.sizeDocument = sizeDocument;
}
public String getSousCategorie() {
return this.sousCategorie;
}
public void setSousCategorie(String sousCategorie) {
this.sousCategorie = sousCategorie;
}
public String getTitreDocument() {
return this.titreDocument;
}
public void setTitreDocument(String titreDocument) {
this.titreDocument = titreDocument;
}
}
和我的网页xhtml:
<h:panelGrid columns="3">
<h:outputLabel for="file" value="Select file" />
<t:inputFileUpload id="file" value="#{docBean.uploadedFile}" required="true" />
<h:message for="file" style="color: red;" />
<h:panelGroup />
<h:commandButton value="Submit" action="#{docBean.submit}" />
<h:message for="uploadForm" infoStyle="color: green;" errorStyle="color: red;" />
</h:panelGrid>
</h:form>
<h:outputLink value="file/#{docBean.fileName}" rendered="#{docBean.fileName != null}">
Download back
</h:outputLink>
知道吗?
<dependency>
<groupId>org.apache.myfaces.tomahawk</groupId>
<artifactId>tomahawk</artifactId>
<version>1.1.12</version>
</dependency>
替换为
<dependency>
<groupId>org.apache.myfaces.tomahawk</groupId>
<artifactId>tomahawk20</artifactId>
<version>1.1.12</version>
</dependency>
感谢Omar,这是我尝试上传的版本问题,我只使用jsf 2.2,没有战斧或素数面。已解析