2012-05-25 47 views
1

我想模仿BalusC在這篇文章中回答的問題 How to make a redirection in JSF 但是我不能讓它工作。 任何提示都非常感謝! :) 當用戶點擊index.xhtml頁面時,他/她將被emidiatly重定向到另一個頁面,帶來地理位置參數的經緯度,我將保留在我的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> 

所以它是用戶點擊的索引,然後將被轉發並帶來經緯度。 作爲一個首發,我只是想向前沒有參數,但它不起作用。 缺什麼? 下面是測試目的,我叫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

這個回答是針對於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"); 
    } 

    // ... 
} 

注意,你不需要在faces-config.xml這裏<managed-bean><navigation-case>,這只是對JSF 1.x的強制性

+0

好極了,現在我們在前面的工作中,接下來我會從客戶端javascript中添加params,並將它們帶到一起。我會盡快發佈我的進度,非常感謝! – user1416776

+0

不客氣。既然你是新來者,請不要忘記標記接受的答案(大多數情況下)以解決問題。另請參閱[如何接受答案的工作?](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235) – BalusC

+0

再次只是爲了澄清,我supose如果沒有參數,我必須使用「navigate()方法導航(PhaseEvent事件)」,但是當我想要傳遞參數時,我需要「導航(PhaseEvent事件)」我假設我還添加了 但他們不會來,我需要做一些其他的事情,但可能會標記爲由BalusC解決 爲你的知識,我會明天回到這裏 – user1416776