2010-11-18 34 views
0

兩個獨立項目(可能是不同的服務器)中的兩個JSF/JSF頁面和相關的託管bean 使用BackingBeanA.java和BackingBeanB.java將它們命名爲PageA.jsp和PageB.jsp(同樣,獨立的項目)單獨項目中JSF頁面之間的數據傳輸

願望是從PageA.jsp重定向到PageB.jsp和傳遞一個對象(篩選數據)

以下被嘗試:

BackingBeanA.java

public String startPageB() { 

    try { 

    FilterData filterData = ... 

     FacesContext facesContext = FacesContext.getCurrentInstance(); 

     HttpServletRequest request = (HttpServletRequest) 
      facesContext.getExternalContext().getRequest(); 
     request.setAttribute("FILTERDATA",filterData); 

     ExternalContext externalContext = facesContext.getExternalContext(); 
     externalContext.setRequest(request); 

     externalContext.redirect("http://localhost:8080/PageB/"); 

    } 
    catch (IOException e) { 
     e.printStackTrace(); 
     System.out.println(e); 
    } 

    return "redirectedData"; 
} 

BackingBeanB.java(在一個單獨的項目,可以是單獨的服務器)

public String getBeanAData() { 

    FacesContext facesContext = FacesContext.getCurrentInstance(); 

    HttpServletRequest request = (HttpServletRequest) 
     facesContext.getExternalContext().getRequest(); 

    FilterData newFilterData = (FilterData) 
     request.getAttribute("FILTERDATA"); 

... do stuff 
return null; 
} 

結果: getBeanAData叫,但是: 篩選數據是不是在BackingBeanA空,但在BackingBeanB爲空 - 數據不轉入。

有關如何正確執行此數據傳遞的任何想法?


我應包括在原題:

的Java 1.6.0_22-B03 JSF 1.2 JSTL 1.2 的Eclipse 3.6.0(太陽神) 的Tomcat 6.0.28(需要運行也可以在Weblogic上) IE 7.0.5730.13 Firefox:3.6.12

如果可能,嘗試使用純JSF,不需要HTTP(但可能),不需要JavaScript(句點)。

我如何解決這個問題(現在不夠好):

從父網頁 -

... 

     FacesContext facesContext = FacesContext.getCurrentInstance(); 
     ExternalContext externalContext = facesContext.getExternalContext(); 

     String initialUrl = externalContext.getInitParameter("RedirectUrl"); 

     String requestDataA = "?ValueA=" + activity.getID(); 
     String requestDataB = "&ValueB=" + activity.getName(); 

     redirectUrl = initialUrl + requestDataA + requestDataB; 

     externalContext.redirect(redirectUrl); 
... 

從重定向網頁 -

... 
     String ownerName = (String) FacesContext.getCurrentInstance(). 
      getExternalContext().getRequestParameterMap().get("ValueB"); 

     String itemId = (String) FacesContext.getCurrentInstance(). 
      getExternalContext().getRequestParameterMap().get("ValueA"); 
... 

的結果是,這個偉大工程 - 用於字符串。這是現在好了。

我想爲以後可能的話什麼:

The same thing, but passing an object. 
I know I cannot do that on the request line, but I thought there was a way 
similar to a standard HTTP setup where a request attribute is set and the 
destination page gets it with a doPost method (do I have this wrong?). 

The BalusC answer indicates this is not possible. 
So is it really not possible to have a JSF page redirect (starup or whatever) 
to an external page, and pass it an object without going to shared storage? 

感謝, 約翰

+0

隨着 「獨立工程」 你這樣實際上意味着 「獨立的Web應用程序上下文」? – BalusC 2010-11-18 18:24:09

回答

0

重定向基本上指示客戶端創建一個全新的請求。原始請求,包括其所有參數和屬性都將丟失。另外,請求屬性不會發送給客戶端。

如果這些web應用程序上下文不共享(這是在服務器級別configureable,在例如Tomcat的,環顧四周採用crossContext關鍵字),那麼最好的辦法是把它作爲請求參數。如果數據太大而無法作爲參數發送,則將其存儲在共享數據源(例如數據庫)中的某處,並將其標識符作爲參數傳遞。

externalContext.redirect("http://localhost:8080/PageB?filterData=" + filterData); 

另一方面,您可以將其設置爲託管屬性。

public class BackingBeanB { 

    @ManagedProperty(value="#{param.filterData}") 
    private String filterData; 

    // ... 
} 

(注意,我假設JSF 2.0,爲JSF 1.x中,你需要在faces-config.xml代替<managed-property>

+0

謝謝你的回答。 – 2010-11-18 18:55:00

+0

它是JSF 1.2和獨立的webapps上下文。 – 2010-11-18 18:56:00