Can't upload file & validation message not appear - jsf

i'm new in java & jsf framework. I have a situation and make me realy confused. I'm trying to create upload form in jsf, the backing bean is request scoped, user must login for using this form. when I test to submit form, the validation message its not appear, the form just refresh without any validation/required message. I try to find the answer in google or stackoverflow but it not works. However i can upload my files when i try create simple application like my form below, without login function.
My form is look like this:
<h:form id="frMember" enctype="multipart/form-data">
<h:inputText id="username" value="#{memberForm.email}" required="true" requiredMessage="username_required_message" pt:placeholder="username"/><br/><br/>
<h:inputFile value="#{memberForm.fileFoto}" required="true" requiredMessage="file_required_message"/><br/>
<h:commandButton value="upload" action="#{memberForm.upload()}" class="btn btn-danger"/>
</h:form>
I'm using glassfish 4, JSF 2.2.7, Netbeans 8 and i'm not used 3rd party like tomahawk fileupload. I also have try googling and searching through stackoverflow for solving my problem, but still i can't solved it.
updated
this is my controller
public class MemberFormView implements Serializable {
private Part fileFoto;
/**
* Creates a new instance of MemberFormView
*/
public MemberFormView() {
}
public void upload() throws IOException {
String fileName = FilenameUtils.getName(fileFoto.getName());
String contentType = fileFoto.getContentType();
try (InputStream input = fileFoto.getInputStream()) {
Files.copy(input, new File("/var/AppFile/tmp/", fileFoto.getName()).toPath());
}
}
}
and this is my faces-config.xml
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
<managed-bean>
<managed-bean-name>memberForm</managed-bean-name>
<managed-bean-class>controller.member.MemberFormView</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>

Related

PrimeFaces actionListener not calling bean

I am trying to mimic the PrimeFaces Dialog example. For some reason that I am not able to find, my PrimeFaces button does not seem to call the required managed bean method:
<h:form>
<p:commandButton value="Open" icon="ui-icon-extlink" actionListener="#{myController.createDialog()}" />
</h:form>
Managed bean:
#Named(value = "myController")
#ViewScoped
public class MyController implements Serializable {
public void createDialog() {
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%");
...
The print statement is never executed, like if the Listener was not working. When I click the button, no response is given. No backend error, no JS error, nothing. I only see that some request is done because I log when a user passes the authorization layer. So something happens but seems to fail silently.
What I have tried:
Move the button to other places in the page
Use an id:
<p:commandButton id="ex" value="Open" icon="ui-icon-extlink" actionListener="#{myController.createDialog()}" />
<h:message for="ex" />
Remove the ViewScoped
Require a javax.faces.event.ActionEvent in the method
public void createDialog(ActionEvent event) {
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%");
Change the method signature
action="#{myController.createDialog(5)}"
and
public void createDialog(int s) {
System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%");
I even tried to create a WEB-INF/faces-config.xml (which I would prefer not to, and according to PrimeFaces documentation I do not need it) with:
<?xml version='1.0' encoding='UTF-8'?>
<faces-config version="2.2"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
<application>
<action-listener>org.primefaces.application.DialogActionListener</action-listener>
<navigation-handler>org.primefaces.application.DialogNavigationHandler</navigation-handler>
<view-handler>org.primefaces.application.DialogViewHandler</view-handler>
</application>
<lifecycle>
<phase-listener>org.primefaces.component.lifecycle.LifecyclePhaseListener</phase-listener>
</lifecycle>
Other answers I have checked are: 1, 2, 3, 4, 5, 6
Apparently the use of actionListeners in PrimeFaces is not as correct as it should be... Is there another way to use PrimeFaces components?
This Dialog Framework code works for me:
index.xhtml
<p:commandButton id="openDialogButton" value="open dialog" action="#{myBean.openDialog('origin')}">
<!-- dialogReturn event: data could be passed, see page 587 in PF 6.1 manual. -->
<p:ajax event="dialogReturn" listener="#{myBean.doSthOnDialogReturn}"/>
</p:commandButton>
myDialogPage.xhtml
<p:commandButton id="closeButtonDialog" value="close dialog"
action="#{myBean.closeDialog('false')}"/>
<p:commandButton id="closeButtonDialog2" value="close dialog 2"
action="#{myBean.closeDialog('true')}"
ajax="false" validateClient="true"/>
Note: Example shows how to pass parameter from dialog. You can also notice, that the first button does just return while the second one does validation at first. I think these things can be useful.
myBean.java
public void openDialog(String origin) {
RequestContext.getCurrentInstance().openDialog("myDialogPage",
options, null);
}
public void closeDialog(Boolean param) {
RequestContext.getCurrentInstance().closeDialog(param);
}
public void doSthOnDialogReturn(SelectEvent event) {
if ((Boolean) event.getObject()) { // retrieve param value
doSth();
}
}
WEB-INF/faces-config.xml
<application>
<action-listener>
org.primefaces.application.DialogActionListener
</action-listener>
<navigation-handler>
org.primefaces.application.DialogNavigationHandler
</navigation-handler>
<view-handler>
org.primefaces.application.DialogViewHandler
</view-handler>
</application>
Finally I got to see some error:
SEVERE - /page.xhtml #159,146 actionListener="#{myController.createDialog()}": Target Unreachable, identifier 'myController' resolved to null
The trick to enable the display of error messages was to add the following in faces-config.xml:
<factory>
<exception-handler-factory>org.primefaces.application.exceptionhandler.PrimeExceptionHandlerFactory</exception-handler-factory>
</factory>
After checking this, I realised that my beans.xml file had been somehow deleted (??)

EJB annotation doesnt work in JSF

i'm starting with JEE and i need to work with JSF-EJB-JPA.
I have an EJB project and it deploys ok, my war file too. When i use the annotation EJB to in a properti in the war, it simply doesnt work, no information is show
I have an h:dataTable and i want to populate it with info that comes from a database, injeting an EJB, but the data just not show (i dont know if it didnt arrive to the WAR or only it isnt being showed). The table is empty, only with the headers that i (of course) wrote directly.
if i dont use #EJB anywhere, the data that doesnt come from the EJB is showed correctly, for example, if i write "Hello world" i cant print it in , but when i dare to use #EJB even that information stop working, as if using #EJB broke all the conection between the xhtml and the manager.
i followed a tutorial, and in the video everything went good.
a part of the controller:
import javax.inject.Named;
import javax.inject.Inject;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import javax.ejb.EJB;
#Named(value = "editorialController")
#SessionScoped
public class EditorialController implements Serializable {
#EJB//(name=Tutorial-ejb")
private EditorialFacade editorialFacade;
private Integer randomInt;
public List<Editorial> findAll()
{
return this.editorialFacade.findAll();
}
public Integer getRandomInt() {
return randomInt;
}
public void setRandomInt(Integer randomInt) {
this.randomInt = randomInt;
}
}
a pice of the xhtml
<h:body>
Hello from Facelets
<h:outputLabel value="#{editorialController.randomInt}"></h:outputLabel>
<h:form>
<h:dataTable var="p" value="#{editorialController.findAll()}">
<h:column>
<f:facet name="Header">
<h:outputText value="NAME"></h:outputText>
</f:facet>
<h:outputText value="#{p.nombre}"></h:outputText>
</h:column>
</h:dataTable>
</h:form>
</h:body>
i tried with: #{editorialController.findAll()} and #{editorialController.findAll}
For the EJB, are as usual: a package for entities, other for facades; netbeans generated them, so i think its not necesary copy&paste the code.
im using glassfish (payara). web.xml has all the JSF stuff.
the persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="Tutorial-ejbPU" transaction-type="JTA">
<jta-data-source>jdbc/MyDataSource</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>

JSF action not triggered

I am trying to do a simple test before I dive into a large activity. But, here is where I was stuck. The test is to submit the JSF form, but the managed bean action never gets triggered.
<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:f="http://java.sun.com/jsf/core"
xmlns:ice="http://www.icesoft.com/icefaces/component">
<f:view>
<head>
<title>A B C</title>
</head>
<body>
<h:form id="test">
<h:inputText value="demo"/>
<h:commandButton type="submit" value="A button" action="#{User.better}" immediate="true" />
</h:form>
</body>
</f:view>
</html>
Here is my managed bean
public class User {
public String send() {
System.out.println("Submitting data.....");
return null;
}
public void better() {
System.out.println("In better...");
}
}
I have set all the configurations correctly. I could be able to see the page. But,the control never gets into action method. How come? Any suggestions would be great.
UPDATE:
Here is my faces-conig.xml
<?xml version="1.0" encoding="UTF-8"?>
<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xi="http://www.w3.org/2001/XInclude" 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-facesconfig_1_2.xsd">
<application>
<view-handler>com.icesoft.faces.facelets.D2DFaceletViewHandler</view-handler>
</application>
<managed-bean>
<managed-bean-name>User</managed-bean-name>
<managed-bean-class>com.srk.beans.User</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>
I have changed the managed-bean-name from User to user(small case) and changed the same in the .xhtml page as well, but seems to be same problem still.
Try user instead of User.Thus, try put user.better instead of User.better. Are you using jsf 2 or no? Also, post your faces-config file
Support for an action method with a void return type did not show up in JSF until v2.x. You must specify a return type of at least Object for better
public Object better() {
System.out.println("In better...");
return null;
}

How to implement jsf validator?

HI,
I want to know how to implement Validator in JSF. What is the advantages of declaring the validator-id. When it will be called in the life cycle?. I have implemented the following code. Please find out what is wrong in the code. I am not seeing it called anywhere in the life cycle.
<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
"http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<faces-config>
<lifecycle>
<phase-listener>javabeat.net.jsf.JsfPhaseListener</phase-listener>
</lifecycle>
<validator>
<validator-id>JsfValidator</validator-id>
<validator-class>javabeat.net.jsf.JsfValidator</validator-class>
</validator>
<managed-bean>
<managed-bean-name>jsfBean</managed-bean-name>
<managed-bean-class>javabeat.net.beans.ManagedBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<navigation-rule>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>success.jsp</to-view-id>
</navigation-case>
</navigation-rule>
</faces-config>
public class JsfValidator implements Validator {
public JsfValidator()
{
System.out.println("Inside JsfValidator Constructor");
}
#Override
public void validate(FacesContext facesContext,
UIComponent uiComponent,
Object object)
throws ValidatorException {
System.out.println("Inside Validator");
}
}
You have to tie this custom validator to some jsf component on the jsf page, and that is where the vaidator-id you have defined is used. for e.g your jsf page might look something like this
<h:inputText id="myInput" value="#{mybean.property}">
<f:validator validatorId="JsfValidator"/>
</h:inputText>
If you are learning jsf validation and conversion then I strongly recommend you to go through this article which clearly explains with examples on in which jsf life cycle phase the validation happens etc.

jsf databinding does not work

I am trying to create a page using richfaces, I created the following field
<h:inputText value="#{petTest.pet.name}"/>
faces-config:
<managed-bean>
<managed-bean-name>petTest</managed-bean-name>
<managed-bean-class>petstore.PetTest</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
managed bean:
package petstore;
public class PetTest {
private Pet pet;
public Pet getPet(){
return pet;
}
public void setPet(Pet pet){
this.pet = pet;
}
}
But I get a field and within it the literal value #{petTest.pet.name} instead of the value of the field. What am I doing wrong?
Is your h:inputText part of a h:form like this:
<h:form id="formId">
..
<h:inputText value="#{petTest.pet.name}"/>
..
</h:form>
Without a form h:inputText is not working.

Resources