PrimeFaces 3.0M4 fileUpload doesn't fire after Login - jsf

After a lot of effort I got PrimeFaces 3.0M4 working on Tomcat 7.0.2 with Mojarra 2.1.3. The upload bean fires if I present the upload feature before login but doesn't fire if I login and then come for the file upload.
My filter definition is as follows:
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
<init-param>
<param-name>thresholdSize</param-name>
<param-value>2097152</param-value>
</init-param>
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>c:\temp\</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<!-- Faces Servlet -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Faces Servlet Mapping -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
The login returns to the same page that holds the upload component. Infact I have two forms in the same Facelet defined as follows:
<h:form id = "loginForm">
<h:panelGrid columns="6" rendered="#{loginBean.loginSectionVisible}">
<h:messages errorClass="errorMessage" infoClass="infoMessage"
warnClass="warnMessage"></h:messages>
<h:outputText value="Username : "></h:outputText>
<h:inputText id="username" value="#{loginBean.username}"></h:inputText>
<h:outputText value="Password : "></h:outputText>
<h:inputSecret id="password" value="#{loginBean.password}"></h:inputSecret>
<h:commandButton value="Submit" action="#{loginBean.login}" type="submit"></h:commandButton>
</h:panelGrid>
</h:form>
<h:panelGrid columns="1" rendered="#{loginBean.uploadSectionVisible}">
<h:form enctype="multipart/form-data" prependId="false">
<p:fileUpload fileUploadListener="#{fileUploadBean.handleFileUpload}"
mode="advanced"
update="messages"
sizeLimit="100000"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>
<p:growl id="messages" showDetail="true"/>
</h:form>
I am almost certain that it has something to with the Filter. Any ideas? Would be much appreciated.

<h:panelGrid columns="1" rendered="#{loginBean.uploadSectionVisible}">
<h:form enctype="multipart/form-data" prependId="false">
Whether the upload form is processed depends on the outcome of #{loginBean.uploadSectionVisible} which you have there in the rendered attribute of a parent component. You seem to be requiring that the user is logged in in order to display the upload form. It will work if the #{loginBean} is in the session scope and you aren't doing any business logic in the getter method. But it will fail if the #{loginBean} is in request scope and/or you are doing the business logic based on request parameters.
You need to represent the logged-in user by a separate managed bean which is put in the session scope and fix the rendered attributes something like follows where #{user} is the session scoped bean representing the logged-in user:
<h:form ... rendered="#{not user.loggedIn}">
...
<h:commandButton action="#{login.submit}" ... />
</h:form>
<h:form ... rendered="#{user.loggedIn}">
<p:fileUpload fileUploadListener="#{upload.handle}" ... />
</h:form>
See also:
commandButton/commandLink/ajax action/listener method not invoked or input value not updated

Related

Why the fields in my login page disappear when I redirect after a ViewExpiredException?

I add the following lines to web.xml
<session-config>
<session-timeout>1</session-timeout>
</session-config>
<error-page>
<exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>/login/login.xhtml</location>
</error-page>
to redirect to login page when the session expires and it is in fact redirecting but the fields name and password and the button "fazer login" disappear:
Heres the relevant code in the login page:
<div class="conteudo">
<div class="icone-produto"></div>
<div class="coluna-direita">
<div class="logo-produto" alt="Wplex-EP"></div>
<div class="card signin-card">
<h:panelGrid columns="1">
<h:dataTable value="#{funcionarioController.lista}"
rendered="false"></h:dataTable>
<h:dataTable value="#{escalaController.lista}" rendered="false"></h:dataTable>
<h:dataTable value="#{indisponibilidadeController.lista}"
rendered="false"></h:dataTable>
<h:dataTable value="#{programacoesController.programacoes}"
rendered="false"></h:dataTable>
<h:dataTable value="#{funcionarioController.lista}"
rendered="false"></h:dataTable>
<h:inputText class="texto" placeholder="Usuário"
value="#{loginController.login}" />
<h:inputSecret class="texto" placeholder="Senha"
value="#{loginController.senha}"/>
<h:commandLink id="fazerLoginId" action="#{loginController.isLoginOk}"
styleClass="btn btn-wplex mouseon">
Fazer Login
</h:commandLink>
</h:panelGrid>
</div>
</div>
</div>
The disappearing elements are actually all JSF components. This part,
<location>/login/login.xhtml</location>
must match the <url-pattern> of the FacesServlet entry in the very same web.xml in order to get it to parse the XHTML and render the JSF components as HTML. Apparently you don't have any one on *.xhtml. Perhaps it's *.jsf or /faces/*. You'd need to alter the <location> accordingly to match the <url-pattern> of the FacesServlet, so that it gets invoked. E.g. in case of *.jsf:
<location>/login/login.jsf</location>
An alternative is to just use an <url-pattern> of *.xhtml, so that you never need to fiddle and get confused with virtual URLs.
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
See also:
JSF Facelets: Sometimes I see the URL is .jsf and sometimes .xhtml. Why?

Why does the page change in JSF, but not the URL? [duplicate]

This question already has an answer here:
How to navigate in JSF? How to make URL reflect current page (and not previous one)
(1 answer)
Closed 7 years ago.
I have simple test web-application:
First Page - index.xhtml:
<?xml version='1.0' encoding='UTF-8' ?>
<html>
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
First page
<h:form>
<h:commandButton value="Go to Next page" action="next"/>
</h:form>
</h:body>
</html>
and Next Page - next.xhtml:
<?xml version='1.0' encoding='UTF-8' ?>
<html>
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
Next page
<h:form>
<h:commandButton value="Go to First page" action="index"/>
</h:form>
</h:body>
</html>
When I run my application i have that url on browser:
http://localhost:8080/Test/
and index.xhtml as first page.
Then when I click "Go to Next page" i have url
http://localhost:8080/Test/index.xhtml
and next.xhtml page. And when I click "Go to First page" page changes (to index.xhtml) but url its
http://localhost:8080/Test/next.xhtml
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>
<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>
</web-app>
What should I do to make that the first page in my application has a URL,
http://localhost:8080/Test/index.xhtml
rather than
http://localhost:8080/Test/
?
I use Tomcat (TomEE)/7.0.37
Though it's an old thread, if someone is still looking for it can try "?faces-redirect=true". Like:
<h:commandButton value="Go to Next page" action="next?faces-redirect=true"/>
You're using command buttons (POST form submit buttons) for plain page-to-page navigation. This is completely wrong. You should be using plain buttons (GET navigation buttons) for this instead.
Replace the
<h:form>
<h:commandButton value="Go to Next page" action="next"/>
</h:form>
by
<h:button value="Go to Next page" outcome="next" />
Note: you don't need a <h:form> at all.
Your concrete problem of "one URL behind" is caused because you're seeing the URL of <h:form> itself being reflected in browser address bar instead of the URL of the form submit's result page. Open the JSF page in your webbrowser, rightclick and View Source and look closer at <form action> value.
See also:
Difference between h:button and h:commandButton
When should I use h:outputLink instead of h:commandLink?
What is the difference between redirect and navigation/forward and when to use what?
How to navigate in JSF? How to make URL reflect current page (and not previous one)

How to pass url parameters to JSF/xHTML?

I know this has been asked multiple times but mine is a slightly diff question.
I have a JSF page which queries a database and throws results, before i move to JSF, i used to do this in JSP and it was working fine.
When i was using JSP below is the link format i used to use
http://localhost:8080/blmdatabase/index.jsp?SearchString=Name&Category=Contact&Submit=Submit
My index.jsp used to capture the values using param.SearchString & param.Category, and 'Submit' used to activate the 'submit' button for the search.
How do i do the same for xHTML/JSF ?
Here is what i tried ...
http://localhost:8080/blmdatabase/index.xhtml?search=#{some search string}
In my index.xhtml
<td>
<f:metadata>
<f:viewParam name="search" value="#{databaseSearch.searchstring}" />
</f:metadata>
<p:inputText id="searchstring" size="20" maxlength="20" value="#{databaseSearch.searchstring}"/> <p:commandButton id="submit" icon="ui-icon-search" title="Search Database" update="panel" actionListener="#{databaseSearch.customerList}" />
</td>
in my databaseSearch.java
#ManagedBean(name = "databaseSearch")
#SessionScoped
public class databaseSearch implements Serializable {
public String searchstring;
//getter and setter for searchstring
}
Also, i would need it 'Submit' the form .... I am new to this, so please excuse me if this was already discussed before ...
Also if i specific index.html , my jsf components would not load, just a blank page. like if i go
http://localhost:8080/blmdatabase/
my primefaces components load fine, but if i do
http://localhost:8080/blmdatabase/index.xhtml
it does not, so now i am wondering how to pass the parameters :(
Web.xml
<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>/faces/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
You could 'submit' your form adding <f:event type="preRenderView"> inside the <f:metadata> tag.
<f:metadata>
<f:viewParam name="search" value="#{databaseSearch.searchstring}" />
<f:event type="preRenderView" listener="#{databaseSearch.doSearch}" />
</f:metadata>
This way, you could implement how your bean will search for this specific query string
public void doSearch(ComponentSystemEvent event) {
if(!searchString.isEmpty()) {
// Do your search here
}
}
Your Faces Servlet maps to anything that's hold in the faces virtual folder:
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
So http://localhost:8080/blmdatabase/index.xhtml URL won't be parsed through Faces Servlet. You must use http://localhost:8080/blmdatabase/faces/index.xhtml, note the use of faces/ before your index.xhtml file, also note that your <welcome-file> also points to faces/index.xhtml.
The downside of this URL pattern is that Faces Servlet would also process non-facelets resources like JavaScript files (.js), Style files (.css), images (*.png, *.jpg) and others. A better Faces Servlet mapping would be:
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
In this way, Faces Servlet will process xhtml pages only and you won't need the faces virtual folder anymore. With this change, now you can access to http://localhost:8080/blmdatabase/index.xhtml with no problems.
you could use a method in the class dataBaseSearch:
if(searchString != null){
//execute a query in database
// return result to a variable(resultSet or list)
}
and use getter and setter to get the the resultSet or List
and render the result in a datatable.

primefaces fileupload not showing up

on 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>
on my xhtml
<h:form enctype="multipart/form-data">
<p:fileUpload fileUploadListener="#{testController.handleFileUpload}"
mode="advanced"
update="messages"
sizeLimit="100000"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>
<p:growl id="messages" showDetail="true"/>
</h:form>
already imported the commons-fileupload, and commons-io libraries.
using JSF 2.0 and Primefaces 3.5
my problem is, the upload layout in not displaying, theres no buttons for choose,upload nor cancel.
code reference is here: http://www.primefaces.org/showcase/ui/file/upload/single.xhtml
Maybe the rest of your page has problems.
Do you have h:head instead of head and h:body instead of body? If you don't that could cause your components not to render properly.
I would leave this as a comment, but I don't have enough points yet
Are you using MyFaces or Mojarra? The Primefaces Team develop thinking only to work on Mojarra, their Ajax implementation for example didn't work with composite:clientbehavior.
Try with Mojarra.
I had the same problem. I spent a few days on this and finnaly I realize that I had in my lib RICHFACES and PRIMEFACES so I decided to delete RICHFACES and the component started to render right! Take a look at this...

PrimeFaces file upload stopped working

I have a problem with file upload using PrimeFaces. I went through old post here on StackOverflow a didn't find anything useful. Strange thing is that I made it work yesterday but I started my server now and it's working anymore. It's giving me NPE when I try to access the uploaded file.
So I downloaded commons-fileupload-1.2.2.jar and commons-io-1.4, put them in my classpath, configured my web.xml like this
<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>
My form in xhtml page contains one field with description, one combo box and field upload element
<h:form enctype="multipart/form-data">
<p:panel header="#{submitProjectPage['header']}">
<h:panelGrid columns="2">
#{submitProjectPage['chooseProject']}
<p:selectOneMenu value="#{submitProjectBean.project}" converter="projectConverter">
<f:selectItems value="#{submitProjectBean.studentsProjects}" />
</p:selectOneMenu>
And finally my bean is RequestScoped and has this method
private UploadedFile projectFile;
public void submitProject(ActionEvent event) {//also tried without parameter
project.setFile(projectFile.getContents());
project.setStatus(StatusEnum.DELIVERED);
daoBean.update(project);
}
#{submitProjectPage['submitInformation']}
<p:inputTextarea rows="10" value="#{submitProjectBean.s}"/>
#{submitProjectPage['file']}
<p:fileUpload value="#{submitProjectBean.projectFile}" mode="simple" />
<p:commandButton ajax="false" value="#{submitProjectPage['submit']}"
actionListener="#{submitProjectBean.submitProject}" />
</h:panelGrid>
</p:panel>
I am sure I have my imports right, there is also a setter for projectFile field so I really don't know where could be the problem. I am using PrimeFaces v 3.01
Thanks for help
I found a solution, the thing was that Glassfish for some reason didn't deploy the apache libraries, so I removed them from classpath, added them again and it magically works:-)
So maybe it will help somebody:)

Resources