0

在 RichFaces 中,为了上传我使用的文件rich:fileUpload。通过使用rich:fileUpload添加、上传和清除所有按钮会在richfaces liveemo 示例中自动生成,但对我来说,添加、上传和清除所有按钮在我的浏览器中显示为文本,我应该在哪里更改以便添加、上传和清除所有可以点击。我是否必须更改web.xml/pom.xml文件中的任何设置。

我正在使用 RichFaces 4 和 JSF 2。

这是我的 web.xml :

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>Sample RichFaces 4 Application</display-name>
<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>
<context-param>
    <param-name>javax.faces.SKIP_COMMENTS</param-name>
    <param-value>true</param-value>
</context-param>
<context-param>
    <param-name>org.richfaces.skin</param-name>
    <param-value>#{skinBean.skin}</param-value>
</context-param>
<context-param>
    <param-name>org.richfaces.fileUpload.maxRequestSize</param-name>
    <param-value>1000000000</param-value>
</context-param>
<context-param>
    <param-name>org.richfaces.fileUpload.createTempFiles</param-name>
    <param-value>true</param-value>
</context-param>
<listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>30</session-timeout>
</session-config>
<mime-mapping>
    <extension>ecss</extension>
    <mime-type>text/css</mime-type>
</mime-mapping>
<welcome-file-list>
    <welcome-file>index.jsf</welcome-file> 
</welcome-file-list>
<login-config>
    <auth-method>BASIC</auth-method>
</login-config>

嗨,我现在可以通过简单地提供 h:head 和 h:body 标签来正确获取这些添加和更新按钮,但是我得到了这个异常 javax.faces.event.AbortProcessingException 并且文件没有被上传,如提供的示例中所示链接“文件上传示例”。任何人都可以帮助我解决这个问题

这是我的 xhtml 页面

 <!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:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:a4j="http://richfaces.org/a4j" xmlns:rich="http://richfaces.org/rich">



<ui:composition>
 <h:head>
 <h:outputStylesheet>
    .top {
    vertical-align: top;
    }
.info {
    height: 202px;
    overflow: auto;
    }
</h:outputStylesheet>
</h:head>
<h:body>
<h:form>
    <h:panelGrid columns="2" columnClasses="top,top">
        <rich:fileUpload fileUploadListener="#{fileUploadBean.listener}" id="upload" acceptedTypes="jpg, gif, png, bmp"
            ontyperejected="alert('Only JPG, GIF, PNG and BMP files are accepted');" maxFilesQuantity="5">
            <a4j:ajax event="uploadcomplete" execute="@none" render="info" />
        </rich:fileUpload>
        <h:panelGrid id="info" layout="block">
            <rich:panel bodyClass="info">
                <f:facet name="header">
                    <h:outputText value="Uploaded Files Info" />
                </f:facet>
                <h:outputText value="No files currently uploaded" rendered="#{fileUploadBean.size==0}" />
                <rich:dataGrid columns="1" value="#{fileUploadBean.files}" var="file" rowKeyVar="row">
                    <rich:panel bodyClass="rich-laguna-panel-no-header">
                        <h:panelGrid columns="2">
                            <a4j:mediaOutput element="img" mimeType="image/jpeg" createContent="#{fileUploadBean.paint}"
                                value="#{row}" style="width:100px; height:100px;" cacheable="false">
                                <f:param value="#{fileUploadBean.timeStamp}" name="time" />
                            </a4j:mediaOutput>
                            <h:panelGrid columns="2">
                                <h:outputText value="File Name:" />
                                <h:outputText value="#{file.name}" />
                                <h:outputText value="File Length(bytes):" />
                                <h:outputText value="#{file.length}" />
                            </h:panelGrid>
                        </h:panelGrid>
                    </rich:panel>
                </rich:dataGrid>
            </rich:panel>
            <br/>
            <a4j:commandButton action="#{fileUploadBean.clearUploadData}" render="info, upload" value="Clear Uploaded Data"
                rendered="#{fileUploadBean.size>0}" />
        </h:panelGrid>
    </h:panelGrid>
</h:form>
</h:body>
</ui:composition>
</html> 

这是我的 FileUploadBean 类

package com.acc.upload;

import java.io.IOException;

import java.io.OutputStream;
import java.util.ArrayList;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import com.acc.fileupload.utils.FileUploadEvent;
import com.acc.fileupload.utils.UploadedFile;


@ManagedBean(name = "fileUploadBean")
@SessionScoped
public class FileUploadBean{

private ArrayList<File> files = new ArrayList<File>();
private int uploadsAvailable = 5;
private boolean autoUpload = false;
private boolean useFlash = true;


public int getSize() {
    if (getFiles().size()>0){
        return getFiles().size();
    }else 
    {
        return 0;
    }
}

public FileUploadBean() {
}

public void paint(OutputStream stream, Object object) throws IOException {
    stream.write(getFiles().get((Integer)object).getData());
}
public void listener(FileUploadEvent event) throws Exception{
    UploadedFile item = event.getUploadedFile();
    File file = new File();
    file.setLength(item.getData().length);
    file.setName(item.getName());
    file.setData(item.getData());
    files.add(file);
    uploadsAvailable--;
}  

public String clearUploadData() {
    files.clear();
    setUploadsAvailable(5);
    return null;
}

public long getTimeStamp(){
    return System.currentTimeMillis();
}

public ArrayList<File> getFiles() {
    return files;
}

public void setFiles(ArrayList<File> files) { 
    this.files = files;
}

public int getUploadsAvailable() {
    return uploadsAvailable;
}

public void setUploadsAvailable(int uploadsAvailable) {
    this.uploadsAvailable = uploadsAvailable;
}

public boolean isAutoUpload() {
    return autoUpload;
}

public void setAutoUpload(boolean autoUpload) {
    this.autoUpload = autoUpload;
}

public boolean isUseFlash() {
    return useFlash;
}

public void setUseFlash(boolean useFlash) {
    this.useFlash = useFlash;
}

}

文件类

package com.acc.upload;

public class File {

private String Name;
private String mime;
private long length;
private byte[] data;
public byte[] getData() {
    return data;
}
public void setData(byte[] data) {
    this.data = data;
}
public String getName() {
    return Name;
}
public void setName(String name) {
    Name = name;
    int extDot = name.lastIndexOf('.');
    if(extDot > 0){
        String extension = name.substring(extDot +1);
        if("png".equals(extension)){
            mime="image/png";
        }else {
            mime = "img/unknown";
        }
    }
}
public long getLength() {
    return length;
}
public void setLength(long length) {
    this.length = length;
}

public String getMime(){
    return mime;
}
}

非常感谢。现在这个错误对我来说已经消失了。我在 web.xml 中将 createTemp 文件设置为 true,但我无法在本地系统的 temp 文件夹中找到上传的文件。也可以通过更改任何文件将上传的文件存储到本地系统中的所需位置web.xml 文件中的设置?请帮助解决这两个问题。

4

2 回答 2

0

关于你的按钮图标问题,我很确定你不会自己管理 RichFaces 的皮肤,所以你可以从你的web.xml

<context-param>
    <param-name>org.richfaces.skin</param-name>
    <param-value>#{skinBean.skin}</param-value>
</context-param>

RichFaces 现在将使用默认皮肤选项。

根据您的评论,您缺少 h:head 这是强制性的,以便为 RichFaces 包含必要的 CSS 和 JavaScript 资源。

此外,您的最后一个问题与您的进口不良有关:

import com.acc.fileupload.utils.FileUploadEvent;
import com.acc.fileupload.utils.UploadedFile;

应替换为:

import org.richfaces.event.FileUploadEvent;
import org.richfaces.model.UploadedFile;

其他信息:

您所基于的示例来自RichFaces 3.3.3(这显示在页脚中)。使用RichFaces 4.3.2的示例仔细检查您的视图代码。

于 2013-05-28T07:28:19.930 回答
0

我也遇到了 Richface 4.0.0 和 spring 4 的这个问题。上传文件时,我总是收到此错误消息“无法读取请求序言”。

我通过以下方式解决了这个问题:将多部分解析器设置为 DispatcherServlet 而不是 CommonsMultipartResolver。因为spring的CommonsMultipartResolver和richface 4.0.0不一致

希望这有帮助!

于 2015-07-17T03:34:16.507 回答