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!