我有几个问题,我正在尝试使用JSF,PrimeFaces(或者如果还有其他方法)和Managed Bean,找到如何将图像保存到数据库中的中间列上的教程知道如果没有JSF和Managed Bean就可以做到这一点,但由于我是新手,我坚持了几件事......我真的不知道如何从我的JSF页面获取图像,转换为字节数组和保存。 请有人开灯。
这是我的所作所为,对不起,但我真的很新,所以请帮助我。
JSF页面:
<?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>
我的实体
@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 "";
}
}
谢谢!
答案 0 :(得分:0)
byte []照片。在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中定义fileupload过滤器
<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>