I am trying to mimic what BalusC answered at this post How to make a redirection in JSF But I can not rely make it work. Any hints are greatfully appreciated !! :) When the user hits index.xhtml page then he/she will be emidiatly redirected to another page bringing the geolocation params latitude and longitude which I will keep in my sessionBean along with other acount information.
So I have my faces-config.xml as this
<?xml version="1.0" encoding="utf-8"?>
<faces-config 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-facesconfig_2_0.xsd"
version="2.0">
<!-- =========== FULL CONFIGURATION FILE ================================== -->
<managed-bean>
<managed-bean-name>mapBean</managed-bean-name>
<managed-bean-class>mapp.MapBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
<managed-bean>
<managed-bean-name>forward</managed-bean-name>
<managed-bean-class>mapp.ForwardBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>latitude</property-name>
<value>#{param.latitude}</value>
</managed-property>
<managed-property>
<property-name>longitude</property-name>
<value>#{param.longitude}</value>
</managed-property>
</managed-bean>
<navigation-rule>
<navigation-case>
<from-outcome>map</from-outcome>
<to-view-id>/destination.xhtml</to-view-id>
<redirect />
</navigation-case>
</navigation-rule>
</faces-config>
And I have my ForwardBean.java
package mapp;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
/**
* Reference BalusC
*/
public class ForwardBean {
private String latitude;
private String longitude;
public void navigate(PhaseEvent event){
FacesContext facesContext = FacesContext.getCurrentInstance();
//String outcome = latitude+":"+longitude;
String outcome = "map";
//TODO Add the paramvalue to the session user bject;
facesContext.getApplication().getNavigationHandler().handleNavigation (facesContext, null, outcome);
}
}
And I have my 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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core" template="/layouts/ui.xhtml">
<h:head>
<f:verbatim>
<script type="text/javascript">
var map;
var mapCenter
var geocoder;
function getLatLong()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition( function (position) {
// alert (position.coords.latitude);
//document.getElementById('latitude').value = 'value';
// document.getElementById('longitude').value = 'value';//(position.coords.longitude);
// document.getElementById('location').submit();
// for (key in position.coords){alert(key)}
},
function (error)
{
switch(error.code)
{
case error.TIMEOUT:
alert ('Timeout');
break;
case error.POSITION_UNAVAILABLE:
alert ('Position unavailable');
break;
case error.PERMISSION_DENIED:
alert ('Permission denied');
break;
case error.UNKNOWN_ERROR:
alert ('Unknown error');
break;
}
}
);
}
else
{
//alert("geolocation services are not supported by your browser.");
}
}
getLatLong();
</script>
</f:verbatim>
<title>Map location</title>
</h:head>
<h:body>
<f:view rendered="true" afterPhase="#{forward.navigate}" />
</h:body>
</html>
So it is index that the user hits and then will be forwarded from and bringing lat and long. As a starter I just want do forward with no params at all but it does not work. What is missing? Below is the test destination that I named destination.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:h="http://java.sun.com/jsf/html">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
Hello from Destination
</h:body>
</html>