重定向两个参数

时间:2012-05-25 08:48:14

标签: jsf geolocation primefaces params forwarding

我试图模仿BalusC在这篇文章中回答的内容 How to make a redirection in JSF 但我不能依靠让它发挥作用。 任何提示都非常感谢!! :) 当用户点击index.xhtml页面时,他/她将被emidiatly重定向到另一个页面,带来geolocation params纬度和经度,我将保留在sessionBean中以及其他帐户信息。

所以我将faces-config.xml作为这个

    <?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>

我有我的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); 
                }
            }

我有我的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>

因此它是用户点击的索引,然后将转发并带来lat和long。 作为首发,我只想做任何没有参数的前锋,但它不起作用。 缺什么? 下面是我命名为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>

1 个答案:

答案 0 :(得分:1)

该答案针对的是JSF 1.x.在JSF 2.x中,您最好使用<f:event type="preRenderView">

<h:body>
    <f:event type="preRenderView" listener="#{forward.navigate}" />
</h:body>

@ManagedBean
@RequestScoped
public class Forward {

    @ManagedProperty("#{param.latitude}")
    private String latitude;

    @ManagedProperty("#{param.longitude}")
    private String longitude;

    @ManagedProperty("#{mapBean}")
    private MapBean mapBean;

    public void navigate() {
        mapBean.setLatitude(latitude);
        mapBean.setLongitude(longitude);

        FacesContext context = FacesContext.getCurrentInstance();
        context.getApplication().getNavigationHandler().handleNavigation(context, null, "/destination.xhtml");
    }

    // ...
}

请注意,此处<managed-bean>中不需要<navigation-case>faces-config.xml,这只是JSF 1.x的强制要求。