我正在使用primfaces 3.5版本来显示数据库中我的数据库中的图像,但不幸的是,实际图像“image”正在写入列,这是我的xhtml文件
<?xml version='1.0' encoding='UTF-8' ?>
<!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:p="http://primefaces.org/ui">
<body>
<ui:composition template="./template.xhtml">
<ui:define name="top">
</ui:define>
<ui:define name="left">
left
</ui:define>
<ui:define name="content">
<h:form id="form1">
<p:panel id="panel" header="New Category">
<p:messages id="msgs1"/>
<h:panelGrid columns="3">
<h:outputLabel for="categoryName" value="Category Name: *" />
<p:inputText id="categoryname" value="#{categoryBean.categoryName}" required="true" label="Categoryname">
<f:validateLength minimum="2" />
</p:inputText>
<p:message for="categoryname" display="icon"/>
<h:outputLabel value="Upload Image" />
<p:fileUpload fileUploadListener="#{categoryBean.upload}"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/" sizeLimit="100000" description="Select Images"/>
</h:panelGrid>
<p:commandButton id="btn" value="Save" update=":form:count" />
</p:panel>
</h:form>
<h:form id="form">
<p:spacer height="20px"></p:spacer>
<p:dataTable id="count" var="category"
value="#{categoryBean.categories}" paginator="true" rows="7">
<f:facet name="header">
Available Categories
</f:facet>
<p:column headerText="Category ID" style="width:4%">
<h:outputText value="#{category.idCat}" />
</p:column>
<p:column headerText="Category Name" style="width:24%">
<h:outputText value="#{category.categoryName}" />
</p:column>
<p:column headerText="Category Image" style="width:24%">
<p:graphicImage alt="image" value="#{category.categoryimg}" cache="false" >
</p:graphicImage>
</p:column>
<p:column style="width:4%">
<p:commandButton value="Delete" action="#{categoryBean.deleteAction(category)}" update=":form:count"/>
</p:column>
</p:dataTable>
</h:form>
</ui:define>
<ui:define name="bottom">
</ui:define>
</ui:composition>
</body>
</html>
这是我的托管bean:
package com.app.beans;
/**
*
* @author
*/
@ManagedBean
@RequestScoped
public class CategoryBean implements Serializable{
public long idCat;
public String categoryName;
public List<Category> categories;
public ItemController itemController;
byte[] data;
private StreamedContent dbImage;
public StreamedContent getDbImage() {
return dbImage;
}
public void setDbImage(StreamedContent dbImage) {
this.dbImage = dbImage;
}
public StreamedContent getImage(Category cat)throws IOException{
FacesContext context = FacesContext.getCurrentInstance();
System.out.println("int get images category id is: "+cat.getCategoryName());
if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
// So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
System.out.println("So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.");
return new DefaultStreamedContent();
}else{
System.out.println("in actual image content");
dbImage= new DefaultStreamedContent(new ByteArrayInputStream(itemController.getImageByName(cat.getCategoryName())), "image/png");
return dbImage;
}
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
Category c = new Category();
c.setCategoryName(categoryName);
int status = itemController.setCategory(c);
if(status == 1){
categories.add(c);
}
}
@PostConstruct
public void init(){
categories = new ArrayList<Category>();
itemController = new ItemController();
categories = itemController.getAllCategories();
}
public long getIdCat() {
return idCat;
}
public void setIdCat(long idCat) {
this.idCat = idCat;
}
public List<Category> getCategories() {
return categories;
}
public void setCategories(List<Category> categories) {
this.categories = categories;
}
/**
* Creates a new instance of CategoryBean
*/
public CategoryBean() {
}
public String deleteAction(Category c) {
System.out.println("from deleteAction"+c.getCategoryName());
int status = itemController.deleteCategory(c.getCategoryName());
if(status == 1){
categories.remove(c);
}
return null;
}
public void upload(FileUploadEvent event) {
FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
// Do what you want with the file
System.out.println("From Upload" + event.getFile().getFileName());
int nRead;
data = new byte[16384];
InputStream is = null;
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
try {
is = event.getFile().getInputstream();
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
} catch (IOException ex) {
Logger.getLogger(UpdateItem.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
我正在获取对应于类别名称的图像,即在日志中出现,我试图理解为什么我的getImage函数的一部分未被调用 这是生成的日志:
WARNING: JSF1091: No mime type could be found for file dynamiccontent. To resolve this, add a mime-type mapping to the applications web.xml.
Jun 07, 2013 11:14:41 PM com.sun.faces.context.ExternalContextImpl getMimeType
WARNING: JSF1091: No mime type could be found for file dynamiccontent. To resolve this, add a mime-type mapping to the applications web.xml.
int get images category id is: xyz
So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
int get images category id is: x1
So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
int get images category id is: x2
So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
int get images category id is: Main Course
So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
int get images category id is: Drinks
So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
Jun 07, 2013 11:14:41 PM org.primefaces.application.PrimeResourceHandler handleResourceRequest
SEVERE: Error in streaming dynamic resource. java.lang.NullPointerException
Jun 07, 2013 11:14:41 PM org.primefaces.application.PrimeResourceHandler handleResourceRequest
SEVERE: Error in streaming dynamic resource. java.lang.NullPointerException
Jun 07, 2013 11:14:41 PM org.primefaces.application.PrimeResourceHandler handleResourceRequest
SEVERE: Error in streaming dynamic resource. java.lang.NullPointerException
Jun 07, 2013 11:14:41 PM org.primefaces.application.PrimeResourceHandler handleResourceRequest
SEVERE: Error in streaming dynamic resource. java.lang.NullPointerException
Jun 07, 2013 11:14:41 PM org.primefaces.application.PrimeResourceHandler handleResourceRequest
SEVERE: Error in streaming dynamic resource. java.lang.NullPointerException
Jun 07, 2013 11:14:42 PM org.primefaces.application.PrimeResourceHandler handleResourceRequest
SEVERE: Error in streaming dynamic resource. Expression cannot be null
Jun 07, 2013 11:14:42 PM org.primefaces.application.PrimeResourceHandler handleResourceRequest
SEVERE: Error in streaming dynamic resource. Expression cannot be null
当评论if else子句时,它会抛出以下错误:
SEVERE: Error Rendering View[/categoryUpdate.xhtml]
java.lang.NullPointerException
at org.primefaces.component.graphicimage.GraphicImageRenderer.getImageSrc(GraphicImageRenderer.java:110)
at org.primefaces.component.graphicimage.GraphicImageRenderer.encodeEnd(GraphicImageRenderer.java:46)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1763)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1759)
at org.primefaces.component.datatable.DataTableRenderer.encodeRegularCell(DataTableRenderer.java:741)
at org.primefaces.component.datatable.DataTableRenderer.encodeRow(DataTableRenderer.java:693)
答案 0 :(得分:4)
到目前为止,您的方法存在两个错误:
您必须使用<f:param>
内的<p:graphicImage>
来传递图片ID。您不能在<p:graphicImage value>
中使用传递的EL 2.2方法参数。图像由完全独立的HTTP请求请求。 EL 2.2方法参数传递仅在生成HTML输出时使用,此时需要为<img src>
生成URL。但是,该URL又必须唯一地标识图像,并且只能通过<f:param>
来完成。
不是技术问题,而是更多的设计问题:你应该将图片流媒体放在一个完全独立的应用程序范围的bean中,而不是将它与&#34;常规&#34;托管bean代码。通过这种方式,图像流可以重复使用,您可以清楚地分离使代码更易于理解和维护的问题。