3

I am currently ussing Tomahawk MyFaces to upload files in to my server. I followed some instructions step by step and all seems ok, but i dont get the file persisted into the data base.I dont see any error, i just see some warnings in my eclipse console. Could someone have a look? This is what i did:

1-Downloaded Tomahawk for JSF 2.0 and added all the .jars to my WEB-INF/lib folder

2-I checked that my web.xml is correctly configured to use the Faces Servlet. And also i added a filter for tomahawks extensions This is how it looks like:

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

<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>/pages/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>pages/index.jsp</welcome-file>
</welcome-file-list>
    <filter>
    <filter-name>MyFacesExtensionsFilter</filter-name>
    <filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>MyFacesExtensionsFilter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>
    </web-app>

3-I also checked that my faces-config.xml is correct:

<?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">

4-I created a very simple composite page for the upload gadget

<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:t="http://myfaces.apache.org/tomahawk">
 <ui:composition template="WEB-INF/templates/BasicTemplate.xhtml">
<ui:define name="uploadForm">
<h:form>
<t:inputFileUpload value="#{uploadController.uploadedFile}" />
        <h:commandButton value="submit" action="#{uploadController.submit}" />
        <h:messages />
</h:form>
</ui:define>
 </ui:composition>
 </html>

5-I have a managed bean to comunicate with the page and get the inputed file:

@ManagedBean
@RequestScoped
public class UploadController {

@EJB
private IFileUploaderEJB fileUploaderEJB;

private UploadedFile uploadedFile;

public void submit() throws IOException {
    String fileName = FilenameUtils.getName(uploadedFile.getName());
    String contentType = uploadedFile.getContentType();
    byte[] bytes = uploadedFile.getBytes();

    // Now you can save bytes in DB (and also content type?)
    Garbage garbage = new Garbage();
    garbage.setFilename(fileName);
    garbage.setFile(bytes);
    garbage.setDescription("info about the file");
    garbage.setFileType("File extension");      
    fileUploaderEJB.uploadGarbage(garbage);

    FacesContext.getCurrentInstance().addMessage(
            null,
            new FacesMessage(String.format(
                    "File '%s' of type '%s' successfully uploaded!",
                    fileName, contentType)));
}

public UploadedFile getUploadedFile() {
    return uploadedFile;
}

public void setUploadedFile(UploadedFile uploadedFile) {
    this.uploadedFile = uploadedFile;
}}

Note: The managed bean calls an EJB that should persist into the database the file

6-An EJB to allow access to database:

    @Stateless(name = "ejbs/FileUploaderEJB")
    public class FileUploaderEJB implements IFileUploaderEJB {
@PersistenceContext
private EntityManager em;


public Garbage uploadGarbage(Garbage garbage) {
    Date date = new Date();
    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");        

    garbage.setUploadDate(dateFormat.format(date));     
    //...

    em.persist(garbage);

    return garbage;
}

7- And finally i have an Entity that uses JPA annotations and it uses a @Lob to store the file in the database:

    @Entity
    public class Garbage {
@Id
@GeneratedValue
@Column(nullable = false)
private Long id;
@Column(nullable = false)
private String filename;
@Column(nullable = false)
private String fileType;
@Column(nullable = false)
private String uploadDate;
@Column(nullable = false)
private String destroyDate;
@Lob
@Column(nullable = false)
private byte[] file;
@Column(nullable = false)
private String description;
    //Getters and Setters...

The problems are 3:

P1 - When i select a file with the browse button and then i click submit nothing happens. I dont see any input query in the console,no new rows are added to the database. What i am missing?

P2 - Whenever i make a change in my source code and publish again the console it takes longer than usual to build(almost 40 secs) and it displays me lots of warnings like this one(20 more or less):

WARNING: JSF1029: Application is versioned at 2.0 (either explicitly by the version of /WEB-INF/faces-config.xml or the lack of a /WEB-INF/faces-confg.xml), however class 'org.ajax4jsf.taglib.html.facelets.AjaxSupportHandler' depends on a legacy facelet class. The facelet artifact represented by this class will not be registered.

Is there something wrong with the jar files or with the configuration at the faces-config.xml?

P3 - When i navigate to any of the pages in my project ussing the browser, a warning like this one:

WARNING: PWC4011: Unable to set request character encoding to UTF-8 from context /Datapool, because request parameters have already been read, or ServletRequest.getReader() has already been called

I think it has something to do with the filter at web.xml

berkay
  • 3,907
  • 4
  • 36
  • 51
javing
  • 12,307
  • 35
  • 138
  • 211

1 Answers1

4

When i select a file with the browse button and then i click submit nothing happens.

Your <h:form> is missing the enctype="multipart/form-data" attribute. Check the mini tutorial here: JSF 2.0 file upload with Tomahawk's <t:inputFileUpload>.

WARNING: JSF1029: Application is versioned at 2.0 (either explicitly by the version of /WEB-INF/faces-config.xml or the lack of a /WEB-INF/faces-confg.xml), however class 'org.ajax4jsf.taglib.html.facelets.AjaxSupportHandler' depends on a legacy facelet class. The facelet artifact represented by this class will not be registered.

You still have some JSF 1.2-targeted RichFaces/A4J libs in your web project. Get rid of them if you don't need it.

WARNING: PWC4011: Unable to set request character encoding to UTF-8 from context /Datapool, because request parameters have already been read, or ServletRequest.getReader() has already been called

This is Glassfish specific. Glassfish defaults to ISO-8859-1 and needs to be reconfigured to use UTF-8. See also How to get rid of WARNING: PWC4011: Unable to set request character encoding to UTF-8

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Yeah that was missing. I update now the code below. But i recive an HTTP status 500 that says: WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception java.lang.NullPointerException: Argument Error: Parameter viewId is null – javing Mar 25 '11 at 11:16
  • Please cleanup all old RichFaces/Ajax4jsf/JSF/Facelets libraries from your `/WEB-INF/lib` first. This is a sign of classpath pollution with libraries containing incompatible versioned Facelet viewhandlers. My previous answer as to how to integrate file uploading expects a brand new and clean web project, not an existing project which is polluted. – BalusC Mar 25 '11 at 11:22
  • There are not jars from richfaces in the lib folder at all,Also I just did a project clean from eclipse builded again but still see the warning JSF1029. Where else is there polution? – javing Mar 25 '11 at 11:29
  • Check the entire classpath. Check the *Deployment assembly* in Eclipse project properties. Check the `/JRE/lib` and `/JRE/lib/ext` folders on disk. Check the Glassfish `/lib` folders, etc. I'd suggest to create a brand new project exactly as described on my previous answer, just to be sure. This way you can build on it further. – BalusC Mar 25 '11 at 11:34
  • After a while i decided to create a new clean project. P2 and P3 are solved. But P1 still gives me an error HTTP 500 when i click submit. I will post a second comment with the stack trace – javing Mar 25 '11 at 12:47
  • WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception javax.faces.el.EvaluationException: java.lang.NullPointerException at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102).................... Caused by: java.lang.NullPointerException at managedbeans.UploadController.submit(UploadController.java:27) That has something to do with the file name? – javing Mar 25 '11 at 12:49
  • OK the original project was thus polluted, as I expected. As to the new problem, what exactly is `null` at this line? – BalusC Mar 25 '11 at 12:51
  • String fileName = FilenameUtils.getName(uploadedFile.getName()); – javing Mar 25 '11 at 12:54
  • OK, `uploadedFile` is `null`. Do you have the extensions filter declared in `web.xml`? – BalusC Mar 25 '11 at 12:55
  • My code for the filter at web.xml looks as shown above. What is missing? – javing Mar 25 '11 at 12:56
  • Does the form have an `enctype="multipart/form-data"` attribute? – BalusC Mar 25 '11 at 12:57
  • Yes that was it(I forgot it when i created the new project). The file is inside the database. Thanks for saving me again :) – javing Mar 25 '11 at 13:00