0

我的文件上传有一个奇怪的问题

永远不会调用handleFileUpload,并且在xhtml文件中它用黄色强调它并说unkown property

这是我的 web.xml

<context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>server</param-value>
    </context-param>
    <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>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    </servlet>

我的文件上传控制器,我回去看看能不能调用最基本的版本,但目前还不行

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package richard.fileupload;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

import org.primefaces.event.FileUploadEvent;

@ManagedBean(name = "fileUploadController")
public class FileUploadController {


     public void handleFileUpload(FileUploadEvent event) {
     System.out.println("called");
     FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
     FacesContext.getCurrentInstance().addMessage(null, msg);
     }
     }
      /*
    private String username;

    @PostConstruct
    public void init() {
        System.out.println("called get username");
        username = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser();
    }
    private String destination = "C:/Users/Richard/printing~subversion/fileupload/web/WEB-INF/uploaded/"; // main location for uploads
    File theFile = new File(destination + username); // will create a sub folder for each user (currently does not work, below hopefully is a solution) 

    public File getDirectory(String destination, String username) {
             System.out.println("called get directory");
        // currently not working, is not calling the username or destination 
        //set the user directory from the destinarion and the logged user name
        File directory = new File(destination, username);
        //check if the location exists
        if (!directory.exists()) {
            //let's try to create it
            try {
                directory.mkdir();
            } catch (SecurityException secEx) {
                //handle the exception
                secEx.printStackTrace(System.out);
                directory = null;
            }
        }
        return directory;
    }

    public void handleFileUpload(FileUploadEvent event) {
             System.out.println("called handle file");
        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);
        try {
            copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
        } catch (IOException e) {
            //handle the exception
            e.printStackTrace();
        }
    }

    public void copyFile(String fileName, InputStream in) {
        try {


            // write the inputStream to a FileOutputStream
            OutputStream out = new FileOutputStream(new File(theFile + "/" + fileName)); // cannot find path when adding username atm
            System.out.println(theFile); //testing 

            int read = 0;
            byte[] bytes = new byte[1024];

            while ((read = in.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }

            in.close();
            out.flush();
            out.close();
//make sure new file is created, (displays in glassfish server console not to end user)
            System.out.println("New file created!");
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
*/

这是我的uploadText.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:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui"
      xmlns:corejsf="http://corejsf.com"> 
    <h:outputStylesheet name="css/testcss.css" />
    <h:head>
        <title>Print to Uni</title>
        <meta http-equiv="content-type" content="text/html; charset=windows-1252" />
        <!-- <link rel="stylesheet" type="text/css" href="style.css" title="style" /> -->
    </h:head>
    <body>
        <div id="main">
            <div id="header">

                <div id="logo">
                    <div id="logo_text">
                        <h1><a href="/GUI/index.xhtml">Remote<span class="logo_colour">Printing</span></a></h1>
                        <h2>This is a web app that allows users to print to University</h2>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <div id="site_content">
        <div id="content">
            <h:body>
                <h:form enctype="multipart/form-data">
                    Upload a text file:
                    <h:form>
                        <!--<p:growl id="messages" showSummary="true" showDetail="true" /> -->
                        <br></br>
                        <br></br>

                        <p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload}"
                                      mode="advanced" 
                                      update="messages"
                                      sizeLimit="100000000" 
                                      allowTypes="/(\.|\/)(gif|jpe?g|png|doc|docx|txt|pdf)$/"
                                      auto="true"/>
                        <p:growl id="messages" showDetail="true"/>
                    </h:form>
                    <br></br>
                    <br></br>
                    <p:commandButton value="Submit" action="/GUI/submittedText" icon ="ui-icon-arrowstop-1-n"/>
                    <div class="divider"/>
                    <p:commandButton  value="Homepage" action="#{userBean.buttonHome}" icon="ui-icon-home"/>   
                </h:form>
            </h:body>
        </div>
    </div>
</html>

我的库中需要两个公共文件,这是什么原因造成的?我确实让它工作但没有备份更改,现在它不起作用

4

1 回答 1

3

您的 HTML 在语法上无效。其中,您有多个<body>标签并且您正在嵌套<form>元素。JSF 看起来像个魔术师,但如果强迫它生成语法上无效的 HTML,那么它就无能为力了。当涉及到语法无效的 HTML 时,浏览器的行为是未指定的。嵌套表单通常是浏览器没有向服务器提交预期数据的原因,因此服务器最终什么也没有收到。

您的 HTML 应该只有一个<body>标记(在 JSF 术语中,因此只有一个<h:body>)。你的 HTML 也不应该有嵌套<form>元素(在 JSF 术语中,你不应该<h:form>相互嵌套)。在浏览器中打开页面,右键单击并查看源代码。将其复制粘贴到前面链接的 W3 验证器中并单独修复问题。

除此之外,您的托管 bean 没有分配范围注释。每次在 EL 中引用它时都会@NoneScoped重新创建它,从而导致模型属性和操作在完全独立的实例中设置和调用。至少做到@ViewScoped

于 2013-02-01T01:03:40.903 回答