我正在使用 primfaces 3.5 版本在数据表中显示来自我的数据库的图像,但不幸的是,不是实际图像“图像”被写入列中,这是我的 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)