1

我试图模仿 BalusC 在这篇文章 How to make a redirection in JSF 中的回答, 但我不能依靠它来工作。任何提示都非常感谢!:) 当用户点击 index.xhtml 页面时,他/她将被重定向到另一个页面,带来地理定位参数纬度和经度,我将与其他帐户信息一起保存在我的 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的就是index。作为一个初学者,我只想在没有参数的情况下向前推进,但它不起作用。什么不见​​了?下面是我命名为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>
4

1 回答 1

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>and <navigation-case>faces-config.xml这只是 JSF 1.x 的强制要求。

于 2012-05-25T11:57:44.960 回答