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

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)

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?

Primefaces 5 Carousel showcase - renders images, but uses bullet points instead of a Carousel

This is what the Primefaces Carousel is looking like in Primefaces 5.0:
I created a new Eclipse(Dynamic Web Project) project in an attempt to reduce as many variables as possible.
My libraries used:
Primefaces 5.0 (I also tried many 3 and 4 versions)
Mojarra 2.2.6 (and I tried a few versions lower than this too)
running on Tomcat 7.0.52
Here is my web page code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head></h:head>
<h:body>
<f:view contentType="text/html">
<h:form id="form">
<h:body>
<p:carousel circular="true" vertical="true">
<p:graphicImage name="images/csvExport.png" />
<p:graphicImage name="images/csvExport.png" />
</p:carousel>
</h:body>
</h:form>
</f:view>
</h:body>
</html>
This is my web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 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-app_2_5.xsd">
<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>/faces/*</url-pattern>
<url-pattern>*.xhtml</url-pattern>
<url-pattern>*.html</url-pattern>
<url-pattern>*.jsp</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>
</web-app>
Yes, I know there are other questions about the Primefaces Carousel. I read those and dozens of other articles elsewhere for most of the day.
I tried throwing .jstl libraries in there too. Adding primefaces themes added a bordering box around it, but the Carousel still wasn't right.
The web.xml file is exactly what I have in another Eclipse web project that works just fine for all the other primefaces components I have used, so I'm not thinking the web.xml file is the problem.
Any ideas on how I can get this Primefaces Carousel to render properly? the images are pulling up, but the "flesh" of the Carousel component just isn't coming through.
Edit:
The Primefaces show case for Carousel is here:
http://www.primefaces.org/showcase/ui/data/carousel.xhtml
My objective is to get the most basic Carousel to work.
Edit2(05-27-2014):
Per LarsBauer's suggestion to add a header tag, the output web page now looks a little different. It looks like a rectangle instead of a bulleted list, but still isn't quite where it needs to be. I tried adding primefaces styles in the header that I used in other Primefaces projects(copying over the files too, of course), but that didn't change anything.
Edit3(05-27-2014):
Per Templar's suggestion, I added the h:body tag. The change didn't do anything different with the output, but I would imaging the page would become problematic later on if that tag weren't there. Not sure why I forgot to add it.
Your h:form needs to be inside your h:body. Also take a look at your Javascript console to see any clientside rendering errors.
<f:view contentType="text/html">
<h:body>
<h:form id="form">
<p:carousel circular="true" vertical="true">
<p:graphicImage name="images/csvExport.png" />
<p:graphicImage name="images/csvExport.png" />
</p:carousel>
</h:form>
</h:body>
</f:view>
It sucks that it took me so many days to figure this out, but Internet Explorer 8 is the problem. Any modern browser displays everything properly. The other Primefaces components I use in other projects work fine, but not so with Carousel.
It's my company that mandates the older version be used, and I bet you'd know exactly who that is >.>

<h:button> not executing simple bean method

im trying to do a simple bean method, but it does not work, dont show the syso in the console of eclispe, and when i click the button, it change my url to http://localhost:8080/Projeto01/index.jsf?jftfdi=&jffi=%2Findex.xhtml
why dont work and why it change the url for this strange url ?
my msg.java(bean)
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
#ManagedBean
#SessionScoped
public class Msg {
public void show() {
System.out.println("Working Bean Method");
}
}
my index.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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<ui:composition>
<h:head></h:head>
<h:body>
<h:form>
<h:button value="Show" action="#{msg.show()}"></h:button>
</h:form>
</h:body>
</ui:composition>
</html>
and my 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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>do0</display-name>
<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>*.jsf</url-pattern>
</servlet-mapping>
</web-app>
The <h:button> is a simple page-to-page navigation button. As you can see in the tag documentation, it doesn't support the action attribute at all. You're most likely confusing the <h:button> with <h:commandButton> which in turn supports that attribute.
In order to achieve your functional requirement of invoking a JSF backing bean method on press of a button, just replace <h:button> by <h:commandButton>:
<h:commandButton value="Show" action="#{msg.show()}" />
See also:
Difference between h:button and h:commandButton
As to those jftfdi and jffi query string parameters in the target URL, this is a bug in Mojarra's implementation of the new JSF 2.2 flow scope. This is fixed in Mojarra 2.2.5. Note that this is further unrelated to your concrete problem as you shouldn't be using a <h:button> in first place.
See also:
How to disable jftfdi jffi query params in JSF

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.

Basic JSF with Session doesn't work in glassfish

I wanted to test Session replication of session and application scoped beans of JSF 2 in a clustered environment, currently trying with glassfish. To do so I created a simple application that I deployed to the server first to verify it's working (it doesn't).
Here is what I did so far:
Setup a Ubuntu Virtual Machine in Oracle VirtualBox
download glassfish as zip-file and unzip it in the home directory
start glassfish
deploy the JSF-app as war-file
go to the page of the app: localhost:8080/jsftest/index.jsf
try to set a variable saved in session/application scope
Depending on javax.faces.STATE_SAVING_METHOD in web.xml the result:
server: viewExpiredException is thrown
client: value is not stored and default value is shown
To create the app I did the following:
create new dynamic web project in eclipse
unzip all files from myfaces core in WEB-INF/lib
set the classes-folder to WEB-INF/classes
write beans and a .xhtml as below
The session scoped bean (application scoped is similiar):
#ManagedBean(name = "sessbean")
#SessionScoped
public class MySessionScopeBean implements Serializable{
private static final long serialVersionUID = -4733271400646173098L;
private String value = "default session data";
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
The index.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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>JSF 2.0 Hello World</title>
</h:head>
<h:body>
<h3>JSF 2.0 Hello World Example - hello.xhtml</h3>
<h:outputText value="Session id: #{sessionScope['id']}" />
<h:form>
<h:panelGroup>
<h:outputText value="Session Variable:"/>
<br/>
<h:inputText value="#{sessbean.value}"/>
</h:panelGroup>
<h:commandButton value="submit"/>
</h:form>
<h:form>
<h:panelGroup>
<h:outputText value="Application Variable:"/>
<br/>
<h:inputText value="#{appbean.value}"/>
</h:panelGroup>
<h:commandButton value="submit"/>
</h:form>
</h:body>
</html>
So this very basic example doesn't work. Any suggestions?
Also the Session-ID is not shown by #{sessionScope['id']}, don't know if that is the way to do it.
Got it working by removing parts of the (generated) web.xml file. The working file looks like this:
<?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">
<display-name>jsftest</display-name>
<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>*.jsf</url-pattern>
</servlet-mapping>
</web-app>
What I stripped out (don't know which part caused the failure):
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<description>
This parameter tells MyFaces if javascript code should be allowed in
the rendered HTML output.
If javascript is allowed, command_link anchors will have javascript code
that submits the corresponding form.
If javascript is not allowed, the state saving info and nested parameters
will be added as url parameters.
Default is 'true'</description>
<param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<description>
If true, rendered HTML code will be formatted, so that it is 'human-readable'
i.e. additional line separators and whitespace will be written, that do not
influence the HTML code.
Default is 'true'</description>
<param-name>org.apache.myfaces.PRETTY_HTML</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<description>
If true, a javascript function will be rendered that is able to restore the
former vertical scroll on every request. Convenient feature if you have pages
with long lists and you do not want the browser page to always jump to the top
if you trigger a link or button action that stays on the same page.
Default is 'false'
</description>
<param-name>org.apache.myfaces.AUTO_SCROLL</param-name>
<param-value>true</param-value>
</context-param>
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>

Resources