我的文件上传存在一个奇怪的问题
永远不会调用handleFileUpload,并且在xhtml文件中,它用黄色加下划线并表示未知属性
这是我的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>
我的库中需要两个公共文件,导致这种情况的原因是什么?我确实有它工作,但没有备份更改,现在它不起作用
答案 0 :(得分:3)
您的HTML为syntactically invalid。除此之外,您还有多个<body>
标记,并且您正在嵌套<form>
个元素。 JSF看起来像魔术师,但是如果你迫使它生成语法无效的HTML,那么它对你来说无济于事。当涉及语法无效的HTML时,浏览器行为未指定。嵌套表单通常是浏览器不向服务器提交预期数据的原因,因此服务器最终不会收到任何内容。
您的HTML应该只有一个<body>
标记(在JSF术语中,因此只有一个<h:body>
)。您的HTML也应该没有嵌套的<form>
元素(在JSF术语中,您不应该相互嵌套<h:form>
)。在浏览器中打开页面,右键单击查看源。将其密封到上面提到的W3验证器中并单独解决问题。
除此之外,您的托管bean没有分配范围注释。它的行为类似于@NoneScoped
,因此每次在EL中引用时都会重新创建它,从而导致在完全独立的实例中设置和调用模型属性和操作。至少要@ViewScoped
。