1

I have a few questions, I'm trying to find a tutorial of how to save a image onto a mediumblob column in my database using JSF, PrimeFaces (or if there is another way to do it) and Managed Bean, I know to do do it without JSF and Managed Bean, but since I'm new on these I stuck on a few things... I really have no idea of how to get the image from my JSF page, transform to a byte array and save it. Please someone give a light.

Here is what I did, I'm sorry but I'm really new, so please help me.

JSF page:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui">

<h:body>

    <h:form>
        <!-- <p:growl id="growl" showDetail="true" sticky="true" /> -->

        <p:messages id="messages" showDetail="true" autoUpdate="true" closable="true" />  
        <h:panelGrid columns="3">
            <h:outputText value="ID : "/>
            <h:inputText id="txtId" name="txtId" value="${produtoMB.prodAtual.id}" readonly="true"/>
            <p:message id="a" for="txtId"/>


            <h:outputText value="Foto:"/>
            <p:fileUpload id="txtFoto" name="txtFoto" value="${produtoMB.prodAtual.foto}" />
            <p:message id="i" for="txtFoto" showSummary="true" showDetail="false"/>



            <h:panelGroup>
                <h:commandButton name="cmd" value="Insert" action="${produtoMB.insert()}"/>

            </h:panelGroup>


        </h:panelGrid>

    </h:form>
</h:body>
</html>

My Entity

@Entity
public class Produto implements Serializable {

    private static final long serialVersionUID = -8914597399554711634L;
    private long id;
    private Byte[] foto;

    @Id
    @GeneratedValue
    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }


    @Lob
    @Column(columnDefinition="mediumblob")
    public Byte[] getFoto() {
        return foto;
    }
    public void setFoto(Byte[] foto) {
        this.foto = foto;
    }



}

Managed Bean

@ManagedBean
@SessionScoped
public class ProdutoMB implements Serializable {

    private static final long serialVersionUID = -3304202810037758438L;

    private Produto prodAtual;

    public ProdutoMB() { 
        prodAtual = new Produto();
        prodDAO = new ProdutoDAOImpl();

    }

    public String insert() { 
        try {
            prodDAO.insert( prodAtual );
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }

}

Thank you!

4

1 回答 1

0

byte[] 照片在 JSF 页面中不可访问。将 ManagedBean 中的 UploadedFile 用于primefaces。

<p:fileUpload id="txtFoto" name="txtFoto" value="${produtoMB.file}" />

UploadedFile 应该是托管 bean 的属性。

@ManagedBean
@SessionScoped
public class ProdutoMB implements Serializable {

private UploadedFile file;

// To save use InputStrem and byte[] from file.

}

还要在 web.xml 中定义文件上传过滤器

<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>
</filter-mapping>
于 2013-06-23T08:21:50.487 回答