2013-04-04 70 views
0

我需要在載入jsf頁面之前執行Web服務(方法)調用。該調用將返回必須在我的jsf頁面上顯示的輸入字段列表。用戶可以填寫表單,然後點擊下一步,我需要將表單中輸入的值發送回另一個Web服務(方法)。 我的方法是爲jsf頁面(包含一個空白表單和一個綁定到bean)的請求作用域bean,並在我的表單方法的setter方法中執行web服務調用並動態創建UIInput字段
JSF上的動態字段

//call web service 
//Loop 
    UIInput input = new HtmlInputText(); 
    //set unique Id 
    form.getChildren().add(input); 
//End Loop 

它確實創建了輸入字段,但是如果我執行瀏覽器回來或刷新它不斷添加輸入字段。很明顯,我的做法是錯誤的。
此外,我發現,當我試圖讓這些動態創建的輸入字段中的值上提交的行動像

List<UIComponent> dynamicFields = form.getChildren(); 
for(int i=0;i<form.getChildCount();i++){ 
    if("javax.faces.Input".equals(componentFamily)){ 
     UIInput input = (UIInput)dynamicFields.get(i); 
     System.out.println("Input Field: ID = "+input.getId() + " , Value="+ input.getValue()); 
     } 
} 

領域的ID是正確打印,但是值始終爲空。顯然這樣做都是錯誤的。

請讓我知道什麼時候以及在什麼時候做我創建領域,我怎麼捕捉使用JSF 2.0 PS上午這些值,JDeveloper中,Glassfish和/或Weblogic Server的

+0

什麼是你的情況的動態領域的工作有什麼好處?爲什麼你不是簡單地使用循環,並在視圖中靜態創建組件? – Bardelman 2013-04-04 13:43:30

+0

好吧,原因是我不會總是使用單一類型的輸入。 Web服務也返回我輸入的類型。我將使用某種開關來構建必要的表單。 – 2013-04-04 16:46:11

+0

那麼,我想使用組件的動態添加(對於我來說,我有另一個具體的原因),但它似乎有很多問題在這個問題上,也是一個糟糕的文檔。如果你找不到解決方案,你可以用一個 jstl標籤來製作你需要的靜態開關(我猜你已經知道了:D)。 – Bardelman 2013-04-04 17:12:34

回答

-1

此問題的原因的範圍你如果它是@RequestScoped,則表示每次刷新或調用頁面時要再次調用post constuctor(@PostConstuct)方法,因此再次創建該作業併爲空值您應該將輸入字段添加到每個輸入字段值表達式以將值存儲在其中。

private String inputValue; //setter() getter() 
    UIInput input = new HtmlInputText(); 

    @PostCostruct 
    public void addInput() 
    { 
     // your previos create and add input fields to the form + setting value expression 
     Application app = FacesContext.getCurrentInstance().getApplication(); 
     input.setValueExpression("value",app.getExpressionFactory().createValueExpression(
        FacesContext.getCurrentInstance().getELContext(), "#{bean.inputValue}", String.class)); 
    } 

正確的答案,如果你正在使用綁定不要使用請求範圍內使用會話範圍,它會與您合作,獲取數據不爲空時檢索值。

+0

不好的建議。組件實例是請求作用域。如果在同一會話的多個瀏覽器選項卡/窗口中打開相同的視圖,將它們綁定到會話作用域中的bean只會導致「重複的組件ID」錯誤,或者至少會導致嚴重的JSF狀態完整性問題。您必須**將組件實例綁定到請求作用域bean。視圖範圍也可以,但是這需要最少的JSF 2.2(或者在Mojarra 2.1.18版本中)由於雞蛋問題1492導致在視圖構建期間重新創建視圖範圍的bean。 – BalusC 2013-04-24 11:08:20

0

從你的問題我不能確定你希望從你的web服務獲得什麼樣的數據,以及你想要渲染它的是什麼樣的組件。我在下面的答案假設你總是會收到一個列表的字符串,你會在文本框中顯示它們。

一種可能的方法是調用web服務並在@PostConstruct方法中獲取數據,將這些數據放入列表中,然後將數據呈現在數據表中。下面的代碼。

豆:

@ManagedBean(name="bean") 
@ViewScoped 
public class YourBean implements Serializable { 


private static final long serialVersionUID = 1L; 

private List<String> values = new ArrayList<String>(); 

    //The method below @PostConstruct is called after the bean is instantiated 
    @PostConstruct 
    public void init(){ 
      //fetch data from source webservice, save it to this.values 
    } 

    public void save(){ 
     for(String s: this.values) 
      // send s to destination webservice 
    } 

    public List<String> getValues(){ 
     return this.values; 
    } 

    public void setValues(List<String> values){ 
     this.values = values; 
    }  

} 

XHTML摘錄:

<h:form> 
    <h:dataTable value="#{bean.values}" var="s"> 
      <h:column> 
       <h:inputText value="#{s}" /> 
      </h:column> 
    </h:dataTable> 
    <h:commandButton value="Save" action="#{bean.save}" /> 
</h:form>