我正在尝试使用PrimeFaces上传文件,但fileUploadListener
上传完成后不会调用该方法.
这是观点:
<h:form>
<p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload}"
mode="advanced"
update="messages"
sizeLimit="100000"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>
<p:growl id="messages" showDetail="true"/>
</h:form>
Run Code Online (Sandbox Code Playgroud)
还有豆子:
@ManagedBean
@RequestScoped
public class FileUploadController {
public void handleFileUpload(FileUploadEvent event) {
FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
Run Code Online (Sandbox Code Playgroud)
我已经在方法上放置了一个断点,但它从未调用过.当使用mode="simple"
和时ajax="false"
,它已被调用,但我希望它在高级模式下工作.我正在使用Netbeans和Glassfish 3.1.
上传图片时,文件已成功保存并且路径已成功设置.但是在表单提交后不会立即显示上传的图像.仅当我重新加载页面时,才会显示上传的图像.
我正在保存上传的文件,如下所示:
InputStream is;
try {
File file = new File("C:\\****\\*****\\Documents\\NetBeansProjects\\EventsCalendary\\web\\resources\\images\\uploadPhoto.png");
is = event.getFile().getInputstream();
OutputStream os = new FileOutputStream(file);
setUserPhoto("\\EventsCalendary\\resources\\images\\"+file.getName());
byte buf[] = new byte[1024];
int len;
while ((len = is.read(buf)) > 0) {
os.write(buf, 0, len);
}
os.close();
is.close();
} catch (IOException ex) {
System.out.println(ex.getStackTrace());
}
Run Code Online (Sandbox Code Playgroud)
为什么上传的图像仅在重新加载页面后显示,我该如何解决?
我正在使用PHP + MySQL + JavaScript创建一个小型聊天应用程序,我编写了一个函数disonnectUser(),当用户按下断开连接按钮时会调用该函数.这里是:
function disconnectUser(){
$.post('web/WEB-INF/classes/handleChatUser.php',{ action: 'disconnect',nick: localNickname});
$('#chat').stop(true,true).fadeOut(2000,function(){
nicknameDialog();
});
$('#messageInput').val(null);
$('#clientList').html(null);
$('#chatScreen').html(null);
clearInterval(refreshIntervalId);
clearInterval(refreshIntervalId2);
connected = false;
}
Run Code Online (Sandbox Code Playgroud)
它就像一个魅力,但当我在另一个上下文中调用这个函数时,当用户而不是按断开连接只是退出页面时,在这个函数中
$(window).unload(function() {
if(connected){
disconnectUser();
connected = false;
}
});
Run Code Online (Sandbox Code Playgroud)
它不起作用.而且我确定它正在被调用,因为如果我发出警报,它会在关闭页面之前正常调用.我认为在代码完全运行之前页面正在关闭,所以我想如果我在那里放一些块直到代码完成运行它会工作吗?
我正在尝试在JSF上建立一个基本的安全系统,如果用户没有登录并尝试访问受限制的页面,他将被重定向到login.xhtml.这是在servlet过滤器中完成的.
我的问题是当使用resp.sendRedirect("login.xhtml")时; 登录页面丢失所有资源,CSS,脚本等.因此页面显示没有任何样式.
web.xml中
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<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>*.xhtml</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<filter>
<filter-name>restrict</filter-name>
<filter-class>br.com.jetcar.filter.RestrictionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>restrict</filter-name>
<url-pattern>*.xhtml</url-pattern>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
</web-app>
Run Code Online (Sandbox Code Playgroud)
的template.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:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.prime.com.tr/ui"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<h:outputScript library="primefaces" name="jquery/jquery.js"/>
<h:outputStylesheet library="css" name="template.css" />
<h:outputScript name="clock.js" library="js"></h:outputScript>
<title><ui:insert name="title">Bem-Vindo</ui:insert></title>
</h:head>
<body>
<p:growl id="messages"/>
<div id="header"> …
Run Code Online (Sandbox Code Playgroud) jsf ×3
file-upload ×2
primefaces ×2
chat ×1
java-ee ×1
javascript ×1
jsf-2 ×1
refresh ×1
servlets ×1