I'm trying to implement a page with primefaces fileUpload but my handleUpload function isn't triggered.
My xhtml:
<h:form enctype="multipart/form-data">
<p:panel id="uploadFormPanel" header="File upload">
<h:panelGrid columns="2" style="margin-bottom:10px" cellpadding="5">
<h:outputText value="#{criteriaTranslation.questionaireFile}:" />
<p:fileUpload update="uploadMessages"
fileUploadListener="#{critereCSVImporter.handleFileUpload}"
mode="advanced"
allowTypes="/(\.|\/)(csv)$/"/>
</h:panelGrid>
<p:growl id="uploadMessages" showDetail="true"/>
</p:panel>
</h:form>
My backing bean:
#ManagedBean
#ViewScoped
public class CritereCSVImporter {
#Inject
private CriteriaBL criteriaBL;
private String OCRMODE;
public void handleFileUpload(FileUploadEvent event) {
System.out.println("handle");
InputStream inputStream = event.getFile().getInputstream();
}
My web.xml:
<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>
Am I missing something?
Place commons-fileupload and commons-io jars in your lib folder
Apache Commons FileUpload
Apache Commons IO
Related
I cannot make the fileUpload option working. I keep having the message file is null.
Could you tell me how to fix this ?
here is my xhtml:
<h:form enctype="multipart/form-data">
<p:fileUpload mode="simple" id="recupereFile" value="#{FileUploadView.uploadedFile}" />
<p:commandButton value="Upload" action="#{FileUploadView.upload}" ajax="false" />
</h:form>
here is my java:
#ManagedBean(name="FileUploadView")
public class FileUploadView {
private UploadedFile file;
public UploadedFile getFile() {
System.out.println(file);
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
public void upload() {
UploadedFile file = getFile();
System.out.println("hello");
System.out.println(file);
if(file != null) {
FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, message);
System.out.println(message);
}
}
}
file is always null !
my web.xml
<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>
I have these jars:
commons-fileupload-1.3.1.jar
commons-io_2.5.jar
Thansk for your help.
We had a similar issue a few month ago here. The colleague who did this left our company, so I can't ask him. Anyway this is how the relevant parts of my web.xml look like.
<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>
<url-pattern>*.jsf</url-pattern>
</filter-mapping>
Also we have this context-param:
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>auto</param-value>
</context-param>
The value attribute of p:fileUpload should correspond to the name of the UploadedFile in the bean.
It should work if you replace FileUploadView.uploadedFile with FileUploadView.file in the p:fileUpload:
<p:fileUpload mode="simple" id="recupereFile" value="#{FileUploadView.file}" />
I have an issue after uploading an image. the image is not refreshed upon upload. The bean is session scoped, and after upload I need to refresh manually the page to see the new image. How can I refresh the graphical Image automatically after image/file upload? thank you
The following code could successfully upload the image but the previous image still visible and the new image could be visible only after refresh:
Platforms:
jsf 2.2
,tomcat 7.37
, primefaces 5.0
<h:form id="form" enctype="multipart/form-data">
<p:growl id="growl_1" showDetail="true"/>
<p:panelGrid id="pg_photo"
style="margin-bottom:10px;
width: 50em"
styleClass="panelGridCenter">
<f:facet name="header">
<p:row>
<p:column>
<p:graphicImage
id="gi_etud"
width="150"
height="120"
value="#{etudiantProfilView.imageEtudAsStream}"/>
</p:column>
<p:column>
<p:fileUpload id="fu_photo"
value="#{etudiantProfilView.imageEtudUF}"
mode="advanced"
dragDropSupport="true"
fileLimit="10"
sizeLimit="100000"
allowTypes="/(\.|\/)(gif|jpg|jpeg|gif|png|PNG|GIF|JPG|JPEG)$/"
auto="true"
update="growl_1 gi_etud"
fileUploadListener="#{etudiantProfilView.imageUploadListener}"
>
</p:fileUpload>
</p:column>
</p:row>
</f:facet>
</p:panelGrid>
</h:form>
The session bean:
#ManagedBean
#SessionScoped
public class EtudiantProfilView implements Serializable {
//some stuff
public void imageUploadListener(FileUploadEvent event) throws IOException {
//save the image to the hard disk
}
//some stuff
}
I have resolved the refresh issue by adding "reload" action to "oncomplete" attribute for uploadFile component:
<p:fileUpload id="fu_photo" value="#{etudiantProfilView.imageEtudUF}"
mode="advanced"
dragDropSupport="true"
fileLimit="10"
sizeLimit="200000"
allowTypes="/(\.|\/)(gif|jpg|jpeg|gif|png|PNG|GIF|JPG|JPEG)$/"
auto="true"
update="#form"
oncomplete="window.location.reload();"
fileUploadListener="#{etudiantProfilView.imageUploadListener}"
>
</p:fileUpload>
I'm developing a web application with PrimeFaces 4.0.RC1 and i have problem with file upload,
exactly with the fileUploadListener; this listener doesn't invoke the method "handleFileUpload".
When i try to invoke this method with commandButton: the action in the commandButton work fine;
this is a pert of my web.xml:
<!-- Filter for the JSF uploder -->
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>commons</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>facesServlet</servlet-name>
</filter-mapping>
this my xhtml page:
<h:form enctype="multipart/form-data">
<p:growl id="kpiUploadNote" showDetail="true" sticky="true" />
<!-- Test with advanced fileUpload -->
<p:fileUpload
fileUploadListener="#{fileUploadController.handleFileUpload}"
mode="advanced" dragDropSupport="false" sizeLimit="100000"
update="kpiUploadNote" fileLimit="3" allowTypes="/(\.|\/)(csv|xlsx)$/" />
<!-- Test with commandButton to test the bean -->
<p:commandButton action="#{fileUploadController.handleFileUpload}"
update="kpiUploadNote" ajax="false" />
</h:form>
this my ManagedBean:
#Scope
#ManagedBean(name = "fileUploadController")
public class FileUploadController {
public void handleFileUpload(FileUploadEvent event) {
System.out.println("calling file upload...");
FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
public void handleFileUpload() {
FacesMessage msg = new FacesMessage("Succesful", " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
This my first Question in stack overflow and i hope you help me :),
Hi I have a primefaces tabView looks like this
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body>
<p:messages />
<h:form id="form">
<p:tabView dynamic="true">
<p:tab title="Tab">
<p:inputText required="true" value="value"></p:inputText>
</p:tab>
<p:tab title="Select">
<p:selectOneMenu value="#{dummyController.selectedValue}" id="select" required="true" requiredMessage="Select is required">
<f:selectItem itemValue="1" itemLabel="asd"></f:selectItem>
<f:selectItem itemValue="2" itemLabel="qwe"></f:selectItem>
<f:selectItem itemValue="3" itemLabel="zc"></f:selectItem>
</p:selectOneMenu>
<p:message for="select" />
</p:tab>
<p:tab title="Tab">
<p:inputText required="true" value="value"></p:inputText>
</p:tab>
</p:tabView>
<h:commandButton action="#{dummyController.submit}" />
</h:form>
</h:body>
</ui:composition>
and it's controller
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
#ManagedBean
#ViewScoped
public class DummyController implements Serializable {
private static final long serialVersionUID = 1L;
private int selectedValue;
public void submit() {
}
public int getSelectedValue() {
return selectedValue;
}
public void setSelectedValue(int selectedValue) {
this.selectedValue = selectedValue;
}
}
it has a strange behaviour, follow the steps tp reproduce:
open the Select tab
open other tab
press Submit twice
the first press nothing happens as regular, the next press triggers required message for the select, though it always has a value
Please tell if something is missing or if there are any solutions
There's no direct solution to this, it's a bug in primefaces tabView, I came with this workaround and worked
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body>
<p:messages />
<h:form id="form">
<p:tabView dynamic="true" activeIndex="#{dummyController.activeindex}" >
<p:tab title="Tab" id="tab1">
<p:inputText required="true" value="value"></p:inputText>
</p:tab>
<p:tab title="Select" id="selectTab">
<p:selectOneMenu disabled="#{dummyController.activeindex != 1}" value="#{dummyController.selectedValue}" id="select" required="true" requiredMessage="Select is required">
<f:selectItem itemValue="" itemLabel=""></f:selectItem>
<f:selectItem itemValue="1" itemLabel="asd"></f:selectItem>
<f:selectItem itemValue="2" itemLabel="qwe"></f:selectItem>
<f:selectItem itemValue="3" itemLabel="zc"></f:selectItem>
</p:selectOneMenu>
<p:message for="select" />
</p:tab>
<p:tab title="Tab" id="tab3">
<p:inputText required="true" value="value"></p:inputText>
</p:tab>
</p:tabView>
<h:commandButton action="#{dummyController.submit}" />
</h:form>
</h:body>
</ui:composition>
and the controller :
package com.ibm.sa.kap.ui.controller;
import java.io.Serializable;
#ManagedBean
#ViewScoped
public class DummyController implements Serializable {
private static final long serialVersionUID = 1L;
private int selectedValue;
private int activeindex;
public void submit() {
}
public int getSelectedValue() {
return selectedValue;
}
public void setSelectedValue(int selectedValue) {
this.selectedValue = selectedValue;
}
public int getActiveindex() {
return activeindex;
}
public void setActiveindex(int activeindex) {
this.activeindex = activeindex;
}
}
it's a conditional disabled according to the tab index, so that to prevent the tabview from resetting the value, how dirty !!
Unfortunatelly, the implementation of p:tabView with dynamic="true" is buggy. There are various issues: http://code.google.com/p/primefaces/issues/list?can=2&q=tabView+dynamic&colspec=ID+Type+Status+Priority+TargetVersion+Reporter+Owner+Summary&y=5000&cells=tiles but the most affected are the components such as p:selectOneMenu.
I've had this issue in my own project - the values from select lists were not submitted, if they were on the other tab as active. The solution is - don't use dynamic tabs, as long as they won't be fixed. There are too many bugs within.
Another thing that doesn't work, is to update the tab view from the ajax event onTabChange.
Because <p:selectOneMenu needs value to save element choosen.
I am making simple login page and trying to validate it using struts ValidatorForm but its not working. But same code worked for DynaValidatorForm. Not able to understand what's problem.
It is not showing any error when I click login button.
Here is my code.
login.jsp
<body>
<div style="color:red">
<html:errors />
</div>
<html:form action="/Login" >
User Name : <html:text name="LoginForm" property="username" /> <br>
Password : <html:password name="LoginForm" property="password" /> <br>
<html:submit value="Login" />
</html:form>
</body>
LoginAction.java
public class LoginAction extends Action
{ public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
LoginForm loginForm=(LoginForm) form;
String userName = loginForm.getUsername();
String password = loginForm.getPassword();
if(userName.equals("sumeet") )
{
return mapping.findForward("success");
}
else
{
return mapping.findForward("failure");
}
struts.config
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
<form-beans>
<form-bean name="LoginForm" type="com.ibm.Forms.LoginForm" >
</form-bean>
</form-beans>
<global-exceptions>
</global-exceptions>
<global-forwards>
</global-forwards>
<action-mappings>
<action name="LoginForm" path="/Login" scope="session" input="/login.jsp" type="com.ibm.Action.LoginAction" cancellable="true" validate="true">
<forward name="success" path="/success.jsp"/>
</action>
</action-mappings>
<message-resources parameter="test2.resources.ApplicationResources"/>
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
valdiation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN" "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd" >
<form-validation>
<formset>
<form name="LoginForm">
<field property="username" depends="required">
</field>
<field property="password" depends="required,minlength">
<arg1 key="${var:minlength}" name="minlength" resource="false"/>
<var>
<var-name>minlength</var-name>
<var-value>6</var-value>
</var>
</field>
</form>
</formset>
</form-validation>
Thank you.
You are missing the .do in the action attribute of the html form. You don't need the name attribute in both inputs.
<html:form action="/Login.do" >
User Name : <html:text property="username" /> <br>
Password : <html:password property="password" /> <br>
<html:submit value="Login" />
</html:form>